【问题标题】:Check for new notifications on Firebase Cloud Messaging. (FCM)检查 Firebase 云消息传递的新通知。 (FCM)
【发布时间】:2019-06-19 07:35:23
【问题描述】:

如果完全连接到 Firebase 云消息传递,我正在寻找一种阅读或收听更改的方式。通知会发送到 firebase 上的 Android 项目,因此应该检查 Android 应用程序是否有新通知。

我应该使用下面的代码吗?

@Override
public void onMessageReceived(RemoteMessage remoteMessage) {

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

我尝试遵循文档Firebase cloud messaging。但是在查看 FCM 上的新通知时遇到了挑战。

【问题讨论】:

    标签: java android firebase firebase-cloud-messaging


    【解决方案1】:

    如果您的意思是列出所有来自 Android 的通知,我相信您不能这样做。反之亦然:Firebase 会通过 onMessageReceived 方法告诉您某个特定客户端何时可以使用新通知。如果您需要额外的逻辑,您可能希望在触发时使用后端存储通知,然后 Android 端可以根据其唯一 ID 将新通知与后端匹配以执行进一步操作,即确保用户收到通知、统计信息等.

    【讨论】:

    • 嘿,感谢您的回答,但我的意思是每次在云中可用时检查一条新消息。
    【解决方案2】:

    您可以检查通知是否被触发

    @Override
        public void onMessageReceived(RemoteMessage remoteMessage) {
            Log.e(TAG, "From: " + remoteMessage.getFrom());
            Log.e(TAG, "Data Payload: " + remoteMessage.getData().toString());
    
            if (remoteMessage.getNotification() == null)
                return;
            // Check if message contains a notification payload.
            if(remoteMessage.getNotification() != null) {
                handleNotification(remoteMessage, remoteMessage.getNotification().getBody(), remoteMessage.getNotification().getTitle());
            }
    

    【讨论】:

    • @Idris 用于检查推送是否被触发,仅检查 remoteMessage.getNotification() != null。
    • 是的,我想检查通知是否被触发,这就是我想要的
    • 实际上我在 FCM 上推送消息后尝试登录 remoteMessage.getNotification(),但没有登录。但是当我添加通知时,它起作用了,是什么原因造成的?
    • 我的另一个问题是,如果我先发送第一条消息然后再发送第二条消息,该声明会继续通知if(remoteMessage.getNotification() != null) { handleNotification(remoteMessage, remoteMessage.getNotification().getBody(), remoteMessage.getNotification().getTitle()); } 有变化吗?
    猜你喜欢
    • 1970-01-01
    • 2022-06-10
    • 2022-01-09
    • 2021-01-19
    • 2017-12-14
    • 1970-01-01
    • 2016-12-25
    • 2019-12-16
    • 1970-01-01
    相关资源
    最近更新 更多