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

发布于 2025-02-02 13:12:26 字数 2707 浏览 3 评论 0原文

在我的应用中,我想显示尽可能多的日常通知(这是一种医疗提醒)。用户还设置了新通知的时间。但是我的应用程序目前仅显示一份最新通知。您能帮我解决这个问题吗?

我用于创建新通知的代码(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);

我的alarm> 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++;
    }
}

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

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:

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++;
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

笑咖 2025-02-09 13:12:26

当您创建pendingIntent时,

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

请检查您不会传递相同的请求代码MID。在这种情况下,Android“重用”以前创建的未决意图,这就是为什么您只会看到最新的通知而不是几个通知。

避免问题的最简单方法似乎是传递不同的请求代码(或其他参数,例如不同的setAction(...) - 参见下面的链接)。

有关更多信息,请浏览一下Android如何比较即将出现的意图: >

When you are creating a PendingIntent here

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

please check that you aren't passing the same request code 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.

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.

For more info please have a look at how Android compares pending intents: https://stackoverflow.com/a/20205696/2219237

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文