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());
}?
1
  • Try to clean your project or something, I have the same code as you and it is working now for me :/
    – Carnal
    Aug 26, 2013 at 15:58

2 Answers 2

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());
}
9
  • @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, 2013 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 Aug 26, 2013 at 14:53
  • @MuhammadAamirALi I am already doing that hence the System.currentTimeMillis()/1000
    – tony9099
    Aug 26, 2013 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, 2013 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, 2013 at 15:21
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.

3
  • did so. same result :/ very unpredictable behaviour
    – tony9099
    Aug 29, 2013 at 8:07
  • Do you sometimes have multiple notifications, or is it always only 1 in the top bar. Aug 30, 2013 at 20:36
  • No I do get multiple ones for sure If my app is not visible. but if it is visible, things go awry and random stuff happen...
    – tony9099
    Aug 31, 2013 at 13:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.