gpt4 book ai didi

android - 如何在android中创建多个本地通知

转载 作者:行者123 更新时间:2023-11-29 17:28:28 24 4
gpt4 key购买 nike

在我的 android 应用程序中,我需要在特定的一天以不同的时间间隔显示多个本地通知,我使用警报管理器和广播接收器来处理一个通知,但是当我尝试实现多个通知时,只有第二个通知显示。

这是我的主 Activity

    AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);

Intent notificationIntent = new Intent("android.media.action.DISPLAY_NOTIFICATION");
notificationIntent.addCategory("android.intent.category.DEFAULT");

PendingIntent broadcast = PendingIntent.getBroadcast(this, 100, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);

Calendar cal = Calendar.getInstance();
cal.set(2015, 10, 23, 15, 03);
alarmManager.setExact(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), broadcast);

这里是BroadcastReceiver

Intent notificationIntent = new Intent(context, MainActivity.class);

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

PendingIntent pendingIntent1 = stackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT);

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

Notification notification = builder.setContentTitle("TRR - 2016")
.setContentText("Station - 1 closed grace period only 10min")
.setTicker("New Message Alert!")
.setSmallIcon(R.mipmap.ic_launcher)
.setDefaults(Notification.DEFAULT_SOUND)
.setContentIntent(pendingIntent1).build();

NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(mCounter, notification);

Notification notification1 = builder.setContentTitle("TRR - 2016")
.setContentText("Station - 2 closed grace period only 10min")
.setTicker("New Message Alert!")
.setSmallIcon(R.mipmap.ic_launcher)
.setDefaults(Notification.DEFAULT_SOUND)
.setContentIntent(pendingIntent1).build();

NotificationManager notificationManager1 = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager1.notify(++mCounter, notification1);

最佳答案

每个通知必须有自己的通知 ID。

你的问题在这里:

notificationManager.notify(0, notification);

具体来说,“0”是通知的 ID。如果您不提供不同的 ID,Android 会认为您只是在更新已经存在的通知。

Documentation .

public void notify (int id, Notification notification)

Post a notification to be shown in the status bar. If a notification with the same id has already been posted by your application and has not yet been canceled, it will be replaced by the updated information.

你可以尝试这样的事情:

private int mCounter = 0;

...

notificationManager1.notify(++mCounter, notification1);

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

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com