UPGRADE YOUR SKILLS: Learn advanced Swift and SwiftUI on Hacking with Swift+! >>

SOLVED: Handle push notifications as local notifications.

Forums > iOS

I am designing an application and I send a push notification payload to iOS app everyday at midnight.

The payload contains 3-4 different times which I want to use as 'triggers' for 3-4 different local notifications, being fired throughout the day.

Is there a way to silent the remote notification completely, and then use the remote notification's payload to parse that JSON and schedule a few local notifications from the payload data ?

3      

Yes this behaviour is supported by iOS and you dont even need user's permission to receive silent notification - https://developer.apple.com/documentation/usernotifications/setting_up_a_remote_notification_server/pushing_background_updates_to_your_app

4      

@nemecek-filip Thanks for sharing the link. An additional query I have is that it says "You have 30 seconds to process the payload.... after that serviceExtensionTimeWillExpire() is called".

My end-goal is: Once I receive the paylaod from my server, the payload has, say, 6 different times for that day. So that means I need to send local notifications 6 times in that day. Now I need to process the payload, parse the JSON, extract the time and schedule 6 local notifications.

What if I am not able to do that in 30 seconds ? Is there a way to increase this 30 seconds limit? If not, is there a workaround for that ? Potentially, I think storing the remote payload in a JSON file would help, maybe ?

3      

30 seconds is a lot of time. I would be surprised if your JSON parsing and notifications scheduling took more than 1 second

4      

Thanks a lot for your help! I will try it out and see how it works out.

One last thing, is it possible to perform some sort of check before actually displaying a scheduled local notification? Like if a user has a "Goal" that starts at 10:30 AM (local notification scheduled for 10:25 AM). But the user actually completes that "Goal" at 10 AM, then I don't want to show the notification at 10:25 AM obviously.

One way is to use removeAllPendingNotificationRequests() everytime user "Completes a Goal" and use a "is_complete" flag to reschedule all local notifications. This is not very efficient obv.

I found only 2 other functions dealing with notification displays:

  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.

Clearly, none of these help me.

Any ideas? Do I really have only removeAllPendingNotificationRequests() as my option ?

3      

You can remove specific scheduled notifications based on their identifier. So when user completes the goal you would use notifications center to remove just that one specific notification from the queue.

4      

Ah, got it! Thanks a lot for the help. Really appreciate it. I was kinda lost for a few days now but now eveything seems finally clear!

3      

Hacking with Swift is sponsored by RevenueCat

SPONSORED Take the pain out of configuring and testing your paywalls. RevenueCat's Paywalls allow you to remotely configure your entire paywall view without any code changes or app updates.

Learn more here

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.