2016-12-06 75 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,因爲您從onMessageReceived函數啓動Activity。因此,如果您想在點擊通知時啓動Activity,則可能需要啓動Activity作爲PendingIntent

我想推薦在您的代碼中進行簡單的修改。 Firebase_Action_Click中的startActivity函數可能會返回它創建的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功能,您需要設置一個PendingIntent太裏面if(data.containsValue("postlike") || data.containsValue("comment") || data.containsValue("newpost")) - 這個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

Simples,所以ajustar O標誌FLAG_ACTIVITY_NEW_TASK SEØ方法方法onMessageReceived estiver迪登特魯德UMA CLASSE闕NAO seja活動:d〜

沒有方法方法受體:

@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

英文請。 –

相關問題