How to Trigger an API Call Before Displaying Local Notifications

Is it possible to trigger an api call before displaying local notifications?

You cannot guarantee anything, but you could implement background fetch and content-available pushes and have as fresh data as possible.

There's a good write-up of the techniques here: https://layer.com/how-we-leverage-ios-push-notifications/

Basically:

  1. If your app is killed or never run, there is nothing you can do to get data.
  2. If your app has been run, you could implement background fetches and then you get 30-second windows at non-deterministic times to get as up to date as you can.
  3. The server can send empty notifications with the content-available flag to try to trigger background fetches. If your data payloads are small, it can send that in the notification.

You cannot schedule anything to happen at a specific time. The best you can do with local notifications is perhaps implement a custom view of it (iOS 10 feature) -- and in that view go get fresh data. You are in an extension when you do this, so it's not trivial.

local notification with API call

So I assume that you have some iBeacon manager which calls didRangeBeacons:

func locationManager(manager: CLLocationManager, didRangeBeacons clBeacons: [AnyObject], inRegion region: CLBeaconRegion) {
// schedule local notification
}

and you schedule a local notification in there. All you need to do is to make a server call first and schedule local notification once it's finished.

You need to remember, that your application can be in the background (or even killed) when the user enters the beacon region so you need to start ranging when didEnterRegion gets called:

func locationManager(manager: CLLocationManager, didEnterRegion region: CLRegion) {
// start ranging for beacons
}

how to call a web service daily on particular time using local notification in objective c

you cant schedule something like this offline but you have 2 options that differ

  1. with thr app background refresh API you may come close. If you opt in to that api, ios will wakeup your app when it has spare cycles and will give you cpu time to run some code and allow you to do this.


    the background refresh api was meant for 'periodic' updates like this IMO. What you cannot do with it though, is schedule any EXACT times/dates/intervals you want to be woken. You can recommend times to ios but it may or may not stick to the plan (this depends on device use .... battery .... time of day...... etc)

  2. another option are a backend that sends 'silent' push notifications at your required time. IOS would wake your app for those notifications and as they are silent, the user wont see it.

  3. you can have a backend send you non silent pushes. Your app will be launched on tapping the notification and you can do whatever you like

==> option 1 works offline, option 2 and 3 require connectivity and even worse a decdicated backend to support it. IMHO option 1 is often very good and underrated.

How do I schedule local notifications to fire once when the app opens without having repeats when the user reopens it? (Swift, XCode, IOS)

Assuming ViewController is the first and rootViewcontroller you can just remove all local notifications before adding new ones.

UNUserNotificationCenter.current().removeAllPendingNotificationRequests()


Related Topics



Leave a reply



Submit