Multiple notification from GCM not directing to co

2019-02-16 03:46发布

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.

1条回答
淡お忘
2楼-- · 2019-02-16 04:06

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


    }
查看更多
登录 后发表回答