gpt4 book ai didi

android - cancelAll() 和 cancel() 不会忽略我的通知

转载 作者:行者123 更新时间:2023-12-05 00:17:01 25 4
gpt4 key购买 nike

我已经面临这个问题很长时间了,我有一个具有很多功能的应用程序,其中之一就是闹钟

我的通知只是停留在那里,永远不会消失尽管我正在打电话cancelAll()/cancel()从通知管理器,我也有 autoCancel设置为true !它还保持可点击状态,我只需要它在触发操作后消失!

(我正在使用 Android Studio 的模拟器上进行测试,不确定这是否是问题所在)

我搜索了很多,尝试了很多东西,但没有任何效果,所以我很感激您的帮助:)

我已将通知设置如下:

notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

// ...

private void initNotification() {
setNotificationChannel();

notificationBuilder = new NotificationCompat.Builder(getApplicationContext(), notificationChannel.getId());

Intent click_intent = new Intent(getApplicationContext(), RingtonePlayingService.class)
.putExtra("intent_action", "click")
.putExtra("REQUEST_CODE", alarmRequestCode)
.putExtra("ID", alarmId);
PendingIntent click_pending = PendingIntent.getService(this, MainActivity.getRequestCode(), click_intent, PendingIntent.FLAG_UPDATE_CURRENT);

Intent dismiss_intent = new Intent(getApplicationContext(), RingtonePlayingService.class)
.putExtra("intent_action", "dismiss")
.putExtra("REQUEST_CODE", alarmRequestCode)
.putExtra("ID", alarmId);
PendingIntent dismiss_pending = PendingIntent.getService(getApplicationContext(),MainActivity.getRequestCode(), dismiss_intent, PendingIntent.FLAG_UPDATE_CURRENT);

Intent snooze_intent = new Intent(getApplicationContext(), RingtonePlayingService.class)
.putExtra("intent_action", "snooze")
.putExtra("REQUEST_CODE", alarmRequestCode)
.putExtra("ID", alarmId);
PendingIntent snooze_pending = PendingIntent.getService(getApplicationContext(),MainActivity.getRequestCode(), snooze_intent, PendingIntent.FLAG_UPDATE_CURRENT);

dismissAction = new NotificationCompat.Action(R.drawable.small_delete,
"Dismiss", dismiss_pending);
snoozeAction = new NotificationCompat.Action(R.drawable.bell_ring,
"Snooze", snooze_pending);

notificationBuilder.setDefaults(Notification.DEFAULT_LIGHTS | Notification.DEFAULT_VIBRATE)
.setSmallIcon(R.drawable.alarm)
.setContentTitle("Alarm On!")
.setContentText("Click the notification to dismiss")
.setPriority(NotificationCompat.PRIORITY_DEFAULT)
.setContentIntent(click_pending)
.setDeleteIntent(click_pending)
.addAction(dismissAction)
.addAction(snoozeAction)
.setAutoCancel(true);
}

private void showNotification() {
notificationManager.notify(alarmRequestCode, notificationBuilder.build());
}

private void setNotificationChannel() {
channelId = "alarm_channel_id";
channelName = "alarm_notification_channel";
int importance = NotificationManager.IMPORTANCE_DEFAULT;
notificationChannel = new NotificationChannel(channelId, channelName, importance);
notificationChannel.enableLights(true);
notificationChannel.setLightColor(Color.RED);
notificationManager.createNotificationChannel(notificationChannel);
}

当按下操作按钮时,这是触发的代码部分:

if (action != null) {
switch (action) {
case "click":
alarmManager.cancel(cancelPendingIntent);
notificationManager.cancelAll();
player.stop();
changeAlarmValuesToOff();
break;
case "snooze":
alarmManager.cancel(cancelPendingIntent);
notificationManager.cancelAll();
player.stop();
setNewSnoozeAlarm();
break;
case "dismiss":
alarmManager.cancel(cancelPendingIntent);
notificationManager.cancelAll();
player.stop();
changeAlarmValuesToOff();
break;
default:
}
}

我也尝试过使用 cancel()使用alarmRequestCode这是用于警报和通知的唯一 ID,但仍然不起作用

一切工作正常,操作按需要执行,只是通知保持在那里并保持可点击并执行操作,如下面的屏幕截图所示

Screenshot of the notification

最佳答案

这里的问题不在于通知设置本身,问题在于通知在 Service 类中运行。当服务仍在运行时,无法取消通知。

我是如何解决这个问题的:在服务类中,执行操作后(我在调用 cancelAll() 的地方),我改为调用 stopSelf()。然后重写 onDestroy() 并调用其中的 cancelAll()!

干杯

关于android - cancelAll() 和 cancel() 不会忽略我的通知,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50163353/

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