Mohamed QARA Mohamed QARA - 4 months ago 32
Android Question

Manage multiple alarms in the broadcastReceiver

I have searched everywhere to find answers to my question. I

I have created one alarm in my PreferenceActivity and it functions with my broadcast receiver who display a simple notification. But when i want to set multiple alarms, i don't know how to choose the notification to display on the broadcastReceiver.

I use the following code to sets the alarms:

public void SetAlarm (){
int id1=1, id2=2;
//Preferences
SharedPreferences prefs = PreferenceManager
.getDefaultSharedPreferences(SettingActivity.this);

tA = prefs.getString("timePrefA_Key", "Default TimePrefrence");
tB = prefs.getString("timePrefB_Key", "Default TimePrefrence");

//Calendars
Calendar cA = Calendar.getInstance();
cA.setTimeInMillis(System.currentTimeMillis());
cA.set(Calendar.HOUR_OF_DAY, TimePreference.getHour(tA));
cA.set(Calendar.MINUTE, TimePreference.getMinute(tA));
cA.set(Calendar.SECOND,0);

Calendar cB = Calendar.getInstance();
cB.setTimeInMillis(System.currentTimeMillis());
cB.set(Calendar.HOUR_OF_DAY, TimePreference.getHour(tB));
cB.set(Calendar.MINUTE, TimePreference.getMinute(tB));
cB.set(Calendar.SECOND,0);


//Intents
Bundle bundle = new Bundle();
AlarmManager am = (AlarmManager) getSystemService(ALARM_SERVICE);

Intent alertIntent = new Intent(this,NotificationReceiver.class);
PendingIntent pi1 = PendingIntent.getBroadcast(this, id1,
alertIntent, PendingIntent.FLAG_UPDATE_CURRENT);
bundle.putInt("NotificationId1", id1);

PendingIntent pi2 = PendingIntent.getBroadcast(this, id2,
alertIntent, PendingIntent.FLAG_CANCEL_CURRENT);
bundle.putInt("NotificationId1", id2);

alertIntent.putExtras(bundle);

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT ) {
am.setExact(AlarmManager.RTC_WAKEUP, cA.getTimeInMillis(), pi1);
am.setExact(AlarmManager.RTC_WAKEUP, cB.getTimeInMillis(), pi2);
}
else {
am.set(AlarmManager.RTC_WAKEUP, cA.getTimeInMillis(), pi1);
am.set(AlarmManager.RTC_WAKEUP, cB.getTimeInMillis(), pi2);
}}


And here is my BroadcastReceiver. My problem is in Onreceive. What should I do to choose the notification to display ?

public class NotificationReceiver extends BroadcastReceiver {
int NotifId1=1, NotifId2=2;
public void createnotification (Context context,String msg, String msgText, String msgAlert,Class <?> cls, int id){
PendingIntent notifIntent = PendingIntent.getActivity(context,0,
new Intent(context,cls),0);
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(context);
mBuilder.setSmallIcon(R.drawable.produits);
mBuilder.setLargeIcon(BitmapFactory.decodeResource(context.getResources(), R.drawable.produits));
mBuilder.setContentTitle(msg);
mBuilder.setTicker(msgAlert);
mBuilder.setContentText(msgText);
mBuilder.setContentIntent(notifIntent);
mBuilder.setDefaults(NotificationCompat.DEFAULT_SOUND);
mBuilder.setAutoCancel(true);
mBuilder.setVibrate(new long[]{1000, 1000, 1000, 1000, 1000});
mBuilder.setWhen(System.currentTimeMillis());
NotificationManager mNotificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
mNotificationManager.notify(id, mBuilder.build());
}
@Override
public void onReceive(Context context, Intent intent) {

Bundle extra = intent.getExtras();
if(extra != null) {
NotifId1 = extra.getInt("NotificationId1");
NotifId2 = extra.getInt("NotificationId2");
}
createnotification(context, "Petit déjeuner", "Heure du petit déjeuner", "Veuillez prendre votre petit déjeuner SVP", MainActivity.class,NotifId1);
createnotification(context, "Collation", "Heure de collation","Veuillez prendre votre collation SVP", MainActivity.class,NotifId2);
}}

Answer

This code can't work:

//Intents
Bundle bundle = new Bundle();
AlarmManager am = (AlarmManager) getSystemService(ALARM_SERVICE);

Intent alertIntent = new Intent(this,NotificationReceiver.class);
    PendingIntent pi1 = PendingIntent.getBroadcast(this, id1,
            alertIntent, PendingIntent.FLAG_UPDATE_CURRENT);
bundle.putInt("NotificationId1", id1);

PendingIntent pi2 = PendingIntent.getBroadcast(this, id2,
        alertIntent, PendingIntent.FLAG_CANCEL_CURRENT);
bundle.putInt("NotificationId1", id2);

alertIntent.putExtras(bundle);

You need to add the extras to alertIntent before you call PendingIntent.getBroadcast(), otherwise they won't be present when you need them.

Comments