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:
- If your app is killed or never run, there is nothing you can do to get data.
- 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.
- 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
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)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.
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
Swift - Could Not Cast Value of Type 'Uitabbarcontroller'
How to Give Cornerradius for Uibezierpath
Uidocumentinteractioncontroller() Swift
Swift 3 Nscache Generic Parameter 'Keytype' Could Not Be Inferred
Cropping Avasset Video with Avfoundation Not Working iOS 8
How to Delete an Image from Photolibrary After I Pick It Up Using Uiimagepickercontroller
Show Posts from the Users You Are Following - Swift
Items in File Provider Extension for iOS 11
How to Set Kerning (Spacing Between Characters) on Uinavigationbar Title - Swift or Objective-C
How to Create a Custom iOS Keyboard
Alpha for Background Color Not Working in iOS
How to Update Constraints Programmatically
Uisearchcontroller Searchbar Delegate Not Working
How to Store Array in Nsuserdefault in Swift
Errortype' Is Not Convertible to 'Nserror'
Validate Jwt Token with Rs256 or Rs512 with Swift iOS
How to Pass in a Void Block to Objc_Setassociatedobject in Swift