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

3

我正在使用FCM进行推送通知,只处理数据有效载荷。当应用程序在前台时,多个通知可以正常工作,在这里,我使用来自服务器的ID值在click_action中移动通知以便在点击它时将其移到相关帖子。但是,当应用程序处于后台/关闭状态时,每当我收到多个通知时,我会点击一个通知,它会进入相关帖子,但是其余的通知不会重定向到相关帖子,只是从通知中清除。我没有找到为什么会出现这个问题的原因。以下是我的代码

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>

你正在使用 PendingIntent.FLAG_UPDATE_CURRENT 这个标志来更新通知数据,如果存在相同的通知ID。就像你正在使用随机数生成ID一样。尝试使用不同的方法来生成通知ID,也许会有所帮助。 - undefined
顺便说一句:通过 random.nextInt(99999-1000)+1000 来生成通知 ID 是一个不好的主意;最好使用 epoch 代替。 - undefined
2个回答

0

在所有情况下,您应该使用通知类型的消息。当应用程序被杀死或在后台运行时,数据类型将无法工作。因为对于数据类型的通知,将调用OnMessageRecieved并且您必须手动使用NotificationBuilder创建状态栏通知。

因此,最好始终从服务器使用通知类型输入,如下所示。

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

        }

这将适用于所有情况,而且您不需要担心在代码中处理它。

0

有两种类型的消息数据消息和通知消息。无论应用程序是在前台还是后台,数据消息都在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);
        }

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接