3

をクリックしたときにアクティビティが起動しない:Firebase通知:ステータスバーの通知は、私は現在、次の問題が発生してい

  1. 私が実装したカスタムFirebaseMessagingService及び方法onMessageReceived()がオーバーライドされます。また、アプリケーションがバックグラウンドにあるとき、getExtras()からバンドルを取得します。
  2. ローカルでdbに保存するには通知コンテンツが必要です。

何が起こる:

  1. をアプリが
  2. 3ステータスバーの通知が作成された背景にあるときFirebaseコンソールから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); 
    } 
} 
+0

コードを投稿してください – Ameer

+0

@Ameer、already done! –

答えて

0

私はあなたがあなたの活動ののonCreate()メソッドでは、あなたのランチャーのアクティビティコードを持っていると仮定するつもりです。アクティビティが作成され、別の通知をクリックすると、onCreate()は再び呼び出されません。

ユーザーに既に表示されているアクティビティを更新するために必要な作業は、データが表示されているアクティビティのonNewIntent(Intent intent)メソッドをオーバーライドしてビューを更新することです。

+0

私はあなたのソリューションを試しました。残念ながら、onNewIntent(Intent intent)メソッドは呼び出されておらず(MainActivityは現在表示されているアクティビティ)、何も起こりませんでした。 AndroidManifest.xmlでこのアクティビティに特別なフラグを設定する必要がありますか? –

+0

私はFirebaseコンソールからメッセージを送信する経験はありませんが(まだGCMを使用しています)、afaik通知はContentIntentとしてPendingIntentを持っています。アプリケーションがすでにフォアグラウンドにある場合、起動モードsingleTop(マニフェストまたはIntentFlag経由)で起動したときにonNewIntentで終了し、それ以外の場合は現在のタスクスタックで新しいアクティビティを起動します。あなたの起動モードに関する詳細情報を提供できますか? – Sander

+0

AndroidManifest.xmlファイルに特定の起動モードを設定していませんでした... –

関連する問題