ios - 在 watchkit 中接收本地通知

标签 ios uilocalnotification watchkit apple-watch ios-extensions

我已经设置本地通知如下:

-(void)startLocalNotification 
{

    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];
}

我在 iPhone 模拟器上收到了通知。但它不会触发 watchkit 应用程序的通知。我在 watchkit 扩展中的 NotificationController.m 中使用以下方法 -

- (void)didReceiveLocalNotification:(UILocalNotification *)localNotification withCompletion:(void (^)(WKUserNotificationInterfaceType))completionHandler
{
     NSLog(@"Notification Received ..");
    completionHandler(WKUserNotificationInterfaceTypeCustom);

}

谁能告诉我为什么我在 watchkit 应用程序中收不到本地通知。

提前致谢。

最佳答案

参见 Apple Watch 编程指南 HERE

当您应用的本地或远程通知之一到达用户的 iPhone 时,iOS 会决定是在 iPhone 上还是在 Apple Watch 上显示该通知。

因此您无需担心在 Watch 上发送通知,iOS 会为您完成。

无论如何,如果你只想在 watch 上发送通知,你可以使用 Darwin Notification Concepts ,但请记住存在一些限制,您可以查看给定的链接。

您还可以使用 shared container用于应用程序和扩展程序之间的通信。

希望对你有所帮助。

关于ios - 在 watchkit 中接收本地通知,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29513301/

相关文章:

iOS Typhoon DI 框架替代 objective-c 中的单例

ios - 本地通知未触发

firebase - Unity 的本地通知(Firebase?)

ios - WKInterfaceMap 禁用触发原生 Apple Watch map

ios - Apple Watch - 如果我想自定义远程通知的发送方式,是否需要添加 watchkit 扩展?

ios - WatchKit WKInterfaceMovie fatal error : unexpectedly found nil while unwrapping an Optional value

objective-c - iOS 录制音频并存储在播放列表中

ios - 默认今日小部件中的布局约束冲突

iphone - 我的 Default.png 使用什么样的图像?我正在使用 iPhone 5's native resolution WITH the status bar, but it won' t 工作

ios - UILocalNotification iOS8 handleActionWithIdentifier 从未调用过