remoteMessage.getMessageType在onMessageReceived中返回null

时间:2016-07-15 21:29:26

标签: android firebase firebase-cloud-messaging

color

5 个答案:

答案 0 :(得分:1)

我不确定getMessageType()应该返回什么。但由于a single message can contain both notification and data information,它似乎不太可能返回DataNotification

答案 1 :(得分:0)

感谢。 我实际上想出了一种方法来检查消息类型本身。以下适用于我而不使用getMessageType。

Map data = remoteMessage.getData();         RemoteMessage.Notification notification = remoteMessage.getNotification();

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

答案 2 :(得分:0)

从文档中看,它是您在发送邮件和使用SDK here时可以设置的参数。

  

matchdata('bzl', ['BraZiL', 'LiZarB', 'BraZiL']); // [ ['brazil', 0], ['brazil', 2] ]

但是,使用Firebase REST API发送邮件时,我还没有看到指定邮件类型的功能。

答案 3 :(得分:0)

你应该尝试这样......

public static void main(String s[]){
     String test = "{key: TVREG, value:WestAfrica Ltd | VAT No: 1009034324829/{834324}<br />Plot No. 56634773, Road};{key: REGISTRATION, value:SouthAfricaLtd | VAT No: 1009034324829/{834324}<br />Plot No. 56634773, Road}";
         StringBuilder sb= new StringBuilder();
                   String[] keyValOld = test.split(";");

                   for(int j=0; j<keyValOld.length; j++){
                       String keyVal = keyValOld[j].substring(1,keyValOld[j].length()-1);

                       String[] parts = keyVal.split("(:)|(,)",4);
                       sb.append("{");
                       for (int i = 0; i < parts.length; i += 2) { 
                           sb.append("\""+parts[i].trim()+"\": \""+parts[i + 1].trim()+"\"");
                           if(i+2<parts.length) sb.append(", ");
                   }
                       sb.append("};");
                   }
                   System.out.println(sb.toString());
}

答案 4 :(得分: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());
            }
        }
}