0

I have the below class that gets called whenever I receive a push notification. (works wherever though). The issue is that, If I send a notification from another device to my mobile (lets say an iphone) I am getting the notification and it stays in the notification bar. If I do not touch it, and send another one, another one will arrive and 2 icons will now appear in the notification bar, However, the moment I send a notification from my own mobile to myself, all the current notifications that are in the bar disappear and get replaced by mine. When I send a new one, additional icon does not appear, but rather the old one gets updated.

How can I make each notification appear in the notification bar ? (because each one has a unique ID sent to it via the intent that does specific thing upon opening of the designated activity)

private void sendNotification(String data, String id, Context ctx)
{
mNotificationManager = (NotificationManager) ctx.getSystemService(Context.NOTIFICATION_SERVICE);

Intent myIntent = new Intent(ctx, myActiviy.class);
myIntent.putExtra("data", data);
myIntent.putExtra("id", id);

PendingIntent contentIntent = PendingIntent.getActivity(ctx, 0, myIntent, PendingIntent.FLAG_UPDATE_CURRENT);

NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(ctx)
        .setSmallIcon(R.drawable.ic_launcher)
        .setContentTitle("notification")
        .setTicker("notification")
        .setStyle(new NotificationCompat.BigTextStyle().bigText(data))
        .setAutoCancel(false)
        .setContentText(data);

mBuilder.setContentIntent(contentIntent);
mNotificationManager.notify((int) (System.currentTimeMillis()/1000), mBuilder.build());
}?
tony9099
  • 4,567
  • 9
  • 44
  • 73
  • Try to clean your project or something, I have the same code as you and it is working now for me :/ – Carnal Aug 26 '13 at 15:58

2 Answers2

0

The procedure you are using to show notification is now depreciated. Use the following also give unique int to mNotificationManager.notify(uniqueId, mBuilder.build() to show seperate notifications.

private void generateNotification(Context context, String message, String notificationId) {
    int notify = Integer.valueOf(notificationId);
    NotificationManager mNotificationManager = (NotificationManager)
            context.getSystemService(Context.NOTIFICATION_SERVICE);

    Intent resultIntent = new Intent(context, ViewPost.class);
    Bundle params = new Bundle();
    params.putString("group_id", this.groupId.get(notificationId));
    params.putString("post_id", this.postId.get(notificationId));
    params.putString("notification", "notification");
    resultIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    resultIntent.putExtras(params);

    TaskStackBuilder stackBuilder = TaskStackBuilder.create(context);
    stackBuilder.addParentStack(ViewPost.class);
    stackBuilder.addNextIntent(resultIntent);
    PendingIntent resultPendingIntent = stackBuilder.getPendingIntent(notify,
                PendingIntent.FLAG_CANCEL_CURRENT);

    NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(context)
        .setSmallIcon(R.drawable.icon)
        .setContentTitle("FleeGroups Notification")
        .setStyle(new NotificationCompat.BigTextStyle()
        .bigText(message))
        .setContentText(message)
        .setAutoCancel(true)
        .setDefaults(Notification.DEFAULT_ALL);
    mBuilder.setContentIntent(resultPendingIntent);
    mNotificationManager.notify(notify, mBuilder.build());
}
Muhammad Aamir Ali
  • 20,419
  • 10
  • 66
  • 57
  • @MuhammadAamirALi , are you sure they are deprecated ? And are you sure your code solves my issue ? the way I see it, its almost the same code.. – tony9099 Aug 26 '13 at 14:50
  • @Carnal The only difference is the TaskStackTrace, tony just pass the unique id to mNotificationManager.notify() mehtod. Your problem will be solved – Muhammad Aamir Ali Aug 26 '13 at 14:53
  • @MuhammadAamirALi I am already doing that hence the System.currentTimeMillis()/1000 – tony9099 Aug 26 '13 at 15:10
  • @tony9099 I don't see why your code should erase all other notifications. Sure, you're not calling cancelAll() or cancel(id) somewhere? – Carnal Aug 26 '13 at 15:20
  • @tony9099, if your "id" that you pass in your method is unique, you could try and use that. Otherwise, create a instance variable "notifyId" and do notifyId++ before calling notify() and see if that works. – Carnal Aug 26 '13 at 15:21
  • @Carnal no I am not calling cancelAll() nor cancel(id).. As I told you, this behavior happens only when I send a notification from my device to my device... I checked via the logs, and each ID is unique.. – tony9099 Aug 26 '13 at 15:24
  • @tony9099 Try to replace PendingIntent.FLAG_UPDATE_CURRENT with 0 – Carnal Aug 26 '13 at 15:36
  • @tony9099 I saw this now in my app too, same problem as you. I'll look into this and see if I find a solution! – Carnal Aug 26 '13 at 15:50
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/36310/discussion-between-tony9099-and-carnal) – tony9099 Aug 27 '13 at 09:03
0

Try to use this for id: int id = (int) System.currentTimeMillis();

It maybe that the two notifications were sent one after the other so the /1000 is making it resolve to the same number.

user1270175
  • 243
  • 3
  • 8