What should I do so that when my application sends a second (third... fourth... etc) notification it is not replaced and all are included?
MainActivity.java:
public class MainActivity extends ActionBarActivity {
DatePicker datePicker;
TimePicker timePicker;
Button btnStart;
Uri uriAlarm;
final static int RQS_1 = 1;
EditText textonombre;
EditText textodescripcion;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.notif_main);
datePicker = (DatePicker)findViewById(R.id.datepicker);
timePicker = (TimePicker)findViewById(R.id.timepicker);
btnStart = (Button)findViewById(R.id.start);
uriAlarm = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_ALARM);
textonombre = (EditText)findViewById(R.id.textonombre);
textodescripcion = (EditText)findViewById(R.id.textodescripcion);
btnStart.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
setAlarm(uriAlarm);
Toast.makeText(MainActivity.this, "Alarma ACTIVADA", Toast.LENGTH_SHORT).show();
}
});
}
private void setAlarm(Uri passuri){
Calendar cal = Calendar.getInstance();
cal.set(datePicker.getYear(),
datePicker.getMonth(),
datePicker.getDayOfMonth(),
timePicker.getCurrentHour(),
timePicker.getCurrentMinute(),
00);
Intent intent = new Intent(getBaseContext(), pruebaintento.dos.notif.AlarmReceiver.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(
getBaseContext(),
RQS_1,
intent,
PendingIntent.FLAG_CANCEL_CURRENT);
AlarmManager alarmManager = (AlarmManager)getSystemService(Context.ALARM_SERVICE);
alarmManager.set(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), pendingIntent);
}
}
AlarmReceiver.java
public class AlarmReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
Intent repeating_intent = new Intent(context,MainActivity.class);
repeating_intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent pendingIntent = PendingIntent.getActivity(context,id,repeating_intent, PendingIntent.FLAG_UPDATE_CURRENT);
NotificationCompat.Builder builder = (NotificationCompat.Builder) new NotificationCompat.Builder(context)
.setContentIntent(pendingIntent)
.setSmallIcon(R.drawable.ic_launcher)
.setContentTitle("NOMBRE")
.setContentText("DESCRIPCIÓN")
.setAutoCancel(true);
notificationManager.notify(id,builder.build());
}
}
Edit: @Elenasys
public class AlarmReceiver extends BroadcastReceiver {
int id= 1;
@Override
public void onReceive(Context context, Intent intent) {
NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
Intent repeating_intent = new Intent(context,MainActivity.class);
repeating_intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent pendingIntent = PendingIntent.getActivity(context,id++,repeating_intent, PendingIntent.FLAG_UPDATE_CURRENT);
NotificationCompat.Builder builder = (NotificationCompat.Builder) new NotificationCompat.Builder(context)
.setContentIntent(pendingIntent)
.setSmallIcon(R.drawable.ic_launcher)
.setContentTitle("NOMBRE")
.setContentText("DESCRIPCIÓN")
.setAutoCancel(true);
notificationManager.notify(id++,builder.build());
}
}
The problem is located here:
You must ensure that the id is different when generating a new notification.
It is even important that the id is different, since if the notification contains different data, it may not be updated even if you send it correctly in subsequent notifications.
In code you can define
or send the id of the notification to send that would be defined as an autoincrementing primary key.
This question has a long time, I came in with the same problem and I was able to solve it in the following way.
generate a random number, curiously creating an int by itself and adding the ++ did not work, and finally the variable "nu" is passed to the notify.
It worked for me, hope it helps someone