gpt4 book ai didi

android - 如何检测通知是否已被驳回?

转载 作者:IT老高 更新时间:2023-10-28 22:18:05 26 4
gpt4 key购买 nike

Android 中是否有任何方法可以检测用户何时向左滑动通知并将其删除?我正在使用警报管理器设置重复警报,并且当用户取消通知时,我需要停止重复警报。这是我的代码:

设置重复提醒:

AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, c.getTimeInMillis(), repeatFrequency, displayIntent);

我的通知代码:

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

//Get the notification ID.
int notifID = getIntent().getExtras().getInt("Reminder_Primary_Key");

//Get the name of the reminder.
String reminderName = getIntent().getExtras().getString("Reminder_Name");

//PendingIntent stores the Activity that should be launched when the user taps the notification.
Intent i = new Intent(this, ViewLocalRemindersDetail.class);
i.putExtra("NotifID", notifID);
i.putExtra("notification_tap", true);

//Add FLAG_ACTIVITY_NEW_TASK to stop the intent from being launched when the notification is triggered.
PendingIntent displayIntent = PendingIntent.getActivity(this, notifID, i, Intent.FLAG_ACTIVITY_NEW_TASK);

NotificationManager nm = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
Notification notif = new Notification(R.drawable.flag_red_large, reminderName, System.currentTimeMillis());

CharSequence from = "Here's your reminder:";

CharSequence message = reminderName;
notif.setLatestEventInfo(this, from, message, displayIntent);

//Pause for 100ms, vibrate for 250ms, pause for 100ms, and vibrate for 500ms.
notif.defaults |= Notification.DEFAULT_SOUND;
notif.vibrate = new long[] { 100, 250, 100, 500 };
nm.notify(notifID, notif);

//Destroy the activity/notification.
finish();

}

我知道我需要调用 alarmManager.cancel(displayIntent) 来取消我的重复警报。但是,我不明白将这段代码放在哪里。仅当用户点击通知或将其关闭时,我才需要取消重复警报。感谢您的帮助!

最佳答案

我相信 Notification.deleteIntent就是你要找的。医生说:

The intent to execute when the notification is explicitly dismissed by the user, either with the "Clear All" button or by swiping it away individually. This probably shouldn't be launching an activity since several of those will be sent at the same time.

关于android - 如何检测通知是否已被驳回?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12665131/

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