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.
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.
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.
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.
When sending a notification to FCM, we also pass through the following attributes:
|knock_message_id*||string||The message ID of the corresponding Knock message|
|data *||string||Any key/value data passed through in your trigger call, formatted for FCM (as strings)|
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.
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 on the template settings modal, open it by clicking on the "Manage template settings" button. Push overrides support liquid for injecting data properties and referencing attributes on your recipients.
See the FCM documentation for details.
|apns||dictionary||APNS specific overrides|
|data||dictionary||Extra data properties to add|
|android||dictionary||Android specific overrides|
|fcm_options||dictionary||FCM specific options|
|webpush||dictionary||Webpush specific overrides|
The following are common FCM errors you may see in your message delivery logs:
|Error||Meaning / action to take|
|The device token provided is not valid and should be removed from the recipients channel data.|
|The device token given may be incorrect.|
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|