Firebase通知:当状态栏通知被点击时,活动不会启动

3
我目前遇到了以下问题:
  1. 我实现了自定义的FirebaseMessagingService,重写了onMessageReceived()方法。当应用程序在后台运行时,我从getExtras()获取Bundle。
  2. 为了将其保存到本地db中,我需要通知内容。

发生了什么:

  1. 当应用程序在后台运行时,从Firebase控制台发送3个通知。
  2. 创建3个状态栏通知。
  3. 点击其中一个 -> 打开启动器活动,并保存来自通知的内容。
  4. 单击其他状态栏通知(当应用程序仍在前台运行时)-> 什么也不会发生...

你能帮忙吗?

启动器活动代码:

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));
    }

自定义的FirebaseMessagingService代码:

@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);
    }
}

请发布您的代码。 - Ameer
@Ameer,已经完成! - Stefan Tsekov
1个回答

0
我假设你在onCreate()方法中已经编写了启动器活动代码。一旦活动被创建并且你点击另一个通知,onCreate()方法将不会再次被调用。
为了更新已经向用户显示的活动,你需要重写包含数据展示的活动的onNewIntent(Intent intent)方法,并在那里更新你的视图。

我已经尝试了你的解决方案。不幸的是,在当前可见的活动MainActivity中,onNewIntent(Intent intent)方法没有被调用,什么也没发生。我需要在AndroidManifest.xml中为这个活动设置特殊标志吗? - Stefan Tsekov
我没有使用Firebase控制台发送消息的经验(我们仍在使用GCM),但据我所知,如果您可以通过单击它们打开应用程序,则通知始终具有PendingIntent作为ContentIntent。如果应用程序已在前台,则在使用launch mode singleTop(通过清单或IntentFlag)启动时,这将最终进入onNewIntent中,否则会在当前任务堆栈上启动新的Activity。您能提供更多关于启动模式的信息吗? - Sander
好的,你刚才复制粘贴的getIntent函数是什么?你在Activity中重写过它吗? - Sander
我越来越困惑了,这与问题有什么关系? - Sander
忘了吧,这与编程无关。 - Stefan Tsekov
你能否更新你原来的问题,提供更多关于任务栈使用的信息?实现方式很大程度上取决于你在那里尝试实现什么。同时,你能否更新"launcher activity code"并展示你已经实现了哪些地方? - Sander

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接