Valeriy
Valeriy

Reputation: 805

Multiple local notifications android

Good afternoon.

I want to get multiple local notifications in my app, and so I have

in my Activity , this method should set alarms to 17:20 , 17:21, 17:22 , but I only have one notification in 17:22.

public void multiplyAlerts(){

    AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
    Intent intent = new Intent(this, LocalNotification.class);

    for(int i = 0; i < 3; i++){

        Calendar t_calendar = Calendar.getInstance();

        t_calendar.set(Calendar.MONTH, Calendar.DECEMBER);
        t_calendar.set(Calendar.YEAR, 2013);
        t_calendar.set(Calendar.DAY_OF_MONTH, 12);

        t_calendar.set(Calendar.HOUR_OF_DAY, 17);
        t_calendar.set(Calendar.MINUTE, 20 + i);
        t_calendar.set(Calendar.SECOND, 23);
        t_calendar.set(Calendar.AM_PM, Calendar.PM);

        PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 0, intent, PendingIntent.FLAG_ONE_SHOT);

        alarmManager.set(AlarmManager.RTC_WAKEUP, t_calendar.getTimeInMillis(),pendingIntent);

        System.out.println("Calling Alarm  " + i);

    }}

Local notification class

public class LocalNotification extends BroadcastReceiver {

NotificationManager nm;

@Override
public void onReceive(Context context, Intent intent) {

    nm = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
    CharSequence from = "Remsmed";
    CharSequence message = "Принимать что-то";
    PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, new Intent(), 0);
    Notification notif = new Notification(R.drawable.launcher,"Notification text", System.currentTimeMillis());
    notif.setLatestEventInfo(context, from, message, pendingIntent);
    notif.flags = Notification.FLAG_AUTO_CANCEL;
    nm.notify((int)System.currentTimeMillis(),notif);

    System.out.println("id = " + System.currentTimeMillis());
}}

And I add this string to manifest

<receiver android:name="ru.fors.remsmed.utils.LocalNotification"></receiver>

Thank you for the answers!

Upvotes: 1

Views: 1342

Answers (1)

Alex R.
Alex R.

Reputation: 861

You need to have a unique ID for each notification :

PendingIntent pendingIntent = PendingIntent.getActivity(context, my_id, new Intent(), 0);

Upvotes: 5

Related Questions