The tabris-plugin-firebase plugin project provides a Tabris.js API to receive firebase cloud messages. The plugin allows to receive notification data when the app is in the foreground or to automatically display a notification when the app is not running or in the background. Currently only Android is supported with iOS support coming in the future.
The following snippet shows how the tabris-plugin-firebase plugin can be used to receive a cloud message in a Tabris.js app.
console.log('Token to send to backend: ' + firebase.Messaging.token);
firebase.Messaging.on('tokenChanged',
({token}) => console.log('Server token refreshed: ' + token));
firebase.Messaging.on('message',
({data}) => console.log('Received message data: ' + JSON.stringify(data)));
console.log('Message data from app cold start: ' + firebase.Messaging.launchData);A more elaborate example can be found in the example folder. It provides a Tabris.js app that demonstrates the various features of the tabris-plugin-firebase integration.
To send a message from the server side a curl command similar to the following POST request can be used:
curl -X POST -H "Authorization: key=<server-key>" -H "Content-Type: application/json" -d '{
"to": "<token>",
"data": {
"title": "New data",
"body": "The new data arrived",
"payload": "custom data"
}
}
' https://fcm.googleapis.com/fcm/sendAn Android notification icon can be provided via the plugin variable ANDROID_NOTIFICATION_ICON. To configure a notification icon a
resource id of an Android drawable can be specified. The <resource-file /> cordova directive can be used to copy the file into the Android folder app/src/main/res/. See the example
config.xml for a snippet to get you started.
The icon can be configured inside your apps config.xml:
<plugin name="tabris-plugin-firebase" spec="^2.0.0">
<variable name="ANDROID_NOTIFICATION_ICON" value="@drawable/ic_notification" />
</plugin>
<platform name="android">
<resource-file src="res/android/drawable-xhdpi/ic_notification.png"
target="app/src/main/res/drawable-xhdpi/ic_notification.png" />
</platform>When no notification icon is specified, the outline of the app icon is used.
When the server sends a message to a device it can create two types of messages: "notification" messages and "data" messages. Messages that contain a notification key in its json payload are treated as "notification" message. More details of the differences between "notification" and "data" messages can be found in the firebase documentation.
The key difference for this plugin is that "notification" messages create their notification automatically when the app is in the background. Tapping on the notification does not forward the notification data to the app.
To create a notification that also delivers its data to the app, when the notification is tapped, a regular "data" notification has to be created. A "data" message does not have a key notification.
To configure the notification several properties can be set:
id:numbertitle:stringbody:string
The following message send from a server would create a notification similar to the screenshot above:
POST /fcm/send HTTP/1.1
Host: fcm.googleapis.com
Authorization: key=<server-key>
Content-Type: application/json
{
"to": "<token>",
"data": {
"title": "New data available",
"body": "The new data can be used in a multitude of ways",
"payload": "custom data"
}
}Note that the json object above does not contain a notification key. It only provides the to key to declare the message receiver and the data payload send to the app.
Using the same id for multiple messages updates an existing notification on the users device. Omitting the id creates a random id on the device so that each message results in a unique notification.
The firebase messaging API is represented as the global object firebase.Messaging.
All Messaging properties are read only.
- A stable identifier that uniquely identifies the app installation. Note that on Android the instance id can become invalid as noted in the documentation.
- A registration
tokento be used on the server side to address an app installation. The registrationtokenis usually available but can change during the apps lifetime. To get notified of a registration token updates you should listen fortokenChangedevents. When resetting theinstanceId, the token is not available until thetokenChangedevent fired.
-
Contains the cloud message data when the app is cold started from a notification. There are two scenarios of message data delivery:
- When the app is in the foreground (or running in the background and the user taps on the notification) the
messageevent callback is invoked. - In case the app process is not running and the app is freshly launched (cold started) from a notification, the data contained within that notification is available in the
launchDataobject.
- When the app is in the foreground (or running in the background and the user taps on the notification) the
-
The recommended way to make sure your app receives all messaging data is to check the
firebase.Messaging.launchDataobject on app startup and to register for themessageevent to receive follow-up messages.
- The
instanceIdChangedevent is fired asynchronously when theresetInstanceId()method is invoked.
target: Messaging- The
Messagingobject which allows to interact with firebase cloud messaging
- The
instanceId: string- The new
instanceIdof the app
- The new
- The
tokenChangedevent is fired when the current registration token has changed. Check the firebase documentation for details when that might be the case.
target: Messaging- The
Messagingobject which allows to interact with firebase cloud messaging
- The
token: string- The new registration
tokento send to backend server
- The new registration
- The
messageevent is fired when a cloud message is received by a running app. This can either be while the app is in the foreground or the user clicks on a notification while the app is running in the background. To get the message data while the app is cold launched from a notification see thelaunchDataproperty.
target: Messaging- The
Messagingobject which allows to interact with firebase cloud messaging
- The
data: object- The message
dataobject as send from the server side
- The message
- Invalidates the current
instanceIdand creates a new one asynchronously. To be notified when a newinstanceIdis available you should listen for theinstanceIdChangedevent. Resetting theinstanceIdalso resets the associated registrationtoken. AtokenChangedevent will be fired once a new token is available.
- Shows the system dialog for requesting permissions for the first time. Does nothing if the permissions have been requested already. The dialog will only be shown again when calling this method if the app has been uninstalled for at least a day or if the device has been restored (see "Resetting the Push Notifications Permissions Alert on iOS" for more information).
