简体   繁体   English

当应用程序处于后台状态时,FCM多个推送通知无法正常工作

[英]FCM Multiple push notifications not working properly when app in background state

I am using fcm for push notifications, I am handling only data payload. 我正在使用fcm进行推送通知,仅处理数据有效负载。 Multiple Notifications are working fine when app is in foreground, here I am using id value from server in click_action to move the notification to related post when we tap on it.But, when app is in background/closed state whenever my getting multiple notifications, I will tap on one notification it will goes to related post but remaining all are not redirecting to related post, just cleared from notification. 当应用程序处于前台时,多个通知可以正常工作,这里我使用click_action中来自服务器的id值将通知移到相关帖子上。但是,当应用程序处于后台/关闭状态时,只要我收到多个通知,我将点击一个通知,它将转到相关的帖子,但其余所有都不会重定向到相关的帖子,只是从通知中清除了。 I didn't find out why this problem happening.Here is my code 我不知道为什么会发生这个问题,这是我的代码

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 的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>

There are two types of messages data messages and notification messages. 消息有两种类型:数据消息和通知消息。 Data messages are handled here in onMessageReceived whether the app is in the foreground or background. 无论应用程序是在前台还是在后台,都在onMessageReceived中处理数据消息。 Data messages are the type traditionally used with GCM. 数据消息是GCM传统上使用的类型。 Notification messages are only received here in onMessageReceived when the app is in the foreground. 当应用程序在前台时,仅在onMessageReceived中在此处接收到通知消息。 When the app is in the background an automatically generated notification is displayed.When the user taps on the notification they are returned to the app. 当应用程序在后台运行时,将显示自动生成的通知;当用户点击通知时,他们将返回到应用程序。 Messages containing both notification and data payloads are treated as notification messages. 同时包含通知和数据有效负载的消息被视为通知消息。 The Firebase console always sends notification messages. 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);
        }

You Should use a Notification type message to work in All the case. 在所有情况下,您都应该使用通知类型的消息来工作。 Data Type not work when the App is Killed / In background. 当应用程序被杀死/在后台运行时,数据类型不起作用。 Because for the Data Type notification The OnMessageRecieved will called and you have to manually create status bar notification using the NotificationBuilder. 因为对于数据类型通知,将调用OnMessageRecieved,因此您必须使用NotificationBuilder手动创建状态栏通知。

So always better to use Notification Type input from your server like below 因此,始终最好使用如下所示的来自服务器的通知类型输入

"notification": {
            "title": "Firebase notification",
            "message": "I am firebase notification. you can customise me. enjoy",
            "click_action": "OPEN_ACTIVITY",
            "sound":"default",

        }

This will work in all the case, And you don't want to worry about handling it on your code. 这在所有情况下都适用,并且您不必担心在代码中处理它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 FCM 推送通知仅在应用程序处于后台时显示 - FCM Push Notifications are only displayed when app is in background 当App位于前台时,FCM推送通知将被替换 - FCM Push Notifications are replacing when App is in Foreground 应用程序终止时的 FCM 推送通知 FLUTTER - FCM push notifications FLUTTER when app is terminated Flutter – 当应用程序被杀死时,推送通知在后台不起作用 - Flutter – push notifications not working in the background when app is killed Amazon Pinpoint 和 Ionic - 当应用程序在后台时推送通知不起作用 - Amazon Pinpoint and Ionic - Push notifications not working when app is in background Android:当应用程序在后台运行时,Notif push(FCM)无法正确接收 - Android: Notif push (FCM) doesn't receive properly when App is in background 应用程序被杀死时是否可以接收 FCM 推送通知? - Is it possible to receive FCM push notifications when app is killed? Android FCM在从后台删除应用时未收到通知 - Android FCM not receiving notifications when app is removed from background 当应用程序有后台处理程序时,在 Flutter 中未收到 FCM 通知 - FCM notifications not received in Flutter when there is a background handler for the app 当应用程序处于前台或后台时如何使用 FCM 处理通知 - How to handle notifications with FCM when app is in either foreground or background
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM