Skip to content

salim7/cordova-plugin-firebase-messaging

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

53 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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 in FOREGROUND.

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

onBackgroundMessage(callback)

Called when a push message received in FOREGROUND.

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

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • Objective-C 57.3%
  • Java 36.5%
  • JavaScript 6.2%