触发 Apple Watch 通知

Firing apple watch notification

我有一个待办事项应用程序,我需要在预定的待办事项时间向手表显示通知。我该怎么做呢?我读到 iOS 本地通知会自动在手表中触发通知。可能吗?如果是,我是否需要额外添加任何代码才能这样做?

UILocalNotification 的概念在 WatchKit 中没有改变。唯一改变的是 iOS 现在可以决定是否向 iPhone 或手表发送本地(或远程)通知。但是,没有编程方式来控制这些设备中的哪一个实际显示通知。

要从 WatchKit 应用程序安排 UILocalNotification 需要在 WKInterfaceController 上使用 openParentApplication:reply: class 方法让 iPhone 应用程序执行实际调度。这是因为 WatchKit 无法访问 UIApplication,用于安排本地通知的 class。

在 watchOS 3 中,您不再需要使用 phone 来安排通知。

您现在可以使用新的 UserNotifications 框架直接在手表上安排本地通知。这些通知只会出现在手表上并由手表处理。

WWDC 2016 Introduction to Notifications session 介绍了如何使用不同类型的触发器来触发待办事项通知,例如:

  • 时间间隔触发器

    // In 2 minutes from now
    UNTimeIntervalNotificationTrigger(timeInterval: 120, repeats: false)
    
    // Repeat every hour starting now
    UNTimeIntervalNotificationTrigger(timeInterval: 3600, repeats: true)
    
  • 日历触发器

    let dateComponents = DateComponents()
    // Configure dateComponents
    UNCalendarNotificationTrigger(dateMatching: dateComponents, repeats: false)
    

session 还介绍了如何处理手表上的可操作通知。