【问题标题】:FCM Push notifications display raw json even after implementing dats messagesFCM 推送通知即使在实现 dat 消息后也会显示原始 json
【发布时间】:2018-11-30 07:18:05
【问题描述】:

以下是我尝试从 firebase 控制台发送的 json

{
    "message": {
        "token": "bk3RNwTe3H0:CI2k_HHwgIpoDKCIZvvDMExUdFQ3P1...",
        "data": {
            "Nick": "Mario",
            "body": "great match!",
            "Room": "PortugalVSDenmark"
        }
    }
}

我在onMessageReceived中的代码如下

    @Override
    public void onMessageReceived(RemoteMessage remoteMessage) {

        Log.d(TAG, "From: " + remoteMessage.getFrom());

        Log.d(TAG, "Message data payload: " + remoteMessage.getData());

        // Check if message contains a data payload.
        if (remoteMessage.getData().size() > 0) {
            Log.d(TAG, "Message data payload: " + remoteMessage.getData());

            String name = remoteMessage.getData().get("Nick").toString();
            String body = remoteMessage.getData().get("body").toString();
            String room = remoteMessage.getData().get("Room").toString();

            String messageBody = name
                    + " "
                    + body
                    + " "
                    + room;

            NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
                    .setContentTitle("Zone")
                    .setContentText(messageBody)
                    .setAutoCancel(true)
                    .setSmallIcon(R.mipmap.ic_launcher)
                    /*Notification with Image*/;
            NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
            notificationManager.notify(0 /* ID of notification */, notificationBuilder.build());
        }
    }

每次我从 firebase 控制台发送上述 json 时,它甚至都不会进入 if 条件,因为 remoteMessage.getData().size() 不大于零。

虽然我正在发送数据负载,但为什么会发生这种情况?

另外,当收到通知时,我想从原始 json 创建一个句子并显示它。例如“尼克喜欢一张照片”

当通知通过时,只显示 json。

为此苦苦挣扎了很久。

任何帮助将不胜感激。谢谢

【问题讨论】:

标签: android firebase push-notification firebase-cloud-messaging android-push-notification


【解决方案1】:

试试这个。

String nick,body,room, fullString;

if(!remoteMessage.getData().isEmpty()){
        Log.d(TAG, "Notification Message Data: " + remoteMessage.getData());
        nick=remoteMessage.getData().get("Nick");
        body=remoteMessage.getData().get("body");
        room=remoteMessage.getData().get("Room");
        Log.d(TAG, "All data: " + nick+","+body+","+room);
        fullString = "Anything you want"+nick+"anything";
        //then use the string anywhere
    }

【讨论】:

    猜你喜欢
    • 2017-03-16
    • 1970-01-01
    • 2017-02-23
    • 2016-12-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-05-07
    相关资源
    最近更新 更多