Firebase Push Notification: Use Cases, Tutorial & Example (2023)

Retention is the most important metric for many startups: no matter how many new users subscribe, you're wasting time and money if they don't stick around. In fact, most of your early marketing efforts should focus on retention to reach product-market fit. One way to grow your retention rate is to use push notifications. They are a great way to re-engage users because they are displayed directly on devices whether you are browsing your app or not.

In this article, we'll see how to send notifications with Firebase Cloud Messaging (FCM) through Rowy, our low-code Firebase CMS.

Use Cases

Push notifications are useful to signal a variety of in-app events, whether you offer a SaaS, an ecommerce store, or an esport game.

With Rowy, you can trigger push notifications whenever a change occurs in your table. For example, you can send a notification to your users when a new order is placed, or when a new product is added to your catalog: the use cases are unlimited!

If you send emails to your users, you can also use push notifications, or skip the emails and go straight to notifications if the conversion rates make sense.

Implementing Push Notifications With Google Cloud Messaging

1. Sending Push Notifications

With Rowy, adding push notifications to your app only takes a minute: just enable the Push Notification extension and you're good to go! The Push Notifications extension wraps Firebase Cloud Messaging, a cross-platform solution to send messages for free.

In your Rowy table, click the Extensions icon and pick 'Push Notification':

1.webp

You can choose when to send the notification―whenever a row is created, updated, and/or deleted―but also trigger a message whenever you want via Rowy Action column.

You can also customize the code logic in the Extension body tab to suit any use case:

0.webp

If we look more closely at the code, we can see that the extension body is a function that takes a row object as a parameter―the data of the row that triggered the notification. You can also access the entire Firestore instance linked to your Rowy account via the db variable. Finally, the function returns an array of notifications to send:

const extensionBody: PushNotificationBody = async({row, db, change, ref, logging}) => {
  return [{
    notification: {
      title: 'Hello!',
      body:"test"
    },
    token: FCMtoken
  }]
}

Of course, you can add any Javascript code you want to create more actionable and engaging push notifications, which is key to generate high conversion rates to your app.

The only thing we need for this code to work is a Firebase Cloud Messaging (FCM) token.

2. FCM Tokens

To send a notification to specific users, you need their FCM tokens. A FCM token is a way to authenticate users, and users need to enable this feature to consent to receive push notifications. It's a way to protect users from notification spam.

The whole FCM registration process takes place on the client side, so you need to store the tokens in your database first. Depending on the client device, your front-end code logic will differ so have a look at the official documentation to get started.

After storing the tokens in your database during the registration process, and because Rowy is a Firebase CMS, you can easily obtain FCM tokens for your users connected with Firebase Auth. If your fcmtoken is under a subcollection of users, you can get the fcmtoken with a simple snapshot:

const user = row.uid

const snapshot = await db.collection('users').doc(user).collection('fcm_tokens').get()

const tokens = snapshots.docs.map(doc=>doc.get("fcm_token"))

Each token is linked to a specific device, so you can send a notification to a specific user by sending a message to each of their tokens.

You can customize the message in the extension. Show this in two ways:

  • First directly via the extension for every create
  • Second, actionable one with action button. Another table that stores a list of push notification messages to be sent that when clicked it sends to all users with FCMtoken

3. Action trigger

As previously mentionned, you don't need create / update / delete events to trigger the Push Notification extension. You can also use an Action column in callable mode.

The Push Notification extension is a standalone Cloud function, so you can call it from an Action column: just create a new action column, select the callable mode instead of script, and pick your push notification extension. Done!

This can be useful to send global, one-time notifications to all your users to tell them about a big release or any type of announcement.

4. Sending to Multiple Users

Since the extension takes an array of notifications as a parameter, you can send a notification to multiple users at once. Just fetch all the tokens from your database and send a message to each of them.

Just like emails, beware to avoid spamming users. It's good practice to remove expired tokens, keep track of users who do not engage with your push notifications, and give them the option to opt-out.

Ask Us On Discord

If you have any questions about push notifications, or if you want to share your use cases to help us improve this article, feel free to reach out on Discord.

Push notifications are powerful, if used properly.

Get started with Rowy in minutes

Continue reading

Browse all