多个本地通知android

时间:2013-12-12 13:43:25

标签: android notifications

下午好。

我想在我的应用中收到多个本地通知,所以我有

在我的Activity中,此方法应将闹钟设置为17:20,17:21,17:22,但我在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);

    }}

本地通知类

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());
}}

我将此字符串添加到清单

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

感谢您的回答!

1 个答案:

答案 0 :(得分:5)

每个通知都需要一个唯一的ID:

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