NEW: Start my new Ultimate Portfolio App course with a free Hacking with Swift+ trial! >>

SOLVED: Local Notifications in watchOS 6 app.

Forums > watchOS

I have a watch application that shows (local) notifications to users at scheduled time. The notifications are "reminders" to the user like "Hey, task xyz is coming up in 5 minutes".... or something to that effect.

I have the notifications working properly and everything.

What I want to do now is, if the person marked a particular "Task xyz" as "completed" before the notification even fired, then I don't want to show that notification.

1 way is whenever the user marks "Task xyz - completed", I can use removeAllPendingNotificationRequests() and after that reschedule all the notifications based on my flag "is_complete?". This is not a really good way because I don't reschedule everything everytime 1 task gets completed.

What I really want to do is, when the scheduled notification's trigger time has come, I want to call a function that checks the "is_complete?" flag and if it is true, the notification isn't fired and otherwise, it is fired.

I found 2 functions:

  1. When the app isn’t running, i.e. it’s in the background or closed, the delegate function userNotificationCenter(_:didReceive:withCompletionHandler:) is called after the user taps on it.

  2. When the app is running and in the foreground, using the delegate function userNotificationCenter(_:willPresent:withCompletionHandler:) is only called when the app is in the foreground.

So, they don't help.

Is it possible to call a function or perform an action right before a notification is fired?


@twostraws can you help me out with this please ? I am really stuck and can't seem to get anywhere. Thanks.


There is no way to intercept a notification like you describe, but you can cancel a task's notification when the user marks it complete. Instead of removing all of them, use removePendingNotificationRequests(withIdentifiers:) and pass in the ID of the task's notification. You can either save the notification ID for each task, or use getPendingNotificationRequests(completionHandler:) to get the pending notifications and decide which ones to cancel.


Hacking with Swift is sponsored by Fernando Olivares

SPONSORED Would you describe yourself as knowledgeable, but struggling when you have to come up with your own code? Fernando Olivares has a new book containing iOS rules you can immediately apply to your coding habits to see dramatic improvements, while also teaching applied programming fundamentals seen in refactored code from published apps.

Try the book!

Sponsor Hacking with Swift and reach the world's largest Swift community!

Archived topic

This topic has been closed due to inactivity, so you can't reply. Please create a new topic if you need to.

All interactions here are governed by our code of conduct.

Unknown user

You are not logged in

Log in or create account

Link copied to your pasteboard.