KnockDocs

How to send push notifications to Firebase Cloud Messaging

In this guide we'll walk through how to configure a Firebase Cloud Messaging (FCM) provider in Knock to send push notifications. This guide assumes that you've already created an FCM channel in the Knock dashboard.

How to configure FCM with Knock

To configure FCM with Knock, you'll need your Firebase Project ID and the complete contents of your Service Account JSON file.

You can get both of these by logging into the Firebase console and navigating to project settings. Once you have them, go back to the environment configuration for your FCM channel, enter in your Project ID and Service Account JSON file, and you're good to go.

Using FCM with Knock

In order to use FCM with Knock you'll need to synchronize your users device tokens retrieved from the FCM SDK in either Android, iOS, or Web to Knock by setting channel data for your recipient.

You can follow the appropriate quickstart guide for your platform on FCM to see how to get the device token.

Managing tokens

Knock makes no assumptions about managing your device tokens for you. Meaning if a recipient opts-out of receiving notifications on a device, or the token expires, it is up to you to remove the token from the recipients channel data. This behavior is subject to change in the future.

Data passed to FCM

When sending a notification to FCM, we also pass through the following attributes:

PropertyTypeDescription
knock_message_id*stringThe message ID of the corresponding Knock message
data *stringAny key/value data passed through in your notify call, formatted for FCM (as strings)

Silent/data notifications

We support sending FCM notifications as "silent", data-only notifications within Knock. You can enable this per push notification template under the 3-dot menu in the header of the template editor.

When silent push is enabled, we'll no longer pass through the message payload. All properties in the data payload described above will be sent with your notification still.

Customizing your notifications

If you need to further customize the payload sent to FCM when issuing your notification like adding custom badges or sounds, please reach out to us.

Common FCM errors

The following are common FCM errors you may see in your message delivery logs:

ErrorMeaning / action to take
UNREGISTEREDThe device token provided is not valid and should be removed from the recipients channel data.
INVALID_ARGUMENTThe device token given may be incorrect.

Channel data requirements

In order to use a configured FCM channel you must store a list of one or more device tokens for the user or the object that you wish to deliver a notification to. If you use multiple device tokens for a single user or object, Knock will generate and try and deliver a notification for each unique token.

PropertyTypeDescription
tokens*string[]One or more device tokens