gpt4 book ai didi

android - 如何取消android中未显示的通知

转载 作者:行者123 更新时间:2023-11-30 02:11:07 25 4
gpt4 key购买 nike

嗨,我是通知的新手,

我为通知编写了这段代码。

PendingIntent pendingIntent = PendingIntent.getActivity(context, leadidforstatus,
myIntent, Intent.FLAG_ACTIVITY_NEW_TASK);

NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(context)
.setSmallIcon(R.drawable.noti)
.setContentTitle("LMS notification")
.setContentIntent(pendingIntent)
.setContentText(intent.getStringExtra("messagenotify"))
.setAutoCancel(true);

i = (int) System.currentTimeMillis();
NotificationManager mNotificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
mNotificationManager.notify(i, mBuilder.build());

在那里我设置了一个通知列表。其中有些显示,有些不显示。但我想取消所有未显示的通知。

我使用了 mNotificationManager.cancleAll() 但那只是取消显示的通知。提前致谢。

最佳答案

我认为这里的问题是通知在创建后立即启动,而您没有使用 AlarmManager 来安排此通知。

解决方案应该是通过 AlarmManager 安排警报,这将触发上面的通知代码。

这需要 3 个部分:

注册 BroadCastReceiver 并在触发时构建通知:

public class UtilityReceiver  extends BroadcastReceiver {

@Override
public void onReceive(Context context, Intent intent) {
// .. Here is where you really want to build the notification and notify
// This will be triggered by the AlarmManager from the Code below
PendingIntent pendingIntent = PendingIntent.getActivity(context, leadidforstatus, myIntent, Intent.FLAG_ACTIVITY_NEW_TASK);

NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(context)
.setSmallIcon(R.drawable.noti)
.setContentTitle("LMS notification")
.setContentIntent(pendingIntent)
.setContentText(intent.getStringExtra("messagenotify"))
.setAutoCancel(true);

i = (int) System.currentTimeMillis();
NotificationManager mNotificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
mNotificationManager.notify(i, mBuilder.build());

}

使用 AlarmManager 安排一个警报以发送上面的接收器正在监听的广播:

        // Create an Intent to Launch
Intent notificationIntent = new Intent(context, UtilityReceiver.class);

// Use a Pending Intent to Launch the Alarm
PendingIntent notificationPendingIntent = PendingIntent.getBroadcast(context,
PendingIntent.FLAG_ONE_SHOT, notificationIntent, Intent.FILL_IN_DATA);
AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
alarmManager.set(AlarmManager.RTC_WAKEUP, timeForAlarmInMillis, notificationPendingIntent);

然后,当您遇到不再希望为您的应用程序显示通知的情况时,您可以告诉 AlarmManager 通过重建挂起的 Intent 并使用 AlarmManager 取消它来将它们从其警报队列中删除。

       // Create an Intent to Launch
Intent notificationIntent = new Intent(context, UtilityReceiver.class);

// Use a Pending Intent to Launch the Alarm
PendingIntent notificationPendingIntent = PendingIntent.getBroadcast(context,
PendingIntent.FLAG_ONE_SHOT, notificationIntent, Intent.FILL_IN_DATA);
AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);

// *********Important this will cancel all the Matching PendingIntents
alarmManager.cancel(notificationPendingIntent);

祝你好运,并确保在你的 Activity 或 Manifest 中注册你的 Receiver

关于android - 如何取消android中未显示的通知,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30075196/

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