2017-03-20 33 views
0

我正在使用FCM推送通知。我通過意圖在點擊通知時啓動新的活動。當應用程序處於前臺時,應用程序可以正常工作並意圖啓動新活動,但當應用程序處於後臺時,它不會啓動新活動,而是啓動默認活動的實例。當應用程序在後臺時FCM不工作的意圖(android)

public class MyFirebaseMessagingService extends FirebaseMessagingService { 

private static final String TAG = "MyFirebaseMsgService"; 

@Override 
public void onMessageReceived(RemoteMessage remoteMessage) { 
    //Displaying data in log 
    //It is optional 







    Log.d(TAG, "From: " + remoteMessage.getFrom()); 
    Log.d(TAG, "Notification Message Body: " + remoteMessage.getNotification().getBody()); 

    //Calling method to generate notification 
    sendNotification(remoteMessage.getNotification().getBody()); 
} 
private void sendNotification(String messageBody) { 
    Intent intent = new Intent(this, SecActivity.class); 
    intent.putExtra("started_from","notification"); 
    intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); 

    PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, 
      PendingIntent.FLAG_ONE_SHOT); 

    Uri defaultSoundUri= RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION); 
    NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this) 
      .setSmallIcon(R.mipmap.ic_launcher) 
      .setContentTitle("Firebase Push Notification") 
      .setContentText(messageBody) 
      .setAutoCancel(true) 
      .setSound(defaultSoundUri) 
      .setContentIntent(pendingIntent); 

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

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

}

+0

你應該來這個線程的解決方案[this](http://stackoverflow.com/questions/42976152/how-to-auto-launch-android-app-when-you-receive-notification-fcm/ 42980848#42980848) –

回答

0

希望您嘗試收到消息時啓動在MainActivity。當應用程序從後臺恢復時,您當前的活動正在清除。 來自FLAG_ACTIVITY_CLEAR_TOP的文檔: 如果已設置,並且正在啓動的活動已在當前任務中運行,則不是啓動該活動的新實例,而是關閉其上的所有其他活動,並且此意圖將作爲新的Intent傳遞給(現在處於頂端的)舊活動。

嘗試清除此標誌。

0

我也有同樣的問題,但我設法把它與此修復程序,

在清單中提到的默認活動在OnCreate

if (bundle != null) { 
    if ((String) bundle.get("tag") != null) { 
     String tag = (String) bundle.get("tag"); 
     if (tag.equals("abc")) { 
      Intent intent = new Intent(SplashActivity.this, MessageDetailsActivity.class); 
      startActivity(intent); 
     } else if (tag.equals("def")) { 
      openSpecificActivity(tag, (String) bundle.get("id")); 
     } 
    } else { 
     Intent i = new Intent(SplashActivity.this, HomeActivity.class); 
     i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); 
     startActivity(i); 
    } 
} 
+0

如果在點擊通知後condition.bundle爲空,則不會首先進入。 –

+0

只有捆綁包具有標籤標題和數據時,它纔是來自fcm的有效通知。這將在將這些值添加到推送時起作用 – g7pro

0

使用該這樣做:

intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP); 
0

我得到了一個解決方案。 只是將下面的代碼放在啓動器活動的創建方法中。

if (bundle != null) { 
     String value = bundle.getString("key"); 
     if (value != null) { 

      startActivity(new Intent(MainActivity.this, secActivity.class)); 
     } 
} 

當應用程序處於後臺或殺死,FCM不會叫onmessagerecieved方法,但它將數據發送到系統托盤以顯示notification.so datapayload(從FCM控制檯發送)不會被onmessagerecieved方法來處理。當用戶點擊通知時,它會啓動應用程序的默認活動,並且datapayload將被意圖傳遞。因此,通過改變啓動器活動的oncreate方法(如上所述),即使應用程序處於後臺或被殺死,我們也可以獲得數據加載。由fcm控制檯發送)。當app處於前臺數據加載中時,將通過fcm服務的onmessagerecieved方法進行處理。

0

基於Antinio的回答

https://stackoverflow.com/a/37845174/4454119

這究竟是爲什麼?

有在FCM(火力地堡雲消息)兩種類型的消息:

顯示的消息:這些消息觸發onMessageReceived()回調,只有當你的應用程序是在前臺

數據 - 消息:即使您的應用處於前臺/後臺/終止狀態,這些消息也會觸發onMessageReceived()回調 Firebase團隊尚未開發用於將數據消息發送到設備的UI。

所以你需要使用數據消息..

0

在FCM你,當你想FCM處理顯示代表您的客戶端應用程序的通知有兩種類型的消息

  • 通知消息
  • 數據消息

使用的通知消息。當您想要處理客戶端應用程序中的消息時使用數據消息。

如果您在將消息發送到系統托盤之前需要處理消息,最好使用數據消息,對於這些類型的消息,在進入系統托盤之前,回調首先會到達onMessageRecieved方法。

相關問題