小编Flo*_*n S的帖子

WatchOS、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 …
Run Code Online (Sandbox Code Playgroud)

watchkit watchos usernotifications swiftui

6
推荐指数
1
解决办法
1408
查看次数

标签 统计

swiftui ×1

usernotifications ×1

watchkit ×1

watchos ×1