繁体   English   中英

Firebase 云消息推送和应用内通知

[英]Firebase Cloud Messaging Push and In-app notifications

现在我可以从 Firebase 控制台发送测试消息并在我的手机中收到推送通知。 我现在对生成应用内通知有一些疑问。 这是我目前的布局。

在此处输入图片说明

我也希望推送通知在我的应用中显示为应用内通知。 处理消息的唯一类是MyFirebaseMessagingService类包括notificationHelper帮助建立通知。 如何将消息信息从MyFirebaseMessagingService到我现在拥有的通知片段? 我是否需要将信息存储在本地文件中,然后从本地文件中检索信息以再次用于通知片段? 在这种情况下最好的方法是什么?

public class MyFirebaseMessagingService extends FirebaseMessagingService {

    @Override
    public void onMessageReceived(@NonNull RemoteMessage remoteMessage) {
        super.onMessageReceived(remoteMessage);

        if (remoteMessage.getNotification() != null) {
            String title = remoteMessage.getNotification().getTitle();
            String body = remoteMessage.getNotification().getBody();
            NotificationHelper.displayNotification(getApplicationContext(), title, body);
        }
    }
}

另一个微不足道的问题是关于 FCM 令牌问题。 我已经创建了一个 FCM 令牌。 如何让应用程序检查是否已生成令牌以防止每次启动应用程序时生成令牌?

if(instanceIdResult.getToken() == null)
{
   //generate token
}

我可以写这样的代码吗?

  1. 您可以使用房间数据库。 您保存所有通知,然后在片段中显示它们。 如果片段已经显示,您可以使用广播接收器立即发送和显示。 房间
  2. FCM 令牌创建一次。 在以下情况下,注册令牌可能会更改:
    • 应用程序删除实例 ID
    • 该应用程序已在新设备上恢复
    • 用户卸载/重新安装应用程序
    • 用户清除应用数据。

您可以像这样检索当前令牌:

FirebaseInstanceId.getInstance().getInstanceId()
    .addOnCompleteListener(new OnCompleteListener<InstanceIdResult>() {
        @Override
        public void onComplete(@NonNull Task<InstanceIdResult> task) {
            if (!task.isSuccessful()) {
                Log.w(TAG, "getInstanceId failed", task.getException());
                return;
            }

            String token = task.getResult().getToken();

        }
    });

您可以使用sharedpreferences来存储您收到的通知数量,当您的应用程序打开时会显示通知数量,如果用户阅读它们,则重置计数器。 你也可以存储令牌

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM