簡體   English   中英

Firebase通知:單擊狀態欄通知時,活動不會啟動

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

我目前遇到以下問題:

  1. 我已經實現了自定義FirebaseMessagingService,並且覆蓋了onMessageReceived()方法。 此外,當應用程序在后台時,我從getExtras()獲取包。
  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);
    }
}

我將假設您在活動的onCreate()方法中擁有啟動器活動代碼。 創建活動后,如果單擊另一個通知,則不會再次調用onCreate()。

更新用戶已經看到的活動需要做的是覆蓋顯示數據的活動的onNewIntent(Intent intent)方法,並在那里更新您的視圖。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM