![](/img/trans.png)
[英]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.