使用Firebase管理员SDK无法在FCM中获取数据消息

时间:2019-06-18 18:46:20

标签: android firebase firebase-cloud-messaging

我已经在NodeJS中实现了firebase admin 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());
  }
}

有人让我知道如何在通知中获取数据。任何帮助将不胜感激。

谢谢

1 个答案:

答案 0 :(得分:2)

根据“在Android应用中接收消息”部分中的Handling messages

  

在后台接收到的同时具有通知和数据有效负载的消息。在这种情况下,通知将传递到设备的系统托盘中,,数据有效载荷将在启动器活动的意图之外传递

现在,根据Notification messages with optional data payload

  

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

  • 在后台,应用程序会在通知托盘中接收通知有效载荷,,并且仅在用户点击通知时处理数据有效载荷
  • 在前台时,您的应用会收到一个两个有效负载均可用的消息对象

因此,在应用处于后台运行期间,notification中的payload 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
}