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

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 ?

   

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

1      

@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 ?

   

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

1      

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 ?

   

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.

1      

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!

   

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.