2016-12-06 5 views
2

アプリがバックグラウンドであっても通知が届いていますが、通知が届くたびに自動的にアクティビティが開きます(ユーザーが通知をクリックしなくても)。FCM Androidのデータメッセージ

マイonMessageReceived

@Override 
public void onMessageReceived(RemoteMessage remoteMessage) { 
    Bundle b = new Bundle(); 
    if (data.containsKey("click_action")) { 
     if(data.containsValue("postlike") || data.containsValue("comment") || data.containsValue("newpost")){ 
      b.putString("PostId", data.get("post_id").toString()); 
      Firebase_Action_Click.startActivity(data.get("click_action"), b, this); 
     } else { 
      Intent intent = new Intent(this, ActivityMain.class); 
      intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); 
      pendingIntent = PendingIntent.getActivity(this, 0, intent,PendingIntent.FLAG_ONE_SHOT); 
     } 
    } 

    NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this) 
     .setSmallIcon(R.drawable.notification_icon) 
     .setContentText(data.get("body")) 
     .setAutoCancel(true) 
     .setSound(defaultSoundUri) 
     .setContentIntent(pendingIntent); 

    NotificationManager notificationManager = 
       (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); 

    notificationManager.notify(0, notificationBuilder.build()); 
} 

であると私Firebase_Action_Click

public static void startActivity(String className, Bundle extras, Context context) { 
    Class cls = null; 
    try { 
     cls = Class.forName(className); 
    } catch(ClassNotFoundException e){ 
     //means you made a wrong input in firebase console 
    } 

    Intent i = new Intent(context, cls); 
    i.putExtras(extras); 
    i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 
    context.startActivity(i); 
} 

そして、私のランチャー活動の中で、私はこれらの行を追加した、

@Override 
protected void onNewIntent(Intent intent) { 
    super.onNewIntent(intent); 
    if (intent.hasExtra("click_action")) { 
     Firebase_Action_Click.startActivity(intent.getStringExtra("click_action"), intent.getExtras(), this); 
    } 
} 

すべてがうまく行くが、唯一の問題私が直面しているのは、アプリがバックグラウンドにあり、通知があったとしてもアプリケーションを自動的に開きます。

ユーザーが通知をクリックしたときにのみ、同じ操作を実行する必要があります。

誰もがこの上で私を助けてもらえ、事前

+0

この行は何をすべきですか? Firebase_Action_Click.startActivity(data.get( "click_action")、b、this); – Mushirih

答えて

0

機能からActivityを起動するため、Activityが起動します。したがって、通知をクリックしたときにActivityを起動する場合は、PendingIntentとしてActivityを開始する必要があります。

私はあなたのコードで簡単な修正をお勧めします。 startActivityFirebase_Action_Clickには、作成した外観のIntentが返されます。

public static Intent startActivity(String className, Bundle extras, Context context) { 
    Class cls = null; 
    try { 
     cls = Class.forName(className); 
    } catch(ClassNotFoundException e){ 
     //means you made a wrong input in firebase console 
    } 

    Intent i = new Intent(context, cls); 
    i.putExtras(extras); 
    i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 

    return i; // Return the intent from here. 
} 

は、今すぐあなたのonMessageReceived機能で、あなたはif(data.containsValue("postlike") || data.containsValue("comment") || data.containsValue("newpost"))の内側にすぎPendingIntentを設定する必要があります - このif文を、あなたはelse一部に設定されたもののよう。だからあなたのコードはこのように見えるかもしれません。

@Override 
public void onMessageReceived(RemoteMessage remoteMessage) { 
    Bundle b = new Bundle(); 

    if (data.containsKey("click_action")) { 
     if(data.containsValue("postlike") || data.containsValue("comment") || data.containsValue("newpost")) { 
      b.putString("PostId", data.get("post_id").toString()); 
      Intent intent = Firebase_Action_Click.startActivity(data.get("click_action"), b, this); 

      // Now add the flags and create pendingIntent here 
      intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); 
      pendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_ONE_SHOT); 

     } else { 
      Intent intent = new Intent(this, ActivityMain.class); 
      intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); 
      pendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_ONE_SHOT); 
     } 
    } 

    NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this) 
     .setSmallIcon(R.drawable.notification_icon) 
     .setContentText(data.get("body")) 
     .setAutoCancel(true) 
     .setSound(defaultSoundUri) 
     .setContentIntent(pendingIntent); 

    NotificationManager notificationManager = 
       (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); 

    notificationManager.notify(0, notificationBuilder.build()); 
} 

通知システムは正常に動作するはずです。

0

最も簡単なので、のajustar 0フラグFLAG_ACTIVITY_NEW_TASK SE O metodo onMessageReceived estiver dentroデ・UMAクラッセのqueのnaoさんのsejaの活動に感謝:D〜

ませんmetodo受容体:

@Override 
public void onMessageReceived(RemoteMessage remoteMessage) { 
    Intent intent = new Intent(); 
    intent.setAction(Intent.ACTION_MAIN); 
    intent.addCategory(Intent.CATEGORY_LAUNCHER); 
    intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 
    ComponentName componenteName = new ComponentName(this, YourActivity.class); 
    intent.setComponent(componenteName); 
    startActivity(intent); 
} 

ませSEUのマニフェストません:

<activity android:name=".YourActivity" /> 
+1

英語をお願いします。 –

関連する問題