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?
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.