【问题标题】:Local Notification in WatchKitWatchKit 中的本地通知
【发布时间】:2017-04-11 16:15:07
【问题描述】:

如何直接从 WatchKit 发送本地通知?甚至可能吗? Apple 文档说,从 Watch 安排的本地通知会直接发送到 Watch,因此似乎必须有办法做到这一点。

在 iOS 中我会这样做:
但是无法从 Watch 共享应用程序....

UILocalNotification *notification = [[UILocalNotification alloc] init];
                                           notification.fireDate = [NSDate dateWithTimeIntervalSinceNow:7];
                                           notification.alertBody = @"This is local notification!";
                                           notification.timeZone = [NSTimeZone defaultTimeZone];
                                           notification.soundName = UILocalNotificationDefaultSoundName;
                                           notification.applicationIconBadgeNumber = 10;
                                           NSDictionary *infoDict = [NSDictionary dictionaryWithObject:@"Hello! This is Local Notification!" forKey:@"Notification"];
                                           notification.userInfo = infoDict;
                                           [[UIApplication sharedApplication] scheduleLocalNotification:notification];

【问题讨论】:

  • 使用UserNotifications 框架

标签: ios objective-c watchkit apple-watch


【解决方案1】:

在使用最新 SDK 中的通知时,无需访问共享应用程序。从watchOS 3.0+开始,使用UNNotificationCenterUserNotifications框架的一部分)类的class func current()方法来获取对当前通知中心的引用,然后使用这个对象通过它提交notification requests

由于与在 iOS 中使用此类没有太大区别,请查看我的其他答案 Using local notifications in iOS。我自己知道的唯一存在的区别是,在 WatchKit 扩展中工作时,您只能触发向 Apple Watch 发送通知。而当从您的 iOS 应用程序中提交通知请求时,系统会决定它应该在哪里发送通知(请参阅apple watch notification documentation)。

【讨论】:

  • 这就是我的猜测。从 iOS 应用触发通知,我很困惑,因为下面的图表:i.stack.imgur.com/3yAsJ.png 显示来自扩展的通知将仅传递到手表。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-07-01
  • 2021-05-28
  • 1970-01-01
相关资源
最近更新 更多