GCM的多次通知没有指向正确的活动[英] Multiple notification from GCM not directing to correct activity

本文是小编为大家收集整理的关于GCM的多次通知没有指向正确的活动的处理/解决方法,可以参考本文帮助大家快速定位并解决问题,中文翻译不准确的可切换到English标签页查看源文。

问题描述

我正在使用GCM进行推送通知.我的约束是,从GCM服务器的接收捆绑包中,我必须将用户重定向到我的应用程序中的特定位置.当我只有一个通知时,每件事都可以正常工作.如果通知托盘中有两个通知,则根据最新捆绑包将用户重定向到活动.所有其他捆绑包被忽略.我已经关注了很多帖子,例如 this 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());


    }

本文地址:https://www.itbaoku.cn/post/102771.html

问题描述

I am using GCM for push notifications. My constraint is that from the received bundle from GCM server I have to redirect user to specific location in my application. Every thing is working fine when I have only one notification. If there are two notification in the notification tray then the user is redirected to the activity based on the latest bundle. All other bundle are ignored. I have followed many SO post like this and this but it didnot solve my problem. Here are excerpts from my code:

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());
}

On the receiving end I use :

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

I might have to compromise on the result that I might get. For me the best case scenario is that user is redirected to the correct page based on the property i set on creating the intent. Any help is much appreciated and if I am not specific enough please ask.

推荐答案

You can try the below code.. (try using 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());


    }