Home > OS >  How to read FCM playload inside mobile device?
How to read FCM playload inside mobile device?

Time:12-08

I am creating mobile app in Cordova which receive notification and payload using cordova-plugin-firebase-messaging 7.0.4 "FirebaseMessagingPlugin" plugin to get device token successfully and send notification to specific device successfully.

What i have tried so far,in index.js file i added below code to get token,payload and badge.

document.addEventListener('deviceready', onDeviceReady, false);


   function onDeviceReady() {
   cordova.plugins.firebase.messaging.getToken().then(function(token) { 
   alert(token);
   document.getElementById('txtGCMId').value = token;
   
   });

  cordova.plugins.firebase.messaging.onMessage(function(payload) {
     alert(payload);
  });
 
  cordova.plugins.firebase.messaging.onBackgroundMessage(function (payload) {
    alert(payload);
  });

  cordova.plugins.firebase.messaging.getBadge().then(function(value) {
   alert(value);
  });

}

Using gettoken i can get token , but in onMessage() and onBackgroundMessage() i cant receive payload and also getBadge() i cant recive badge count which send in payload.

I am using Postman Agent to send notification using https://fcm.googleapis.com/fcm/send firebase rest api.

This is notification i send

{
"to": "e3PGidrDSqKP-pOHJ0W_yC:APA91bEGmKlVzYatnJam1GJ2k55fFJlMfT2uIymZnLQoFvYYoxBXKOMvaN26l1flFwDMbrQoLxWqCQEc0wWxIHRjEfUn7t13kzXPsFDXECsRVUTR1CLxynzg270H22jHaaqzJugFcGhS",
"notification": {
    "body": "Testing from Sample",
    "title": "Testing",
    "icon": "myicon",
    "sound": "default",
    "badge": "10"
},
"data" : {
    "key_1" : "Data for key one",
    "key_2" : "Hellowww"
}

}

So how to receive payload and badge and show badge count in app icon.

Note: My Cordova version 11.0.0 and following plugin i installed,

cordova-plugin-firebase-messaging 7.0.4 "FirebaseMessagingPlugin"
cordova-support-android-plugin 2.0.4 "cordova-support-android-plugin"

Any reply much appreciated.

Regards, Aravind

CodePudding user response:

anything passed in the notification will appear as a notification. Anything passed in the data, you can display as below

document.addEventListener('deviceready', onDeviceReady, false);

function onDeviceReady() {
   cordova.plugins.firebase.messaging.getToken().then(function(token) { 
   alert(token);
   document.getElementById('txtGCMId').value = token;
   //why print the above line? why not save that on the server in a database?
   });

  cordova.plugins.firebase.messaging.onMessage(function(payload) {
     
     alert(payload.key_1); //the value key_1, key_2 that you have set in your payload for the data array. this is the foreground payload action. When the app is open. You can either set a badge count or do what ever you want.

  });
 
  cordova.plugins.firebase.messaging.onBackgroundMessage(function (payload) {
    alert(payload.key_1) // this is the background notification event. When the user clicks on the notification, you would want to take the user to a specific page or perform some action. any thing set in data array is called by payload.<data_param>
  });

  cordova.plugins.firebase.messaging.getBadge().then(function(value) {
   alert(value);

  });

}
  • Related