简体   繁体   English

在 Android Studio 中显示多个每日重复通知

[英]Show multiple daily repeating notifications in Android Studio

In my app I want to show as many daily notifications as user wants (it's sort of a medical reminder).在我的应用程序中,我想根据用户的需要显示尽可能多的每日通知(这是一种医疗提醒)。 User also sets time of the new notification.用户还设置新通知的时间。 But my app currently shows only one, latest notification.但我的应用目前只显示一个最新通知。 Could you help me solve this problem?你能帮我解决这个问题吗?

My code for creating new notification ( setHour etc. are previously set by the user):我用于创建新通知的代码( setHour等之前由用户设置):

Calendar calendar = Calendar.getInstance();
                    calendar.set(Calendar.HOUR_OF_DAY, setHour);
                    calendar.set(Calendar.MINUTE, setMinute);
                    calendar.set(Calendar.SECOND, 0);
                    Intent intent1 = new Intent(ReminderActivity.this, AlarmReceiver.class);
                    PendingIntent pendingIntent = PendingIntent.getBroadcast(ReminderActivity.this, MID,intent1, 0);
                    AlarmManager am = (AlarmManager) ReminderActivity.this.getSystemService(ReminderActivity.this.ALARM_SERVICE);
                    am.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), AlarmManager.INTERVAL_DAY, pendingIntent);

And my AlarmReceiver :还有我的AlarmReceiver

public class AlarmReceiver extends BroadcastReceiver {
    public static int MID = 0;
    private static final String CHANNEL_ID = "this.is.my.channelId";
    @Override
    public void onReceive(Context context, Intent intent) {
        Intent notificationIntent = new Intent(context, MainActivity.class);

        TaskStackBuilder stackBuilder = TaskStackBuilder.create(context);
        stackBuilder.addParentStack(MainActivity.class);
        stackBuilder.addNextIntent(notificationIntent);

        PendingIntent pendingIntent = stackBuilder.getPendingIntent(MID, 0);

        Notification.Builder builder = new Notification.Builder(context);

        Notification notification = builder.setContentTitle("Notification")
                .setContentText("New Notification")
                .setTicker("New Message Alert!")
                .setSmallIcon(R.mipmap.ic_launcher)
                .setVibrate(new long[]{1000, 1000, 1000, 1000, 1000})
                .setContentIntent(pendingIntent).build();

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            builder.setChannelId(CHANNEL_ID);
        }

        NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
//below creating notification channel, because of androids latest update, O is Oreo
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            NotificationChannel channel = new NotificationChannel(
                    CHANNEL_ID,
                    "NotificationDemo",
                    NotificationManager.IMPORTANCE_DEFAULT
            );
            notificationManager.createNotificationChannel(channel);
        }

        notificationManager.notify(MID, notification);
        MID++;
    }
}

When you are creating a PendingIntent here当您在此处创建PendingIntent

PendingIntent pendingIntent = PendingIntent.getBroadcast(ReminderActivity.this, MID,intent1, 0);

please check that you aren't passing the same request code MID .请检查您是否传递了相同的请求代码MID In this case Android "reuses" the pending intent which were created previously, that's why you would see only the latest notification instead of several notifications.在这种情况下,Android 会“重用”之前创建的待处理 Intent,这就是为什么您只会看到最新通知而不是多个通知的原因。

It seems the easiest way to avoid the issue would be passing different request codes (or another params, for example different setAction(...) - see the link below) for every type of notifications.避免该问题的最简单方法似乎是为每种类型的通知传递不同的请求代码(或其他参数,例如不同的setAction(...) - 请参阅下面的链接)。

For more info please have a look at how Android compares pending intents: https://stackoverflow.com/a/20205696/2219237有关更多信息,请查看 Android 如何比较未决意图: https ://stackoverflow.com/a/20205696/2219237

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM