【问题标题】:Display firebase notification data-message on Android tray在 Android 托盘上显示 Firebase 通知数据消息
【发布时间】:2017-08-21 19:52:03
【问题描述】:

我需要使用 Firebase 的不可折叠通知。为此,我正在使用这样的数据消息:

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

此消息在 onMessageReceived() 方法中进行解释。 我仍然想在托盘中显示此数据消息,就像系统自动显示通知消息一样。

如何实现这一目标?我找不到这方面的文档。

谢谢!

【问题讨论】:

  • 它是json 所以解析它

标签: android firebase firebase-cloud-messaging


【解决方案1】:

您可以通过调用RemoteMessage.getData(),然后调用.get("<KEY_HERE>"),从onMessageReceived() 中的数据负载中检索值。像这样:

public void onMessageReceived(RemoteMessage remoteMessage) {
        super.onMessageReceived(remoteMessage);
        Log.d(TAG, "onMessageReceived()");

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

            String dataTitle = remoteMessage.getData().get("data_title");
            String dataBody = remoteMessage.getData().get("data_body");

            sendNotification(dataTitle, dataBody);
}

然后你必须自己build and display the Notification。像这样:

private void sendNotification(String title, String body) {
        Notification notif = new NotificationCompat.Builder(this)
                .setSmallIcon(R.mipmap.ic_launcher)
                .setContentTitle(title)
                .setContentText(body)
                .build();

        NotificationManagerCompat notificationManager = NotificationManagerCompat.from(this);
        notificationManager.notify(0, notif);
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-05-18
    • 1970-01-01
    • 2017-09-29
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多