0

i trying do a "read more" after user click on notification.

What i need, start new Activity with unique extra content "id", "title" and "text".

My code:

void notificationSend(String id, String bar, String title, String text, String image, int delay) {
    Bitmap bitmap = getBitmapFromURL(image, 130);

    if(bitmap == null){
        bitmap = getBitmapFromURL("https://pp.vk.me/c621316/v621316641/119d5/HB9s2z5mX-s.jpg", 130);
    }

    Intent notificationIntent = new Intent(this, SingleNewsActivity.class);
    notificationIntent.putExtra("id", id);
    notificationIntent.putExtra("title", title);
    notificationIntent.putExtra("text", text);

    notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);

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

    NotificationCompat.Builder builder = new NotificationCompat.Builder(this);

    builder.setContentIntent(contentIntent);
    builder.setSmallIcon(R.drawable.ic_bitcoin_notification);
    builder.setLargeIcon(bitmap);
    builder.setTicker(bar);
    builder.setContentTitle(title);
    builder.setContentText(text);
    builder.setWhen(System.currentTimeMillis());
    builder.setSound(RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION));


    Notification notification = builder.build();
    notification.flags |= Notification.FLAG_AUTO_CANCEL;

    try {
        notificationManager.notify(new Integer(id), notification);
        sleep(2000);
    } catch (InterruptedException e) {
        e.printStackTrace();
    }
}

And it do next thing, lets send 2 notifications, after i click on first, activity started but with extra data from second notification, why?

  • Use should check this. It might help you getting what you want. [link] http://stackoverflow.com/questions/15297710/create-new-pending-intent-every-time-in-android – Ankii Rawat Jul 14 '15 at 05:54

2 Answers2

0

The issue is due to using : FLAG_UPDATE_CURRENT

if you see document for this flag:

Flag indicating that if the described PendingIntent already exists, then keep it but replace its extra data with what is in this new Intent. For use with getActivity(Context, int, Intent, int), getBroadcast(Context, int, Intent, int), and getService(Context, int, Intent, int).
This can be used if you are creating intents where only the extras change, and don't care that any entities that received your previous PendingIntent will be able to launch it with your new extras even if they are not explicitly given to it.

in your case, you are updating just extra and due to that, the value of extras get updated.

For solution of your issue, you should pass request code unique for all case:

Currant code:

PendingIntent.getActivity(this, 0, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);

Actual:

PendingIntent.getActivity(this, unique_code, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);
Kinnar Vasa
  • 397
  • 1
  • 9
0

So The reason is that you are setting unique id to the notification, lets suppose id value is 1 when your 1st notification comes then the notification object is created with that id, when you second notification come then your id is again 1 so your notification is updated and when you click on it you get the second notification. what you can do is set the different id for notifications. have a look at below code

private NotificationManager mNotificationManager;
private NotificationCompat.Builder builder;

write below code inside a function

 mNotificationManager = (NotificationManager) this
            .getSystemService(Context.NOTIFICATION_SERVICE);
    Random random = new Random();
    int NOTIFICATION_ID = random.nextInt(9999 - 1000) + 1000;
    Intent intent = new Intent(this, SingleNewsActivity.class);
    Bundle bundle = new Bundle();
    bundle.putExtra("id", id);
    bundle.putExtra("title", title);
    bundle.putExtra("text", text);

    intent.putExtras(bundle);

    PendingIntent contentIntent = PendingIntent.getActivity(this, NOTIFICATION_ID,
            intent, 0);


    NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(
            this).setSmallIcon(R.drawable.ic_launcher)
            .setContentTitle("TITLE")
            .setStyle(new NotificationCompat.BigTextStyle().bigText("ASD"))
            .setContentText("Summary").setAutoCancel(true);

    mBuilder.build().flags |= Notification.FLAG_AUTO_CANCEL;
    mBuilder.setContentIntent(contentIntent);

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

    Log.d("", "Notification sent suessfully.");

in the above code whenever you will receive notification you will get a unique code in NOTIFICATION_ID

Moubeen Farooq Khan
  • 2,875
  • 1
  • 11
  • 26