在watchkit中接收本地通知

时间:2015-04-08 11:25:59

标签: 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 app的通知。我在watchkit扩展中的NotificationController.m中使用下面的方法 -

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

}

任何人都可以告诉我为什么我没有在watchkit App中收到本地通知。

提前致谢。

1 个答案:

答案 0 :(得分:4)

请参阅Apple Watch编程指南HERE

When one of your app’s local or remote notifications arrives on the user’s iPhone, iOS decides whether to display that notification on the iPhone or on the Apple Watch.

因此,您不必担心会在Watch上发送通知,iOS会为您执行此操作。

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

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

希望它会对你有所帮助。