[英]Where is FCM Intent with data delivered, when app is in background?
[英]intent with FCM not working when app is in background(android)
我正在使用FCM推送通知。 單擊通知時,我正在傳遞啟動新活動的意圖。當應用程序處於前台時,應用程序可以正常運行,並且意圖啟動新活動,但是當應用程序處於后台時,它不會啟動新活動,而是啟動默認活動的實例。
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());
}
}
希望您在收到消息后嘗試啟動mainactivity。 當應用程序從后台恢復時,您當前的活動將被清除。 從FLAG_ACTIVITY_CLEAR_TOP的文檔中:如果已設置,並且正在啟動的活動已在當前任務中運行,則與其啟動該活動的新實例,不關閉該活動之上的所有其他活動,並且此Intent將為作為新的Intent交付給(現在最重要的)舊活動。
嘗試刪除此標志。
我也有同樣的問題,但是我設法解決了這個問題,
在清單中提到的默認活動中,在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);
}
}
用這個:
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
我有一個解決方案。 只需將以下代碼放在啟動器活動的oncreate方法中即可。
if (bundle != null) {
String value = bundle.getString("key");
if (value != null) {
startActivity(new Intent(MainActivity.this, secActivity.class));
}
}
當應用程序在后台或被殺死時,FCM不會調用onmessagerecieved方法,但會將數據發送到系統托盤以顯示通知。因此datapayload(從fcm控制台發送)不會由onmessagerecieved方法處理。當用戶單擊通知時,它將將啟動應用程序的默認活動,並通過意圖傳遞datapayload。因此,對啟動器活動的oncreate方法進行更改(如上所述),即使應用程序處於后台或終止狀態,我們也可以獲取datapayload(ex密鑰由fcm控制台發送)。當應用程序處於前台數據有效負載時,將通過fcm服務的onmessagerecieved方法處理。
根據Antinio的回答
https://stackoverflow.com/a/37845174/4454119
為什么會這樣呢?
FCM(Firebase雲消息傳遞)中有兩種消息類型:
display-messages:僅當您的應用程序位於前台時,這些消息才會觸發onMessageReceived()回調
數據消息:即使您的應用程序位於前台/后台/已被殺死的Firebase團隊中,這些消息也會觸發onMessageReceived()回調,但尚未開發出可將數據消息發送到您的設備的UI。
因此,您需要使用數據消息。
在FCM中,您有兩種類型的消息
當您希望FCM處理代表客戶端應用程序顯示的通知時,請使用通知消息。 當您要在客戶端應用程序中處理消息時,請使用數據消息。
如果需要在將消息發送到系統托盤之前對其進行處理,最好使用數據消息,因為對於這些類型的消息,回調首先到達onMessageRecieved方法,然后再進入系統托盤。
為您服務
"to": token,
"notification": {
"title": "Title,
"body": "Body"
},
"data" : {
"update": "yes"
}
在ANDROID KOTLIN中
val intent = Intent(this,MainActivity::class.java)
intent.putExtra("update","yes")
......
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.