title: Sending push notification on firestore document field change
body: <p>I have a Flutter app that lets users rent items from eachother with Firestore RTDB. In my rental document, I have a field <code>status</code> that determines the status of the rental (think of it like shipping an item, where items can have status of 'ordered', 'shipped', 'delivered' etc). My <code>status</code> variable is a number between 0 and 5, and each number represents a different phase. When the <code>status</code> variable changes, I want to notify the other user in the rental with a push notification. But I don't know which of the following methods is best.</p>
  
  <ol>
  <li><p>The first way is to use a cloud function that triggers every time the rental document is updated. But I only check for the <code>status</code> field. It would look something like this:</p>
  
  <pre><code>exports.notify = functions.firestore.document('rentals/{rentalId}')
  .onUpdate(async (snapshot, context) =&gt; {
      const oldSnap = snapshot.before.data(); // previous document        
      const newSnap = snapshot.after.data(); // current document       
  
      // status changes from 0 to 1
      if (oldSnap.status === 0 &amp;&amp; newSnap.status === 1) {
          // do something
      }
  })
  </code></pre>
  
  <p>The one downside I can think of is I would have to do another read to get the device push token of the other user. Also, for every rental document update this cloud function will trigger, and ultimately may not even need to execute in the first place</p></li>
  <li><p>The other way would be to have a <code>notifications</code> collection that stores notifications, and have a cloud function that triggers when a new notification document is added. Then, on the client side, when the user taps a button, update the <code>status</code> in the rental as well as create a new notification document.</p>
  
  <pre><code>Firestore.instance
      .collection('rentals')
      .document(rentalId)
      .updateData({'status': newStatus});
  
  Firestore.instance.collection('notifications').add({
    'title': title,
    'body': body,
    'pushToken': &lt;TOKEN HERE&gt;,
  });
  </code></pre>
  
  <p>In comparison to method 1, this does an extra write instead of a read.</p></li>
  </ol>
  
  <p>Which method is better?</p>