触发苹果手表通知

时间:2015-04-22 04:56:29

标签: ios notifications watchkit apple-watch

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

2 个答案:

答案 0 :(得分:2)

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

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

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

  • 时间间隔触发器

    // 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)
    

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

答案 1 :(得分:1)

使用WatchKit,UILocalNotification的概念没有改变。唯一改变的是iOS现在可以决定是向iPhone还是Watch发送本地(或远程)通知。但是,没有程序化的方法可以控制这些设备实际显示通知中的哪一个。

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

相关问题