从GCM多个通知不是指导纠正活动 [英] Multiple notification from GCM not directing to correct activity

本文介绍了从GCM多个通知不是指导纠正活动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用GCM推送通知。我的约束是,从GCM服务器接收到的包我要重定向用户到特定的位置,我的应用程序。每一件事情是工作正常,当我只有一个通知。如果在通知托盘2通知则用户被重定向到基于最新束中的活性。所有其他的包都被忽略。 SO张贴就像我跟很多<一href="http://stackoverflow.com/questions/17023839/android-multiple-notification-sending-same-data-on-clicking">this和<一href="http://stackoverflow.com/questions/6066363/multiple-notifications-to-the-same-activity">this但因此未解决我的问题。下面是我的code摘录:

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

在接收端使用:

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

我会妥协的,我可能得到的结果。对我来说,最好的情况是,用户被重定向到基于我创造的意图设置该属性正确的页面。任何帮助是非常AP preciated,如果我不够具体请询问。

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.

推荐答案

您可以试试下面的code .. (请尝试使用FLAG_UPDATE_CURRENT)

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


    }

这篇关于从GCM多个通知不是指导纠正活动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆