0

I am using Google cloud messaging for my android application.GCM part is running successfully.Now I want to show the message in a UI activity when the user taps on the notification icon in the status bar.

I am launching my ResultShowActivity from the notification using the following code.

Intent notificationIntent = new Intent(context, ResultShowActivity.class);
   notificationIntent.putExtra("Message",msg);
PendingIntent contentIntent = PendingIntent.getActivity(this, 0,notificationIntent, 0);
    Uri alarmSound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
    NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(
            this).setSmallIcon(R.drawable.gcm_cloud)
            .setContentTitle("GCM Notification")
            .setStyle(new NotificationCompat.BigTextStyle().bigText(msg))
            .setAutoCancel(true)
            .setSound(alarmSound)
            .setContentText(msg);

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

The message is being sent properly,and shown correctly when the GCM notification appears but when I capture the extras of intent in ResultShowActivity it is always showing the old message. I am using following code to receive message from intent in ResultShowActivity.

String GCMMsg = showViewIntent.getStringExtra("Message");  

I have tried removing the in ResultShowActivity by using

intent.removeExtra("Message"); 

or

intent.replaceExtras(extras);

Can anyone please suggest how to retrieve the updated message from intent.Thanks for any help regarding this.

Samrat
  • 161
  • 2
  • 16
  • In newer version of android I found the same issues, following changes helped me. check this link : https://stackoverflow.com/a/70227216/5664529 – Siddhesh Shirodkar May 09 '23 at 06:29

2 Answers2

2

Please use following code

int iUniqueId = (int) (System.currentTimeMillis() & 0xfffffff);


PendingIntent contentIntent = PendingIntent.getActivity(this,iUniqueId,notificationIntent, 0);

Instead of following line of code

PendingIntent contentIntent = PendingIntent.getActivity(this, 0,notificationIntent, 0);

Hope this will help

Aman Systematix
  • 347
  • 2
  • 10
2

You need to add Intent.FLAG_ACTIVITY_SINGLE_TOP to intent flag and PendingIntent.FLAG_UPDATE_CURRENT to pending intent flag.

Intent intent = new Intent(this,YourActivity.class);

intent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);

PendingIntent pendingIntent = 
PendingIntent.getActivity(this,0,intent,PendingIntent.FLAG_UPDATE_CURRENT);

Important: Make sure to implement onNewIntent(Intent intent). This function will be called when your activity is in foreground.