Tzahi Tzahi - 1 month ago 8
Android Question

Create multi Notification With Different Intent

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

Answer

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!