Unable to get data message in FCM using firebase a

2019-08-13 16:38发布

问题:

I have implemented firebase admin SDK in NodeJS and trying to send push notification from server.My problem is I am not getting data part in my notification(account and balance).It is only showing notification part(title and body).

I have implemented server side code something like this:

const admin = require("firebase-admin");
const serviceAccount = require("./my_service_account.json");

admin.initializeApp({

credential: admin.credential.cert(serviceAccount),
databaseURL: "https://<myapp name>.firebaseio.com"
});

var payload = {
          notification: {
            title: "Hello",
            body: "How are you."
          },  
          data: {
            account: "Savings",
            balance: "$3020.25"
          } 
        };

 admin.messaging().sendToDevice(registrationToken,payload)
 .then((response) =>{

                         console.log("Response", response); 

                    }).catch((error) => {

                         console.log("Error", error);
                    });  

On client side I am doing like this below:

public class MessagingReceive extends FirebaseMessagingService {

@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
    super.onMessageReceived(remoteMessage);

if(remoteMessage.getData().size() > 0){

        Map<String, String> data = remoteMessage.getData();

        handleData(data);
    }
  }

private void handleData(Map<String,String> data){

       String title = data.get("account");
       String body = data.get("balance");

    Intent intent = new Intent(MessagingReceive.this,MainActivity.class);
    intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
    PendingIntent pendingIntent = PendingIntent.getActivity(this,0,intent,PendingIntent.FLAG_ONE_SHOT);

    NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this);
    notificationBuilder.setContentTitle(title);
    notificationBuilder.setContentText(body);
    notificationBuilder.setAutoCancel(true);
    notificationBuilder.setSmallIcon(R.mipmap.ic_launcher);
    notificationBuilder.setContentIntent(pendingIntent);

    NotificationManager notificationManager = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
    notificationManager.notify(0,notificationBuilder.build());
  }
}

Someone let me know how can I get data part in my notification. Any help would be appreciated.

THANKS

回答1:

According to Handling messages in Receive messages in an Android app section

Messages with both notification and data payload, when received in the background. In this case, the notification is delivered to the device’s system tray, and the data payload is delivered in the extras of the intent of your launcher Activity.

Now , according to Notification messages with optional data payload

App behavior when receiving messages that include both notification and data payloads depends on whether the app is in the background or the foreground essentially, whether or not it is active at the time of receipt.

  • When in the background, apps receive the notification payload in the notification tray, and only handle the data payload when the user taps on the notification.
  • When in the foreground, your app receives a message object with both payloads available.

so during the time the app is in the background , the notification obj in your payload is available in order to build a standart notification (title,body).

to get the payload data object when the user taps on the notification for example is implemented by intent .

@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
super.onMessageReceived(remoteMessage);
String account;
String balance;
String body = remoteMessage.getNotification().getBody();
String title = remoteMessage.getNotification().getTitle();
if(remoteMessage.getData().size() > 0){
    Map<String, String> data = remoteMessage.getData();
        account = data.get("account");
        balance = data.get("balance");
 }
  handleData(title,body,account,balance);
}

private void(String body , String title , Stirng account , String balance){

Intent intent = new Intent(MessagingReceive.this,MainActivity.class);
intent.putExtra("account",account);
intent.putExtra("balance",balance);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent pendingIntent = 
PendingIntent.getActivity(this,0,intent,PendingIntent.FLAG_ONE_SHOT);

    // rest of your code
}