代码之家  ›  专栏  ›  技术社区  ›  Sti

如何通过自定义操作消除3D触摸通知?

  •  4
  • Sti  · 技术社区  · 7 年前

    UserNotifications 。当3D触摸此通知时,我希望它显示一些“警报信息”,以及“暂停”警报的选项,这将在5分钟内有效地重复通知。 这一切都很好,只是当我单击“打盹”按钮时,通知没有被取消。

    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秒内发送。 UIView Notification

    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" 。然而,通知不会被驳回。 如果我从 .不要解雇 .dismissAndForwardAction ,它会关闭并打开我的应用程序。

    我找不到有这个问题的人。我想要 不予考虑

    我现在看到这两种情况正在发生:

    1. 我在锁屏上用3D触摸我的通知,点击“Snooze”(什么都没发生),然后点击通知外,常规通知就会消失。

    1 回复  |  直到 7 年前
        1
  •  8
  •   Sti    6 年前

    我现在明白我愚蠢的错误了。。

    正如您在我上面的代码中所看到的,我在我的类别中实例化了动作按钮:

    let snooze = UNNotificationAction(identifier: "snoozeActionIdentifier", title: NSLocalizedString("SNOOZE", comment: ""), options: .foreground)
    

    最后,我指定了选项 .foreground 。这应该只是一个空数组 [] 为了我的目的。

    选项,并且输入类型为 UNNotificationActionOptions .authenticationRequired (“不,我不想那样”) .destructive (“不,我不想要红色按钮”)和 .前景 如果这些..”)。结果表明,只需指定一个空数组 成功了。