Cordova plugin for Firebase Cloud Messaging
cordova plugin add cordova-plugin-firebase-messaging --save
If you need to set a specific dependency version on Android then use variable FIREBASE_VERSION
.
Plugin depends on cordova-support-google-services for setting up google services properly. Please read the README carefully in order to avoid common issues with a project configuration.
- iOS
- Android
In general (for both platforms) you can only rely on custom data fields from a FCM payload.
For iOS APNS payload is stored in aps
object. It's available when a message arrives in both foreground and background.
For Android GCM payload is stored in gcm
. It's available ONLY when a message arrives in foreground. For a some reason Google applied this limitation into their APIs. Anyway I've created an issue for a future improvement.
Called when a push message received in FOREGROUND.
cordova.plugins.firebase.messaging.onMessage(function(payload) {
console.log("New foreground FCM message: ", payload);
});
Called when a push message received in FOREGROUND.
cordova.plugins.firebase.messaging.onBackgroundMessage(function(payload) {
console.log("New background FCM message: ", payload);
});
Grant permission to recieve push notifications (will trigger prompt on iOS).
cordova.plugins.firebase.messaging.requestPermission().then(function(token) {
console.log("APNS device token: ", token);
});
Returns a promise that fulfills with the current FCM token
cordova.plugins.firebase.messaging.getToken().then(function(token) {
console.log("Got device token: ", token);
});
Triggers every time when FCM token updated. You should usually call getToken
to get an updated token and send it to server.
cordova.plugins.firebase.messaging.onTokenRefresh(function() {
console.log("Device token updated");
});
Use this callback to get initial token and to refresh stored value in future.
Subscribe to topic in background.
cordova.plugins.firebase.messaging.subscribe("New Topic");
Unsubscribe from topic in background.
cordova.plugins.firebase.messaging.unsubscribe("New Topic");
Reads current badge number (if supported).
cordova.plugins.firebase.messaging.getBadge().then(function(value) {
console.log("Badge value: ", value);
});
Sets current badge number (if supported).
cordova.plugins.firebase.messaging.setBadge(value);