0

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.

Community
  • 1
  • 1
Illegal Argument
  • 10,090
  • 2
  • 44
  • 61

1 Answers1

2

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


    }
Govind
  • 2,482
  • 1
  • 28
  • 40
  • already tried that and all the 5 kinds of pending intent. None worked for me. FLAG_UPDATE_CURRENT will update the all the notification that I have to the latest one so if a notification is intended to take user to message fragment it can take it to another fragment any ways thanks for trying – Illegal Argument Jun 13 '14 at 09:53