【发布时间】: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