[英]FCM Multiple push notifications not working properly when app in background state
我正在使用fcm进行推送通知,仅处理数据有效负载。 当应用程序处于前台时,多个通知可以正常工作,这里我使用click_action中来自服务器的id值将通知移到相关帖子上。但是,当应用程序处于后台/关闭状态时,只要我收到多个通知,我将点击一个通知,它将转到相关的帖子,但其余所有都不会重定向到相关的帖子,只是从通知中清除了。 我不知道为什么会发生这个问题,这是我的代码
public class MyFirebaseMessagingService extends FirebaseMessagingService {
private static final String TAG = "MyFirebaseMsgService";
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
Log.d(TAG, "From: " + remoteMessage.getFrom());
Log.d(TAG, "Message data payload: " + remoteMessage.getData());
sendNotification(remoteMessage.getData().get("title"),remoteMessage.getData().get("body"),remoteMessage.getData().get("click_action"),image);
}
private void sendNotification(String title, String message, String id, String image) {
Log.d("notificationdetails",title+",,,"+message+",,"+id);
Intent intent = null;
int postid = Integer.valueOf(id);
if(id.equals("")|| id==null) {
intent = new Intent(this, SplashActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
}
else{
Constants.PushId = postid;
intent = new Intent(this, DetailActivity.class);
Log.d("mypostid",postid+"");
intent.putExtra("id", postid);
intent.putExtra("backpage","main");
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
}
PendingIntent pendingIntent = PendingIntent.getActivity(this, postid, intent, PendingIntent.FLAG_UPDATE_CURRENT);
Uri defaultSoundUri= RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.ic_launcher_primepost)
.setContentTitle(title)
.setStyle(new NotificationCompat.BigTextStyle().bigText(message))
.setContentText(message)
.setAutoCancel(true)
.setSound(defaultSoundUri)
.setContentIntent(pendingIntent);
NotificationManager notificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
Random random = new Random();
int num = random.nextInt(99999-1000)+1000;
notificationManager.notify(num /* ID of notification */, notificationBuilder.build());
}
}
的Manifest.xml
<activity android:name="com.primepostnews.app.Activities.DetailActivity"
android:screenOrientation="portrait">
<intent-filter>
<action android:name="Detail" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
消息有两种类型:数据消息和通知消息。 无论应用程序是在前台还是在后台,都在onMessageReceived中处理数据消息。 数据消息是GCM传统上使用的类型。 当应用程序在前台时,仅在onMessageReceived中在此处接收到通知消息。 当应用程序在后台运行时,将显示自动生成的通知;当用户点击通知时,他们将返回到应用程序。 同时包含通知和数据有效负载的消息被视为通知消息。 Firebase控制台始终发送通知消息。
if (remoteMessage.getData() != null && remoteMessage.getData().size() > 0) {
Log.d(TAG, "Message data payload: " + remoteMessage.getData());
sendNotification(remoteMessage.getData().get("title"),remoteMessage.getData().get("body"),remoteMessage.getData().get("click_action"),image);
} else if (remoteMessage.getNotification() != null) {
Log.d(TAG, "Message Notification Body: " + remoteMessage.getNotification().getBody());
sendNotification(remoteMessage.getNotification().getBody(), remoteMessage.getNotification().getBody(),remoteMessage.getData().get("click_action"),image);
}
在所有情况下,您都应该使用通知类型的消息来工作。 当应用程序被杀死/在后台运行时,数据类型不起作用。 因为对于数据类型通知,将调用OnMessageRecieved,因此您必须使用NotificationBuilder手动创建状态栏通知。
因此,始终最好使用如下所示的来自服务器的通知类型输入
"notification": {
"title": "Firebase notification",
"message": "I am firebase notification. you can customise me. enjoy",
"click_action": "OPEN_ACTIVITY",
"sound":"default",
}
这在所有情况下都适用,并且您不必担心在代码中处理它。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.