gpt4 book ai didi

java - Android多通知BroadcastReceiver

转载 作者:太空狗 更新时间:2023-10-29 16:27:48 25 4
gpt4 key购买 nike

我正在尝试为我的应用程序中的每个提醒实现多个通知。我在这里发现了一些线索,它可以使用每个通知的唯一 ID 来实现,但它不起作用我只收到一个通知,它被最后一组覆盖。另外我想知道如何在设备重启后保持通知完好无损。这是我的代码

 //call notification activation Intent
Intent intent = new Intent(getBaseContext(), AlarmReceiver.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(getBaseContext(), RQS_1, intent, 0);
AlarmManager alarmManager = (AlarmManager)getSystemService(Context.ALARM_SERVICE);
alarmManager.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), pendingIntent);

报警接收器类

public class AlarmReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {

//creating unique id for each specific notification
int ID = (int) ((new Date().getTime()/1000L) % Integer.MAX_VALUE);

NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
Intent notification = new Intent(context,MainActivity.class);
notification.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);

PendingIntent pi = PendingIntent.getActivity(context,0,notification,0);

Uri defaultSoundUri= RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
NotificationCompat.Builder mynotification = new NotificationCompat.Builder(context)
.setSmallIcon(R.drawable.ic_date_range_black_48dp)
.setContentTitle("note taker")
.setContentText("you have a reminder about a note")
.setAutoCancel(true)
.setContentIntent(pi)
.setSound(defaultSoundUri)
.setVibrate(new long[]{1000,1000,1000,1000,1000});

notificationManager.notify(ID,mynotification.build());
}
}

最佳答案

I found some leads around here that it could be achieved using unique id per each notification but it is not working i get only one notification which is overwritten by the last one set

我认为问题不在于您的通知,而在于您的AlarmManager。尝试为每个 PendingIntent 使用不同的 ID,并将标志设置为 PendingIntent.FLAG_ONE_SHOT

您的调用 Intent 应如下所示:

//call notification activation Intent
Intent intent = new Intent(getBaseContext(), AlarmReciever.class);
int RQS_1 = (int) System.currentTimeMillis();
PendingIntent pendingIntent = PendingIntent.getBroadcast(getBaseContext(), RQS_1, intent, PendingIntent.FLAG_ONE_SHOT);
AlarmManager alarmManager = (AlarmManager)getSystemService(Context.ALARM_SERVICE);
alarmManager.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), pendingIntent);

关于java - Android多通知BroadcastReceiver,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45530775/

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