watchOS Local Notification Will Not Fire More Than Once

A watchOS Local NotificationI seem to have come across a vexing issue with the watchOS Local Notification API – I can not get them to trigger reliably using a time interval that uses a common notification identifier.

I have been trying to create an application that will fire off a local notification on the watch even if the device is away from its paired iPhone. However, using the UNUserNotifications framework in watchOS 3, the local notification will never fire more than once or twice, even if scheduled again in code.

For example, if you were creating a Pomodoro App to alert you when your 25 minutes is over you would expect to be able to schedule another 25-minute notification.

The code below is a very basic version of this. Tapping the ‘Start Notifications’ button will set a local notification to fire off after 15 seconds. You should be able to request another one to fire off 15 secs later by tapping the ‘Another’ button in the alert. However, the third or fourth notification never fires (I have sometimes had a second one fire, oddly).

ExtensionDelegate.swift

InterfaceControler

I’m not sure if I have missed something within the framework – if you can see the issue please do leave a comment.

However, I have found a workaround, by including a random identifier to each watchOS local notification request. In a production App this may need to be a counter of some sort to deal with the issue of a duplicate number randomly being drawn in close succession. For my testing purpose and I chose the random number route.

The code is available on GitHub if you want to play around with it Swift3 Local Notification Test. If another comes up with a better approach I’d be very interested in hearing from you.

Posted in Swift

Leave a Reply

Your email address will not be published. Required fields are marked *

*