简体   繁体   English

Firebase通知:单击状态栏通知时,活动不会启动

[英]Firebase notifications: activity doesn't launch when status bar notification is clicked

I'm currently experiencing the following problem: 我目前遇到以下问题:

  1. I've implemented custom FirebaseMessagingService and the method onMessageReceived() is overriden. 我已经实现了自定义FirebaseMessagingService,并且覆盖了onMessageReceived()方法。 Also when the app is in background i get the bundle from getExtras(). 此外,当应用程序在后台时,我从getExtras()获取包。
  2. I need the notification content in order to save it locally in db. 我需要通知内容才能在db中本地保存。

What happens: 怎么了:

  1. Send 3 notifications from Firebase console when the app is in background 当应用在后台时,从Firebase控制台发送3个通知
  2. 3 status bar notifications are created. 创建3个状态栏通知。
  3. Click on one of them -> launcher activity is opened and the content from the notification is saved. 单击其中一个 - >打开启动器活动,并保存通知中的内容。
  4. Click on the other status bar notifications (when the app is still in foreground) -> nothing happens... 单击其他状态栏通知(当应用仍处于前台时) - >没有任何反应...

Could you please help? 能否请你帮忙?

Launcher Activity code: 启动器活动代码:

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

Custom FirebaseMessagingService code: 自定义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);
    }
}

I'm going to assume you have your launcher activity code in the onCreate() method of your activity. 我将假设您在活动的onCreate()方法中拥有启动器活动代码。 Once the activity is created and you click another notification the onCreate() will not be called again. 创建活动后,如果单击另一个通知,则不会再次调用onCreate()。

What you need to do to update the activity that is already visible to the user is override the onNewIntent(Intent intent) method of the activity in which the data is displayed and update your views there. 更新用户已经看到的活动需要做的是覆盖显示数据的活动的onNewIntent(Intent intent)方法,并在那里更新您的视图。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM