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.

1      

Hacking with Swift is sponsored by RevenueCat

SPONSORED Building and maintaining in-app subscription infrastructure is hard. Luckily there's a better way. With RevenueCat, you can implement subscriptions for your app in hours, not months, so you can get back to building your app.

Try it for free

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

Reply to this topic…

You need to create an account or log in to reply.

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.