Reputation: 73
I develop an apple watch application. I want a simple behavior : when i press a button on iPhone on paired watch app i want to appear a notification. Is this possible? I use watch OS 2 and my watch app communicates perfect with iPhone app and vice versa via WatchConectivity. So using WCSession i can notify watch app but how can i display a controller in notification style, i mean as this
I saw that on controller that inherits from WKUserNotificationInterfaceController exist
- (void)didReceiveLocalNotification:(UILocalNotification *)localNotification withCompletion:(void (^)(WKUserNotificationInterfaceType))completionHandler {
How can i make this method to be called? I tried on watch create a UILocalNotification
UILocalNotification *notification = [[UILocalNotification alloc] init];
notification.category = @"myCategory";
notification.fireDate = [NSDate dateWithTimeIntervalSinceNow:8];
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];
The problem is that in watch doesn't exist UIApplication so i could't schedule the notification. I made the same thing on iOS application but in this case i got the notification on iPhone. I have to say that on watch app exists an NotificationInterfaceController with category name "myCategory".
Thanks!
Upvotes: 1
Views: 290
Reputation: 7451
UILocalNotification *notification = [[UILocalNotification alloc] init];
notification.category = @"myCategory";
notification.fireDate = [NSDate dateWithTimeIntervalSinceNow:8];
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];
Lock your iPhone.
Notification appear on your Apple Watch and iPhone.
didReceiveLocalNotification
on WatchApp is called.
POINT: You have to lock iPhone.
Upvotes: 3