cordova-plugin-firebase-messaging-ka

Cordova plugin for Firebase Messaging

Usage no npm install needed!

<script type="module">
  import cordovaPluginFirebaseMessagingKa from 'https://cdn.skypack.dev/cordova-plugin-firebase-messaging-ka';
</script>

README

cordova-plugin-firebase-messaging
NPM version NPM downloads

Cordova plugin for Firebase Cloud Messaging

Installation

cordova plugin add cordova-plugin-firebase-messaging --save

You can also specify variables ANDROID_NOTIFICATION_ICON and ANDROID_NOTIFICATION_COLOR.

Supported Platforms

  • iOS
  • Android

Methods

onMessage(callback)

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.

onTokenRefresh(callback)

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

Subscribe to topic in background.

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

unsubscribe(topic)

Unsubscribe from topic in background.

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

getBadge(callback)

Reads current badge number (if supported).

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

setBadge(value)

Sets current badge number (if supported).

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

requestPermission (iOS only)

Grant permission to recieve push notifications (will trigger prompt).

window.cordova.plugins.firebase.messaging.requestPermission(function(token) {
    console.log("APNS device token: ", token);
});