I have developed a small iBeacon based application, when the application detects one of our iBeacons makes a call to a web service to obtain a data set and send a local notification to the user. All this is working correctly.
I have now raised the idea that these local notifications could vary over short time intervals, with new content. The problem is that if the user does not leave the region of the iBeacon and reenters, the application will not "wake up" and the user will not receive the new updated notification.
I do not know if this could be solved somehow or actually the approach to make something like that should not be related to technology of iBeacons.
I'm really lost and do not know if anyone would know advise me on how to raise it.
You can schedule local notifications to be delivered at a specific time: use the fireDate and timeZone properties of the UILocalNotification, and then use scheduleLocalNotification instead of the presentLocalNotificationNow method of the UIApplication.
With that in mind you could do this: upon an "enter" event, retrieve a few notifications to be showed to the user over a certain time period while they remain in the zone, schedule them appropriately and let iOS do the delivering.
If the user exits the zone before all the notifications are shown, you can cancel the remaining local notifications using cancelLocalNotification or cancelAllLocalNotifications methods.
In order to do this, you need to get the app to run in the background while beacons are in the vicinity. This would allow you to periodically check for updated content associated with the beacon and then display notifications under two conditions:
When a beacon first appears, and there is a message associated with that beacon.
The message associated with the beacon above changes during the time the beacon is still visible.
As you mention, the second item is a problem, because you need a way to continually check to see if there is updated content despite the fact that iOS will suspend your app within 5 seconds of beacon detection in the background.
A few options, each of which have downsides:
You can use a custom hardware beacon that changes its identifier every minutes or so (e.g. the minor goes back and forth between 0 and 1). This would allow you to monitor two regions and re-trigger on each every minute the beacon is in the vicinity. Downside: This requires building a custom beacon.
You can make your app request an extra 3 minutes of background running time during which you can check for changed messages. Downside: You only get three minutes to display changed messages.
You can specify extra background modes in your .plist so you can continue running in the background. Downside: Apple won't approve your app for distribution in the store unless you have a good reason to run in the background (e.g. a navigation app or a music player app.)
You can send a push notification to your app each time the message changes, which would wake up your app in the background so you could display an updated notification if a beacon is in the vicinity. Downside: Setting up push notifications are a bit complex, delivery can be slow, and is not guaranteed.
Read here for more info on some of these options: http://developer.radiusnetworks.com/2014/11/13/extending-background-ranging-on-ios.html
Related
To get the device settings time change notification, i found that we have to use UIApplication.significantTimeChangeNotification notification and func applicationSignificantTimeChange(_ application: UIApplication) delegate method.
But in my case even though if app in background or in killed state, then i have to show local notification to the user saying that device time is changed. Is it possible, Please let me know if we have any solution for this.
From Apple Document on applicationSignificantTimeChange
If your app is currently suspended, this message is queued until your
app returns to the foreground, at which point it is delivered. If
multiple time changes occur, only the most recent one is delivered.
So, it clearly conveys that this will work only when the app is in Foreground. It is not even queued up when the app is not running.
To display any local notifications when the app is not running, it should be scheduled in the past to trigger it in the future. And it is not possible for the app to know about the time change event when it is not running. It looks like you would not be able to achieve what you have asked.
I don't believe it is. iOS cannot deliver notifications to an app that is not running and they don't get queued up anywhere.
One potential workaround for this however could be to take the current UTC value from a trusted, online source and compare it with what the device thinks UTC is and store that offset. Once your app restarts, calculate this offset again and if it's significantly different from the one you stored previously, take the action you'd take in response to the Significant Time Change notification.
Is there a way to schedule a function which calls a web service at a particular time on a daily basis?
According to my research, I will have to use local notifications or NSTimer to schedule or call the function at a specific time.
But I need this to work whether my application is in the background at the schedule time (and not running any background task), or in the foreground.
There are two cases:
1. Location service is running in the background in my application
2. No location or background service is running, or the app is in the suspended state (if the user disabled location updates)
But I have to send some status to my server at the scheduled time daily.
You have the following options.
Silent Notification : A silent push notification is pushed from the server, indicating that now is the time, you need to call the webservice and further do the necessary tasks. You will have around 30 seconds to complete the task. Now notice that during this it is not necessary that your app is opened, as this will be done in the background, without your app not opening.
Follow
this
link here to configure your project with silent notification
Location based (Geofencing / Location Change) : Well you manage your app by giving it a location, that whenever it enters/ exists / location is updated near to / on that particular location, the app even if terminated, will be launched in the background.
applicationDidBeginLaunchingWithOptions will be called with a
specific key notifying that this app was launched from a location
change.
Follow this
link to configure your app accordingly. Now the caveat here is this
wont depend on time, but rather a particular location or locations,
whichever suits you.
UILocalNotification or Push Notification : But in both cases, its no guarantee that user will interact with the notification banner and open the application in the first place.
Hence in the first option, you can do the work without the user interacting with the device (Device has to be connected to the internet) on Time based and Location based changes and in the second option User has to interact with the device (notification banner). Please choose the one best for your requirement.
Im developing an app and I want to have a feature that sends push notification when user leaves predefined region. For tracking the process of leaving Im gonna use CLCircularRegion and Parse for notifications. The question is - can the app execute methods of sending push when it is in background state? I know that there is a permission available, which allows to monitor location update always and I also know about delegate, which can be used on location update.
The actual question is does it really work when app in background state? The book iOS 8 Swift Programming Cookbook says that location delegate messages are not delivered normally to application, but instead they are delivered in a batch when application again becomes the foreground application. Otherwise, the Apple documentation says that the app becomes awake when location changes.
Say, user terminated app day before and on the next day he left region defined in CLCircularRegion. Will delegate trigger and fire up the method of sending push?
Region monitoring will trigger updates even if the app is in the background. Your app could respond to the region monitoring callbacks didEnterRegion:, didExitRegion: to then trigger the desired push notifications as appropriate.
Per Apple:
In iOS, regions associated with your app are tracked at all times, including when the app isn’t running. If a region boundary is crossed while an app isn’t running, that app is relaunched into the background to handle the event. Similarly, if the app is suspended when the event occurs, it’s woken up and given a short amount of time (around 10 seconds) to handle the event. When necessary, an app can request more background execution time using the beginBackgroundTaskWithExpirationHandler: method of the UIApplication class.
More info here: https://developer.apple.com/library/ios/documentation/UserExperience/Conceptual/LocationAwarenessPG/RegionMonitoring/RegionMonitoring.html#//apple_ref/doc/uid/TP40009497-CH9-SW1
I have 8 medicine local notifications which differ from day to day.
They work fine for one day but I want to fire them everyday. I used BackgroundFetch to reschedule the local notifications every time fetch is executed. But my problem here is that background fetch depends on how often the user uses the app. What if the user doesn't open the app more often Also I didn't want to implement silent notifications because it will not wake up the app if the user does not have internet connection. what approach should I use instead of background fetch?
EDIT:
I also thought about location updates in background because my notification times are taken from location of user and calculated accordingly. But will this consume a lot of battery?
Since I've got the same issue in an
app that probably does the same stuff as yours, I'd like to share my solution.
It comes with one compromise works only from >=iOS8.
By using an interactive notifications you can reschedule your notifications in background, of course the user need to interact with the notification, but I think that if you different actions instead of open the app or cancel the notification is possible to have more interested user.
It's all about creating a configuration with actions.
Here you can find a tutorial.
User has an iOS app in his device and he walks into a physical store. I would like to identify that user programmatically within the app and throw him push notifications like Welcome message and based on his usage history or his level, send him coupons etc., on entering the store. He has to receive these messages automatically. In what all ways this is possible? And most important thing is that this needs to work in all states of the app (foreground, background, not running, suspended). Appreciate any quick inputs here.
Since you tagged your question with iBeacon, here's a summary of what's possible with that technology. In general, you can do what you want, but there are some caveats.
If you install an iBeacon in the store and make the app look for that iBeacon using [locationManager startMonitoringForRegion: iBeaconRegion], your app will get a callback whenever that beacon is first seen. In this callback, you can make it send a local notification to the user (or by making a web service call, you can make it send a push notification to the user). This can happen in all of the app states you mention, using this definition of these app states.
The situations where this will not work is if:
The phone is turned off
Bluetooth is turned off
The app has never been launched since installation
The user has stopped the app from the task manager and has not restarted it since then
The user has denied the app permission to access your location
The callback to your app will happen as soon as the iBeacon is detected by the phone. This may be as far as 150 feet from the iBeacon if it is detected quickly. It is also possible that detection will be delayed by a few minutes until iOS enters its next Bluetooth LE scan cycle. Once the callback takes place, local notifications can take place immediately. Push notifications will probably have a delay of several minutes more.
The message you send to the user will show up on the lock screen, and if the user gestures to it, you can launch a specific part of the app.