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:

knock_message_id*stringThe message ID of the corresponding Knock message
data *stringAny key/value data passed through in your trigger 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.

Using overrides to customize notifications

We have full support for overriding the payload sent to FCM for adding things like badge counts, extra data properties, and sound files. You can set the push overrides under the 3 dot menu in "Manage settings > Push overrides". Push overrides support liquid for injecting data properties and referencing attributes on your recipients.

See the FCM documentation for details.

apnsdictionaryAPNS specific overrides
datadictionaryExtra data properties to add
androiddictionaryAndroid specific overrides
fcm_optionsdictionaryFCM specific options
notificationdictionaryNotification overrides
webpushdictionaryWebpush specific overrides

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.

tokens*string[]One or more device tokens