remoteMessage.getMessageType在onMessageReceived返回null

问题描述 投票:2回答:5
public class FirebaseMessagingService extends com.google.firebase.messaging.FirebaseMessagingService{

    String TAG = "FirebaseMessagingService";
    @Override
    public void onMessageReceived(RemoteMessage remoteMessage){

        Log.i(TAG," input string :"+ String.valueOf(remoteMessage.getData()));
        /**
         * Parse remote input and pass id, action, frequency and payload to eventHandler.
         * **/

        String messageType = remoteMessage.getMessageType();// messageType is null always
        if(messageType.equalsIgnoreCase("Data")){
            Log.i(TAG, " Data notification received");
        }
        else if(messageType.equalsIgnoreCase("Notification")){
            Log.i(TAG, " Push notification received");
        }
}
android firebase firebase-cloud-messaging
5个回答
1
投票

我不知道什么getMessageType()应该返回。但它似乎不太可能,它返回要么DataNotification,因为a single message can contain both notification and data information


0
投票

谢谢。其实,我想出了一个办法来检查的消息类型本身。以下为我工作,而无需使用的getMessage类型。

地图数据= remoteMessage.getData(); RemoteMessage.Notification通知= remoteMessage.getNotification();

    if (data.isEmpty()) { // message type is notification.
        parseDataMessage(remoteMessage);
    } else { // message type is data.
        parseNotificationMessage(remoteMessage);
    }

0
投票

从文档看起来它是你可以发送消息和使用SDK here时设置的参数。

public RemoteMessage.Builder setMessageType (String messageType)

然而,发送使用火力地堡REST API消息时,我还没有看到指定消息类型的能力。


0
投票

你应该尝试像这样...

  if(getIntent().getExtras() != null && getIntent().getExtras().get("your_data_key") != null) {
 String strNotificaiton = getIntent().getExtras().get("your_data_key").toString(); }

0
投票

使用此代码onMessageReceived它为我工作:

@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
    Log.e(TAG, "From: " + remoteMessage.getFrom());

        if (remoteMessage == null)
        return;

    //if message contains a notification payload.
        if (remoteMessage.getNotification() != null) {
            Log.e(TAG, "Notification Body: " + remoteMessage.getNotification().getBody());
            handleNotification(remoteMessage.getNotification().getBody());
        }

    //if message contains a data payload.
        if (remoteMessage.getData().size() > 0) {
            Log.e(TAG, "Data Payload: " + remoteMessage.getData().toString());

            try {
                JSONObject json = new JSONObject(remoteMessage.getData().toString());
                handleDataMessage(json);
            } catch (Exception e) {
                Log.e(TAG, "Exception: " + e.getMessage());
            }
        }
}
© www.soinside.com 2019 - 2024. All rights reserved.