【问题标题】:Firebase notifications: activity doesn't launch when status bar notification is clickedFirebase 通知:单击状态栏通知时,活动不会启动
【发布时间】:2017-05-31 03:05:46
【问题描述】:

我目前遇到以下问题:

  1. 我已经实现了自定义 FirebaseMessagingService 并且 onMessageReceived() 方法被覆盖。此外,当应用程序处于后台时,我会从 getExtras() 获取捆绑包。
  2. 我需要通知内容才能将其保存在本地数据库中。

会发生什么:

  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,已经完成了!

标签: android firebase android-notifications firebase-cloud-messaging


【解决方案1】:

我将假设您在活动的 onCreate() 方法中有您的启动器活动代码。一旦创建了活动并且您单击了另一个通知,将不会再次调用 onCreate()。

要更新用户已经可见的 Activity,您需要做的是覆盖显示数据的 Activity 的 onNewIntent(Intent intent) 方法并在那里更新您的视图。

【讨论】:

  • 我已经尝试过您的解决方案。不幸的是,没有调用 onNewIntent(Intent intent) 方法(在 MainActivity 中,这是当前可见的活动)并且没有发生任何事情。我是否必须在 AndroidManifest.xml 中为此活动设置特殊标志?
  • 我没有从 Firebase 控制台发送消息的经验(我们仍在使用 GCM),但是如果您可以通过单击打开应用程序,afaik 通知始终具有 PendingIntent 作为 ContentIntent。如果应用程序已经在前台,当使用启动模式 singleTop(通过清单或 IntentFlag)启动时,这将在 onNewIntent 结束,否则在当前任务堆栈上启动一个新 Activity。您能否提供有关您的启动模式的更多信息?
  • 好的,你刚才复制粘贴的getIntent函数是什么?您是否在 Activity 中覆盖了它?
  • 我越来越糊涂了,这和问题有什么关系?
  • 算了,没关系。
猜你喜欢
  • 2015-04-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多