1

I got an application where I have some local notification. And they work fine with trigger and all that, even when the app is in the background.

But right now I just have some static text in that notification, I then click on it and it open on the App and it makes api call to server where I then get the name back I need and content.

What I would like to do is that when the notification get triggered it make the Api call and get the content. So my notification instead can be write something like "This is X" and X get replaced with the content from the server.

It is with iBeacons I'm working with, so it is when I get near a iBeacon I get a notification.

Eric Aya
  • 69,473
  • 35
  • 181
  • 253
LinkToThePast
  • 232
  • 2
  • 12

2 Answers2

1

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
}
KlimczakM
  • 12,576
  • 11
  • 64
  • 83
  • Yes I have didEnterRegion, and that is where I make the notification right now. But from it I can't see what the major and minor of the iBeacon is that I need for my api call. The api call I do I got in didRangeBeacons. Is it possible to make it start that when didEnterregion get called and it is still in the background? – LinkToThePast Apr 18 '16 at 11:53
  • 1
    I just put the notification and that in the didrangebeacons, and it fine ran even when the app was in the background. – LinkToThePast Apr 19 '16 at 14:42
0

Use the beginBackgroundTaskWithExpirationHandler , to make the API call in the background. See this thread for detail info: Proper use of beginBackgroundTaskWithExpirationHandler. In this background task you can execute the API request and show the notification with the requested result.

Community
  • 1
  • 1
Peter Visser
  • 129
  • 9
  • So in the func doUpdate I should have my api call and all that just? And then call the doUpdate in my didEnterRegion? – LinkToThePast Apr 18 '16 at 11:59
  • Yes, the doUpdate function is where your code gets executed and your notification is sent. The name doUpdate is just for demo purposes, you can just call the background logic from within the didEnterRegion function. – Peter Visser Apr 18 '16 at 12:49