Florin Lazau
Florin Lazau

Reputation: 73

Watch Kit notification from IOS App

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

enter image description here

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

Answers (1)

Kosuke Ogawa
Kosuke Ogawa

Reputation: 7451

  1. Create a UILocalNotification on your iPhone

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

  1. Lock your iPhone.

  2. Notification appear on your Apple Watch and iPhone.

  3. didReceiveLocalNotification on WatchApp is called.

POINT: You have to lock iPhone.

Upvotes: 3

Related Questions