我目前遇到以下问题:
- 我已经实现了自定义FirebaseMessagingService,并且方法onMessageReceived()被覆盖了。此外,当应用程序在后台时,我从getExtras()获取捆绑包。
- 我需要通知内容才能将其本地保存在数据库中。
发生什么:
- 当应用处于后台时,从 Firebase 控制台发送 3 条通知
- 创建 3 个状态栏通知。
- 单击其中一个 ->启动器活动将打开并保存通知中的内容。
- 单击其他状态栏通知(当应用程序仍处于前台时)->没有任何反应...
你能帮忙吗?
启动器活动代码:
if (getIntent().getExtras() != null) {
Bundle extras = getIntent().getExtras();
String title = (String) extras.get(Constants.TOPIC_KEY_TITLE);
String imageUrl = (String) extras.get(Constants.TOPIC_KEY_IMAGE_URL);
String url = (String) extras.get(Constants.TOPIC_KEY_URL);
String description = (String) extras.get(Constants.TOPIC_KEY_DESCRIPTION);
Long sentTime = (Long) extras.get(Constants.TOPIC_KEY_SENT_TIME);
if (Util.isStringsNotNull(description)) {
News news = new News();
news.setTitle(title);
news.setMessage(description);
news.setDescription(description);
news.setImageUrl(imageUrl);
news.setUrl(url);
news.setDate(sentTime);
news.save();
EventBus.getDefault().post(new OnNewsUpdatedEvent(news));
AppPreferences.incrementUnseenNewsCount(this);
}
}
String action = getIntent().getAction();
if (Util.isStringNotNull(action) && action.equals(ACTION_SEARCH)) {
startActivity(MainActivity.getIntentActionSearch(this));
} else {
startActivity(MainActivity.getIntent(this));
}
自定义 Firebase 消息服务代码:
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
LogUtil.log(BASIC_TAG, "onMessageReceived called!");
String description = null;
String imageUrl = null;
String url = null;
String title = null;
Map<String, String> dataMap = remoteMessage.getData();
if (dataMap != null && !dataMap.isEmpty()) {
description = dataMap.get(Constants.TOPIC_KEY_DESCRIPTION);
imageUrl = dataMap.get(Constants.TOPIC_KEY_IMAGE_URL);
url = dataMap.get(Constants.TOPIC_KEY_URL);
title = dataMap.get(Constants.TOPIC_KEY_TITLE);
}
if (Util.isStringNotNull(description)) {
RemoteMessage.Notification notification = remoteMessage.getNotification();
News news = new News();
news.setDate(remoteMessage.getSentTime());
news.setTitle(Util.isStringNotNull(title) ? title : notification.getTitle());
news.setMessage(notification.getBody());
news.setDescription(description);
news.setImageUrl(imageUrl);
news.setUrl(url);
news.save();
EventBus.getDefault().post(new OnNewsUpdatedEvent(news));
AppPreferences.incrementUnseenNewsCount(this);
}
}
我假设您在活动的 onCreate() 方法中具有启动器活动代码。创建活动并单击另一个通知后,将不会再次调用 onCreate()。
要更新用户已可见的活动,您需要做的是覆盖显示数据的活动的 onNewIntent(Intent Intent) 方法,并在其中更新视图。