多个本地Android的通知 [英] Multiple local notifications android

查看:141
本文介绍了多个本地Android的通知的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下午好。

我想在我的应用程序的多个本地通知,所以我有

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

在我的活动,此方法应设置报警以17:20,17:21,17:22,但我只有一个通知在17:22。

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

    }}

本地通知类

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>

感谢您的答案!

推荐答案

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

You need to have a unique ID for each notification :

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

这篇关于多个本地Android的通知的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆