作者:yangwei的马甲 | 来源:互联网 | 2023-01-31 10:20
我正在使用我的应用程序实现一种警报通知UserNotifications
.当3D触摸此通知时,我希望它显示一些"警报信息",以及"暂停"警报的选项,这将在5分钟内有效地重复通知.这一切都正常,但单击"暂停"按钮时通知不会被解除.
我使用a NotificationContentExtension
来修改内容,使用categoryIdentifier "ReminderNotificationExtensionCategory"
.然后我在代码中创建了我的类别,如下所示:
let snooze = UNNotificationAction(identifier: "snoozeActionIdentifier", title: NSLocalizedString("SNOOZE", comment: ""), options: .foreground)
let reminderCategory = UNNotificationCategory(identifier: "ReminderNotificationExtensionCategory", actions: [snooze], intentIdentifiers: [], options: .customDismissAction)
UNUserNotificationCenter.current().setNotificationCategories([reminderCategory])//Not sure if I should set this every time or just once..
然后我创建我的通知对象
let cOntent= UNMutableNotificationContent()
content.categoryIdentifier = "ReminderNotificationExtensionCategory"
content.title = "test"
content.body = "test"
let trigger = UNTimeIntervalNotificationTrigger(timeInterval: 3, repeats: false) //Fire in 3 seconds
let request = UNNotificationRequest(identifier: "someIdentifier, content: content, trigger: trigger)
UNUserNotificationCenter.current().add(request) { (error:Error?) in /**/}
现在,我的通知将在3秒后发送.我锁定屏幕并接收它,它看起来很棒.当我3D触摸它时,我的扩展程序启动了,UIView
我在设置的时候得到了一个潜行峰值,以及通知下方的"Snooze"按钮,如图所示:(内容已删除)
当我点击"打盹" -按钮,一个UNNotificationContentExtension
代表-方法将被调用,具体地func didReceive(_ response: completion:)
完成只接受的值UNNotificationContentExtensionResponseOption
,这将是任一.dismiss
,.doNotDismiss
或.dismissAndForwardAction
.仅仅为了测试,我做了这个:
func didReceive(_ response: UNNotificationResponse, completionHandler completion: @escaping (UNNotificationContentExtensionResponseOption) -> Void) {
print("This is happening. ActionIdentifier: ", response.actionIdentifier)
completion(.dismiss)
}
当我调试通知扩展时,打印确实发生了.打印出来"This is happening. ActionIdentifier: snoozeActionIdentifier"
.但是,通知没有被驳回.如果我从改变.dismiss
到.doNotDismiss
,没有任何改变.它仍然没有消失.如果我改为.dismissAndForwardAction
,它会解散并打开我的应用程序.
我找不到有这个问题的人.我想.dismiss
解雇我的通知.为什么不呢?难道我做错了什么?
编辑
我现在看到这两种情况正在发生:
我从锁定屏幕3D触摸我的通知,然后在它外面点击,我的通知'折叠',并且在3D触摸它之前可以看到常规通知.
我从锁定屏幕3D触摸我的通知,点击"暂停"(没有任何反应),然后点击通知外部,常规通知消失.
因此,完成回调实际上会解除常规通知,但不会解除扩展.为什么?我如何解雇延期?这是我必须打电话的手动方法吗?
1> Sti..:
我现在看到了我的愚蠢错误..
正如您在上面的代码中所看到的,我已经在我的类别中实例化了操作按钮:
let snooze = UNNotificationAction(identifier: "snoozeActionIdentifier", title: NSLocalizedString("SNOOZE", comment: ""), options: .foreground)
最后,我指定了选项.foreground
.[]
为了我的目的,这应该只是一个空数组.
我以为我必须指定一个选项,输入类型是UNNotificationActionOptions
,只有选项.authenticationRequired
("不,我不想要那个").destructive
("不,我不想要一个红色按钮")和.foreground
("井" ,如果我必须选择一个,如果这些..").事实证明,只需指定一个空数组即可[]
.