来自 FCM 的多个通知未重定向到正确的活动

2023-12-14

我正在使用 GCM 进行推送通知。我的限制是,从 GCM 服务器收到的捆绑包中,我必须将用户重定向到应用程序中的特定位置。当我只有一个通知时,一切都工作正常。如果通知托盘中有两个通知,则用户将被重定向到基于最新捆绑包的活动。所有其他捆绑包都将被忽略。我关注了很多这样的帖子this and this但这并没有解决我的问题。以下是我的代码的摘录:

private void sendNotification(String msg, String type, String id) {
    mNotificationManager = (NotificationManager) this
            .getSystemService(Context.NOTIFICATION_SERVICE);
    Intent intent = new Intent(this, MainActivity.class);
    intent.putExtra("notification_type", type);
    intent.putExtra("notification_id", id);
    intent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
    PendingIntent contentIntent = PendingIntent.getActivity(this, 0,
            intent, 0);//I have tried all flags of pending intent like PendingIntent.FLAG_UPDATE_CURRENT and other 3
    // flag_update_current
    NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(
            this).setSmallIcon(R.drawable.ic_launcher)
            .setContentTitle("RWD Rugby").setAutoCancel(true)
            .setStyle(new NotificationCompat.BigTextStyle().bigText(msg))
            .setContentText(msg);
    Random rnd = new Random();
    NOTIFICATION_ID = rnd.nextInt(90000);
    Log.d("notification number", NOTIFICATION_ID + "");
    mBuilder.setContentIntent(contentIntent);
    mNotificationManager.notify(NOTIFICATION_ID, mBuilder.build());
}

在接收端我使用:

Intent intent = getIntent();//this gives me extras from the intent that started the activity not pending intent
String fromNotification = intent.getStringExtra("notification_type");

我可能不得不对可能得到的结果做出妥协。对我来说,最好的情况是根据我在创建意图时设置的属性将用户重定向到正确的页面。非常感谢任何帮助,如果我不够具体,请询问。


你可以尝试下面的代码.. (尝试使用 FLAG_UPDATE_CURRENT)

private void sendNotification(String msg) {
        mNotificationManager = (NotificationManager)
                this.getSystemService(Context.NOTIFICATION_SERVICE);

        Intent myintent = new Intent(this, ReceiveActivity.class);
        myintent.putExtra("message", msg);
        PendingIntent contentIntent = PendingIntent.getActivity(this, 0,
                myintent, PendingIntent.FLAG_UPDATE_CURRENT);

        NotificationCompat.Builder mBuilder =
                new NotificationCompat.Builder(this)
       // .setSmallIcon(R.drawable.ic_stat_gcm)
        .setContentTitle("GCM Notification")
        .setStyle(new NotificationCompat.BigTextStyle()
        .bigText(msg))
        .setContentText(msg);

        mBuilder.setContentIntent(contentIntent);
        mNotificationManager.notify(NOTIFICATION_ID, mBuilder.build());


    }
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

来自 FCM 的多个通知未重定向到正确的活动 的相关文章