I'm developing an app that can create multiple alarms, and these alarms send notifications to the user, so they can tap the notification and that leads to an activity with the alarm details which I want to display depending on the value of the extras, but my extras are always null, I don't understand what it's wrong.. I already searched a lot for questions and nothing seems to solve my problem.
This is where a create the alarm with a unique id each one. "item.getID()"
NewAlarm.java
firstTime = System.currentTimeMillis();
intervalTime = (1000 * 60 * 4); // 4 minutes
// crear el pending intent y la alarma
Intent intent = new Intent(this, AlarmBroadcastReceiver.class);
intent.putExtra("ID", item.getID());
intent.putExtra("MEDICINA", item.getMedicina());
PendingIntent pendingIntent = PendingIntent.getActivity(this, item.getID(), intent, PendingIntent.FLAG_UPDATE_CURRENT);
AlarmManager alarmManager = (AlarmManager) getSystemService(Activity.ALARM_SERVICE);
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, firstTime, intervalTime, pendingIntent);
AlarmBroadcastReceiver.java
public void onReceive(Context context, Intent intent) {
try {
int notifID = intent.getExtras().getInt("ID");
String medicina = intent.getExtras().getString("MEDICINA");
Intent alarmDetailsIntent = new Intent(context, AlarmDetails.class);
alarmDetailsIntent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
intent.putExtra("ID", notifID);
intent.putExtra("MEDICINA", medicina);
PendingIntent piDetails = PendingIntent.getActivity(context, notifID, alarmDetailsIntent, PendingIntent.FLAG_UPDATE_CURRENT);
NotificationManager notificationManager = (NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE);
NotificationCompat.Builder mbBuilder = new NotificationCompat.Builder(context);
mbBuilder.setSmallIcon(R.drawable.ic_menu_my_calendar);
mbBuilder.setContentTitle("Medic Reminder");
mbBuilder.setContentText(medicina);
mbBuilder.setVibrate(new long[]{100, 250, 100, 500});
mbBuilder.setContentIntent(piDetails);
notificationManager.notify(notifID, mbBuilder.build());
}catch (Exception ex){
ex.printStackTrace();
}
}
and when the user clicks on the notification, will have to open an activity and show details of the alarm with the ID extra, but getIntent.getExtras() is always NULL.
AlarmDetails.java
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.recordatorio_alarma);
Bundle extras = getIntent().getExtras();
if (extras != null){
try {
int notifID = extras.getInt("ID");
String medicina = extras.getString("MEDICINA");
TextView txtMedicina = (TextView) findViewById(R.id.txtMedicinaAlarma);
txtMedicina.setText("Medicina de prueba");
Button btnCerrar = (Button) findViewById(R.id.btnStopAlarm);
btnCerrar.setOnClickListener( new View.OnClickListener() {
@Override
public void onClick(View view) {
finish();
}
});
NotificationManager notificationManager = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);
notificationManager.cancel(notifID);
} catch (Exception e) {
e.printStackTrace();
}
}
}
You want getStringExtra(), not getExtras().getString(). That takes care of the case where you don't have any extras yet.