【问题标题】:Unable to get data message in FCM using firebase admin SDK无法使用 firebase admin SDK 在 FCM 中获取数据消息
【发布时间】:2019-06-18 18:46:20
【问题描述】:

我已经在 NodeJS 中实现了 firebase 管理 SDK 并尝试从服务器发送推送通知。我的问题是我没有在通知中获取数据部分(帐户和余额)。它只显示通知部分(标题和正文)。

我已经实现了类似这样的服务器端代码:

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);
                    });  

在客户端我这样做如下:

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());
  }
}

有人告诉我如何在通知中获取数据部分。任何帮助将不胜感激。

谢谢

【问题讨论】:

  • remoteMessage.getData().size() &gt; 0true 吗?
  • 我正在尝试使用 if(remoteMessage.getData().size() > 0){ Log.d("Data",remoteMessage.getNotification().getTitle()); }
  • 但是日志中没有任何价值。
  • Log.d("Data",remoteMessage.getData()); 看到了什么?
  • 我在 MessagingReceive 活动中获取数据,但我的启动器活动是 MainActivity,所以我如何才能在日志中获取数据。

标签: android firebase firebase-cloud-messaging


【解决方案1】:

根据Handling messages 在 Android 应用程序中接收消息部分

在后台接收时包含通知和数据负载的消息。在这种情况下,通知会发送到设备的系统托盘,数据负载会在启动器 Activity 的 Intent 的附加部分中发送

现在,根据Notification messages with optional data payload

接收包含通知和数据负载的消息时的应用行为取决于应用本质上是在后台还是前台,在接收时它是否处于活动状态。

  • 在后台时,应用会在通知托盘中接收通知负载,只有在用户点击通知时才会处理数据负载
  • 在前台时,您的应用会收到一个消息对象,其中包含两个可用的有效负载

所以在应用程序在后台运行期间,您的payload 中的notification obj 可用以构建标准通知(标题、正文)。

在用户点击通知时获取payload data 对象,例如由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
}

【讨论】:

  • 那么当我的应用程序处于后台状态或被杀死时,我该如何实现它。
  • OK.. 所以如果我想重定向到通知点击的某些活动,我需要在服务器端的有效负载中添加一些点击。
猜你喜欢
  • 2022-01-09
  • 1970-01-01
  • 1970-01-01
  • 2019-05-14
  • 1970-01-01
  • 2019-02-08
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多