【发布时间】:2020-07-08 08:50:42
【问题描述】:
现在我可以从 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
}
我可以这样写代码吗?
【问题讨论】:
标签: android firebase firebase-cloud-messaging android-notifications