0

I will try to create multi local notification every time with different parameters this is my code to create the notification:

 public void setNotficition(int Time,String ProdName,String ProdDesc,String ProdID){
    AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);

    Intent notificationIntent = new Intent("android.media.action.DISPLAY_NOTIFICATION");
    notificationIntent.addCategory("android.intent.category.DEFAULT");
    notificationIntent.putExtra("ProdName",ProdName);
    notificationIntent.putExtra("ProdDesc",ProdDesc);
    notificationIntent.putExtra("ProdID",ProdID);

    PendingIntent broadcast = PendingIntent.getBroadcast(this, 100, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);

    Calendar cal = Calendar.getInstance();
    cal.add(Calendar.SECOND, Time);
    alarmManager.setExact(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(),broadcast);
}

This is my Code for BroadcastReceiver:

    @Override
public void onReceive(Context context, Intent intent) {
    String ProdName= intent.getStringExtra("ProdName");
    String ProdDesc= intent.getStringExtra("ProdDesc");
    String ProdID= intent.getStringExtra("ProdID");

    int ID = Integer.parseInt(ProdID);
    Intent notificationIntent = new Intent(context, NotificationActivity.class);

    TaskStackBuilder stackBuilder = TaskStackBuilder.create(context);
    stackBuilder.addParentStack(NotificationActivity.class);
    stackBuilder.addNextIntent(notificationIntent);

    PendingIntent pendingIntent = stackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT);

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

        Notification notification = builder.setContentTitle(ProdName)
                .setContentText(ProdDesc)
                .setTicker("New Message Alert!")
                .setSmallIcon(R.mipmap.ic_launcher)
                .setContentIntent(pendingIntent).build();
        NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
        notificationManager.notify((int) ((new Date().getTime() / 1000L) % Integer.MAX_VALUE), notification);
}

Every time its take the last call

lpacheco
  • 976
  • 1
  • 14
  • 27
Tzahi
  • 259
  • 3
  • 12

3 Answers3

4

Notification id should be unique within your application.

If a notification with the same id has already been posted by your application and has not yet been canceled, it will be replaced by the updated information.

NotificationManager notiManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);    
notiManager.notify(UNIQUE_ID, notification);

If you are using PendingIntent.getBroadcast() method, use different requestCode for different notification:

Intent notificationIntent = new Intent("android.media.action.DISPLAY_NOTIFICATION");
notificationIntent.addCategory("android.intent.category.DEFAULT");
notificationIntent.putExtra("ProdName",ProdName);
notificationIntent.putExtra("ProdDesc",ProdDesc);
notificationIntent.putExtra("ProdID",ProdID);

PendingIntent broadcast = PendingIntent.getBroadcast(this, REQUEST_CODE, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);

Hope this will help!

Ferdous Ahamed
  • 21,438
  • 5
  • 52
  • 61
2

You can create multiple notification by changing notification id, everytime.

PendingIntent broadcast = PendingIntent.getBroadcast(this, 100, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);

The second parameter of the getBroadcast call is "notification id" (i.e. 100 in your case). Just use different different notification id in case of generating multiple Notifications.

Hope it will help u :-)

Neo
  • 3,546
  • 1
  • 24
  • 31
0

A notification is created and linked to a id, this id can be used to modify or update on existing notification, weather you are changing the intent or just the behavior of the stack.

Remario
  • 3,813
  • 2
  • 18
  • 25