iosamammohamed
iosamammohamed

Reputation: 327

Pending Intent for more than one notification

I have app which sends notifications, I used this code for pending intent

  Intent myIntent = new Intent(getApplicationContext(),MainActivity.class);
  myIntent.putExtra("link",Link);

  PendingIntent intent2 =  
  PendingIntent.getActivity(getApplicationContext(),1,myIntent, 
  PendingIntent.FLAG_ONE_SHOT);

and it worked nice for the first time, but I use this piece of code inside a method that invokes every 1 minute.

The problem is that the link variable changes from one to another.

and when I get the data in the MainActivity I found the last link only, all the notifications created has the last link.

and I don't know how to avoid this.

Upvotes: 3

Views: 2914

Answers (3)

Mori
Mori

Reputation: 4671

In Kotlin

Make a notification

val builder = NotificationCompat.Builder(context, CHANNEL_ID)

For one and more just change the request Code number:

    val pendingIntent = PendingIntent.getActivity(context,
System.currentTimeMillis().toInt(),intent, 0)

then

builder.setContentIntent(pendingIntent)

Upvotes: 0

New
New

Reputation: 152

Try This,

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

Upvotes: 0

Kapil G
Kapil G

Reputation: 4141

You are sending the same request code for your pending intents. These codes are defined as the 2nd parameter of your Pending intent declaration

Change

PendingIntent intent2 =  
  PendingIntent.getActivity(getApplicationContext(),1,myIntent, 
  PendingIntent.FLAG_ONE_SHOT);

To:

PendingIntent intent2 =  
  PendingIntent.getActivity(getApplicationContext(),UNIQUE_INT_VALUE_FOR_EVERY_CALL,myIntent, 
  PendingIntent.FLAG_ONE_SHOT);

If you use the same id, the intent will be reused and you will only get the last data rather than getting new data for every call.

Upvotes: 11

Related Questions