android - 多个本地通知android

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

然后我将这个字符串添加到 list 中

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

感谢您的回答!

最佳答案

您需要为每个通知设置一个唯一的 ID:

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

关于android - 多个本地通知android,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20545182/

相关文章:

asp.net - 有哪些方法可以监控SQL数据库记录?

Android编程——将程序推送到后台

notifications - Windows Phone 7 通知/弹出/ toast

ios - UIApplicationProtectedDataWillBecomeUnavailable 通知 VS applicationProtectedDataWillBecomeUnavailable 委托(delegate)

android - 无法解决方法 setLatestEventInfo

android - 如何比较Android中的当前日期和上次修改日期?

android - 如何避免 ListView 闪烁 @android :id/empty before loading data. 设置的 View

android - 超出最小未出列缓冲区计数

java - float 操作按钮和 View 寻呼机

laravel - 如何路由来自不同领域 Laravel 的 Nexmo 通知?