![](/img/trans.png)
[英]Chat App for Android using a XMPP Server and Google Cloud Messaging (or the newer Firebase Cloud Messaging) for Push Notifications
[英]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
}
我可以写这样的代码吗?
您可以像这样检索当前令牌:
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.