【问题标题】:Is UNUserNotification have option to show notification only on apple watch?UNUserNotification 是否可以选择仅在 Apple Watch 上显示通知?
【发布时间】:2017-06-19 09:03:27
【问题描述】:

我正在开发 Apple Watch 应用程序,该应用程序具有在每 2、3 或 5 分钟后向用户显示通知的功能。

我已经通过 iOS UNUserNotification 实现了这个功能,并且它运行良好。

但我想要的是用户只能在 Apple Watch 上看到所有通知,而不是在 iPhone 上。

现在它同时在 iPhone 和 Apple watch 上显示。

是否可以只在 Apple Watch 上显示通知?

我已经为UserNotification 实现了以下代码。

func scheduleNotification(at date: Date) {

        
        let trigger = UNTimeIntervalNotificationTrigger(timeInterval: (1*60), repeats: true)
        
        let content = UNMutableNotificationContent()
        content.title = "Test Reminder"
        content.body = "Show More detail in Body!"
        content.sound = UNNotificationSound.default()
        content.categoryIdentifier = "myCategory"
        
        if let path = Bundle.main.path(forResource: "logo", ofType: "png") {
            let url = URL(fileURLWithPath: path)
            
            do {
                let attachment = try UNNotificationAttachment(identifier: "logo", url: url, options: nil)
                content.attachments = [attachment]
            } catch {
                print("The attachment was not loaded.")
            }
        }
        
        let request = UNNotificationRequest(identifier: "textNotification", content: content, trigger: trigger)
        
        UNUserNotificationCenter.current().delegate = self
        UNUserNotificationCenter.current().removeAllPendingNotificationRequests()
        UNUserNotificationCenter.current().add(request) {(error) in
            if let error = error {
                print("Uh oh! We had an error: \(error)")
            }
        }
    }

提前致谢!

【问题讨论】:

    标签: ios swift apple-watch


    【解决方案1】:

    不,这是不可能的。 系统会自动决定在哪里显示通知,开发人员无法控制这一点。 如果配对的 iPhone 被锁定并且用户佩戴 Watch,则通知将显示在 Apple Watch 上。在任何其他情况下,通知都会显示在 iPhone 上。

    Notifications on your Apple Watch

    【讨论】:

      【解决方案2】:

      是的,如果您从 WatchKit Extension 触发 Local 通知,您可以让通知仅显示在手表上。 请查看对应文档:https://developer.apple.com/documentation/watchkit/notifications/taking_advantage_of_notification_forwarding?changes=__5

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多