Flo*_*n S 6 watchkit watchos usernotifications swiftui
创建 XCode 项目时,我选择了 Apple Watch Application 并启用了“包含通知”。这导致我的项目中有一个NotificationView.swift和一个NotificationController.swift。
我已经用我想要在本地通知中显示的视图内容填充了NotificationView.swift。
在我的常规 HostingController.swift 中,我现在想使用 NotificationView.swift 的内容安排本地通知。
到目前为止,我正在使用当前的代码:
let userNotificationCenter = UNUserNotificationCenter.current()
let notificationContent = UNMutableNotificationContent()
notificationContent.title = "title"
notificationContent.body = "body"
notificationContent.categoryIdentifier = "categoryNameDummy"
let category = UNNotificationCategory(identifier: "categoryNameDummy", actions: [], intentIdentifiers: [] as? [String] ?? [String](), options: .customDismissAction)
let categories = Set<AnyHashable>([category])
userNotificationCenter.setNotificationCategories(categories as? Set<UNNotificationCategory> ?? Set<UNNotificationCategory>())
let trigger = UNTimeIntervalNotificationTrigger(timeInterval: 2, repeats: false)
let request = UNNotificationRequest(identifier: UUID().uuidString, content: notificationContent, trigger: trigger)
userNotificationCenter.add(request) { (error) in
if let error = error {
debugPrint(error)
}
}
Run Code Online (Sandbox Code Playgroud)
我没有对 Info.plist 中有关 CategoryIdentifier 的内容进行任何更改。我必须添加什么代码以及在哪里添加代码才能现在“捕获”此通知并用我的自定义NotificationView.swift内容填充它?
您是否请求用户允许发送通知?
UNUserNotificationCenter.current().requestAuthorization(options: [.sound, .alert]) { success, error in
if success {
print("Ok!")
} else if let error = error {
print(error.localizedDescription)
}
}
Run Code Online (Sandbox Code Playgroud)
它仅在第一次时显示权限警报:一旦授予权限,它就不会再次显示。
归档时间: |
|
查看次数: |
1408 次 |
最近记录: |