i am trying to show different notifications after some time interval but what happens is it updates the current PendingIntent
by the new one as a result i get only single notification even if i fire 4 - 5 pending intents requests
On button click i do the following thing
try{
adapter.OpenDB();
int id = adapter.getMaxId("reminder")+1;
adapter.CloseDB();
Intent intent = new Intent(MilestonesActivity.this, AlarmReceiver.class);
intent.putExtra("message", ""+editMsg.getText()+" "+editDate.getText());
intent.putExtra("id", id);
PendingIntent pendingIntent = PendingIntent.getBroadcast(MilestonesActivity.this, 0,
intent, PendingIntent.FLAG_UPDATE_CURRENT);
am.set(AlarmManager.RTC_WAKEUP,
reminddate.getTimeInMillis(), pendingIntent);
adapter.CloseDB();
}catch (Exception e) {
// TODO: handle exception
}
AlarmReceiver.java
@Override
public void onReceive(Context context, Intent intent) {
Intent i =new Intent(context, NotificationService.class);
try{
int id = intent.getExtras().getInt("id");
i.putExtra("id", id);
CharSequence message = intent.getExtras().getString("message");
i.putExtra("message", message);
}catch (Exception e) {
// TODO: handle exception
}
context.startService(i);
}
NotificationService.java
public void show(Intent intent) {
try{
NotificationManager nm;
Log.e("recieve", "ok");
nm = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
CharSequence from = "Lawyer app Reminder";
CharSequence message = intent.getExtras().getString("message");
PendingIntent contentIntent = PendingIntent.getActivity(this, 0, new Intent(),
PendingIntent.FLAG_ONE_SHOT);// also tried FLAG_UPDATE_CURRENT
int id =intent.getExtras().getInt("id");
Log.e("I", ""+id);
Notification notif = new Notification(R.drawable.message_active,
"Lawyer app Reminder.", System.currentTimeMillis());
notif.defaults = Notification.DEFAULT_ALL;
notif.flags = Notification.FLAG_AUTO_CANCEL;
notif.setLatestEventInfo(this, from, message, contentIntent);
nm.notify(id, notif);
}catch (Exception e) {
e.printStackTrace();
}
plaese help me out with this .thanks in advance
In conclusion, the general and main difference between Intent and PendingIntent is that by using the first, you want to start / launch / execute something NOW, while by using the second entity you want to execute that something in the future.
Android PendingIntent In other words, PendingIntent lets us pass a future Intent to another application and allow that application to execute that Intent as if it had the same permissions as our application, whether or not our application is still around when the Intent is eventually invoked.
Create an Intent that starts the Activity . Set the Activity to start in a new, empty task by calling setFlags() with the flags FLAG_ACTIVITY_NEW_TASK and FLAG_ACTIVITY_CLEAR_TASK . Create a PendingIntent by calling getActivity() .
1- requestCode is used to get the same pending intent later on (for cancelling etc) 2- Yes, they will get override as long as your specify the same Receiver to your Intent that you specify on your PendingIntent.
i found it
the second parameter of pendingintent constructor should not be same (which was 0 hard coded)
i changed it to (int) System.currentTimeMillis() so that it should not be the same :)
Intent ni =new Intent(NotificationService.this,ModifyDatabaseService.class);
ni.putExtra("id", ""+id);
PendingIntent contentIntent = PendingIntent.getActivity(this, (int) System.currentTimeMillis(), ni,
PendingIntent.FLAG_ONE_SHOT);
If the value of id in nm.notify(id, notif);
is same then the Notification will be overwritten.
So you need to make sure that the id is different for different Notification
In your case if second PendingIntent
is "equal" to the previous one, it replaces it.
PendingIntents are equal if all of the following are the same:
Intent
's data (type, uri, category..etc)So, if you don't want the second PendingIntent
to override the previous, change one of the list above.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With