Create Watchkit notification programmatically

642 Views Asked by At

I'm looking to push a notification to the Apple Watch at a specific time. I was able to display a static message on the simulator using an apns file, but I'd like to set the data and time of the notification in the controller.

I heard I might not be able to do this on a simulator, but if I got a real phone and watch, how would it work?

2

There are 2 best solutions below

0
On BEST ANSWER

So, you want to do something in the watch app extension and based on the results, schedule a UILocalNotification that will be sent to the phone at some point?

You cannot directly schedule a UILocalNotification from the watch because you don't have access to the UIApplication object. See Apple staff comment here.

However, using Watch Connectivity, you could send a message from the watch to the phone and have the phone app create and schedule it in the background. The watch will display the notification iff the phone is locked at the trigger time. The watch's notification scene will be invoked in that case.

2
On

Assuming you want to send the notification from the phone to the watch: You can use UILocalNotification to send a notification to the watch - but the watch must be locked to receive it. There's also no guarantee when the Watch OS will turn on the watch to run the code that receives your notification, so your notification may arrive minutes or hours after it's sent.