使用Firebase管理员SDK无法在FCM中获取数据消息 [英] Unable to get data message in FCM using firebase admin SDK

查看:131
本文介绍了使用Firebase管理员SDK无法在FCM中获取数据消息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经在NodeJS中实现了firebase admin SDK,并试图从服务器发送推送通知.我的问题是我的通知(账户和余额)中没有数据部分,只显示了通知部分(标题和正文).

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.

谢谢

推荐答案

根据

在后台接收到的同时具有通知和数据有效负载的消息.在这种情况下,通知会发送到设备的系统托盘中,,数据有效载荷会在启动器活动的意图之外的其他地方发送.

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.

现在,根据具有可选数据有效负载的通知消息

接收包含通知和数据有效载荷的消息时,应用程序的行为取决于该应用程序本质上是在后台还是在前台,在接收时是否处于活动状态.

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.
    • 因此,在应用程序处于后台运行期间,您可以使用payload中的notification obj来建立标准通知(标题,正文).

      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).

      例如当用户点击通知时通过intent实现payload data对象.

      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
      }
      

      这篇关于使用Firebase管理员SDK无法在FCM中获取数据消息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆