BLACK FRIDAY SALE: Save big on all my Swift books and bundles! >>

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?

1      

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

1      

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.

2      

hello mr harshittrehan
can u tell me how u send local notifications in watchOS, what u ues to do this thank u

1      

Hacking with Swift is sponsored by RevenueCat

SPONSORED In-app subscriptions are a pain to implement, hard to test, and full of edge cases. RevenueCat makes it straightforward and reliable so you can get back to building your app. Oh, and it's free if your app makes less than $10k/mo.

Learn more

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.