Reputation: 439
I have an Xcode project in Swift with the following targets:
In the common project I have the following code:
public class func scheduleNotification(seconds: Int) {
var notification = UILocalNotification()
notification.fireDate = NSDate().dateByAddingTimeInterval(NSTimeInterval(seconds))
notification.alertBody = "item"
notification.alertAction = "open"
notification.soundName = UILocalNotificationDefaultSoundName
notification.userInfo = ["UUID": "XX", ]
notification.category = "CATEGORY"
UIApplication.sharedApplication().scheduleLocalNotification(notification)
}
I'm able to call this method AND fire a notification from the iOS APP:
@IBAction func XXX(sender: AnyObject) {
NotificationHelper.scheduleNotification(100)
}
But the same code executed from the WatchKit Extension doesn't fire any notification. The method IS called but then nothing happens on the iPhone or on the Apple Watch, no notifications are fired.
Can someone help me?
How can I schedule notification from the WatchKit Extension logic?
Upvotes: 2
Views: 1734
Reputation: 18765
First of all run iOS
app from WatchKit
app:
NSDictionary *userInfo = @{@"text":@"Hello world!",
@"delay":@10.0};
[WKInterfaceController openParentApplication:userInfo reply:^(NSDictionary *replyInfo, NSError *error) {
// Completion
}];
Then handle on iOS
device:
- (void)application:(UIApplication *)application handleWatchKitExtensionRequest:(NSDictionary *)userInfo reply:(void (^)(NSDictionary *))reply
{
UIBackgroundTaskIdentifier tid = [application beginBackgroundTaskWithExpirationHandler:nil];
UILocalNotification *note = [[UILocalNotification alloc] init];
note.alertBody = userInfo[@"text"];
note.fireDate = [NSDate dateWithTimeIntervalSinceNow:[userInfo[@"delay"] doubleValue]];
note.timeZone = [NSTimeZone systemTimeZone];
note.userInfo = userInfo;
[application scheduleLocalNotification:note];
reply(nil); // Completion callback
[application endBackgroundTask:tid];
}
And of course do not forget to register iOS
app for notifications:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
[application registerUserNotificationSettings:[UIUserNotificationSettings settingsForTypes:(UIUserNotificationTypeAlert) categories:nil]];
return YES;
}
Upvotes: -1
Reputation: 366
I suppose you do NOT yet have the real device, but are using the simulator? Then you will probably not be able to send notifications to the Watch at all. This is because Apple has not (yet) added this feature to XCode.
To view and test your notification interface on the watch simulator, use a payload file as described here (paragraph "Specifying a Notification Payload for Testing"): https://developer.apple.com/library/ios/documentation/General/Conceptual/WatchKitProgrammingGuide/ConfiguringYourXcodeProject.html
And do not have much confidence in a change of that coming soon/anymore. As it is described somewhere in the Watch Documentation, iOS will decide itself where to send your notification, anyway (when you finally made it to the hardware release...)
Upvotes: 0
Reputation: 5939
App extensions are not allowed to access sharedApplication
. I'm guessing that sharedApplication
is returning nil
in your case, which would explain why the notification is not scheduled.
I'd suggest using something like openParentApplication:reply:
to open your host iOS app in the background and schedule the notification from there.
Upvotes: 3