Cordova plugin for Firebase Cloud Messaging
cordova plugin add cordova-plugin-firebase-messaging --save
You can also specify variables ANDROID_NOTIFICATION_ICON
and ANDROID_NOTIFICATION_COLOR
.
- iOS
- Android
Called when a message is received.
window.cordova.plugins.firebase.messaging.onMessage(function(payload) {
console.log("New FCM message: ", payload);
});
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.
Logs an instance id token received.
window.cordova.plugins.firebase.messaging.onTokenRefresh(function(token) {
console.log("Got device token: ", token);
});
Use this callback to get initial token and to refresh stored value in future.
Subscribe to topic in background.
window.cordova.plugins.firebase.messaging.subscribe("New Topic");
Unsubscribe from topic in background.
window.cordova.plugins.firebase.messaging.unsubscribe("New Topic");
Reads current badge number (if supported).
window.cordova.plugins.firebase.messaging.getBadge(function(value) {
console.log("Badge value: ", value);
});
Sets current badge number (if supported).
window.cordova.plugins.firebase.messaging.setBadge(value);
Grant permission to recieve push notifications (will trigger prompt).
window.cordova.plugins.firebase.messaging.requestPermission(function(token) {
console.log("APNS device token: ", token);
});