evertonrobertoauler / cordova-plugin-firebase-messaging

Cordova plugin for Firebase Cloud Messaging

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

cordova-plugin-firebase-messaging
NPM version NPM downloads

Cordova plugin for Firebase Cloud Messaging

Installation

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.

Supported Platforms

  • iOS
  • Android

Methods

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.

onMessage(callback)

Called when a push message received while app is in foreground.

cordova.plugins.firebase.messaging.onMessage(function(payload) {
    console.log("New foreground FCM message: ", payload);
});

onBackgroundMessage(callback)

Called when a push message received while app is in background.

cordova.plugins.firebase.messaging.onBackgroundMessage(function(payload) {
    console.log("New background FCM message: ", payload);
});

requestPermission

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);
});

getToken

Returns a promise that fulfills with the current FCM token

cordova.plugins.firebase.messaging.getToken().then(function(token) {
    console.log("Got device token: ", token);
});

onTokenRefresh(callback)

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(topic)

Subscribe to topic in background.

cordova.plugins.firebase.messaging.subscribe("New Topic");

unsubscribe(topic)

Unsubscribe from topic in background.

cordova.plugins.firebase.messaging.unsubscribe("New Topic");

getBadge(callback)

Reads current badge number (if supported).

cordova.plugins.firebase.messaging.getBadge().then(function(value) {
    console.log("Badge value: ", value);
});

setBadge(value)

Sets current badge number (if supported).

cordova.plugins.firebase.messaging.setBadge(value);

About

Cordova plugin for Firebase Cloud Messaging

License:MIT License


Languages

Language:Objective-C 51.1%Language:Java 43.3%Language:JavaScript 5.6%