Present local notification when NSPersistentCloudKitContainer changes - ios

I use NSPersistentCloudKitContainer for sharing data between users, everything seems to be working fine, my question is how to show a notification to a user that there was a change in shared content even when the app is terminated.
I found that the app receives a silent remote push notification each time there is a change (by adding didReceiveRemoteNotification function to the AppDelegate), but I'm not sure if I'm safe to use it as a trigger for presenting local notifications as I did not find any documentation regarding this. Also, it's not clear how to get any info from the remote push about the change, for example, what type of data was changed and who did that change.

Related

Do Push Notifications with content-available work for users that have disabled Push Notifications?

I'm looking into reliable ways of updating an iOS app with content of critical importance, when instructed to do so by a server.
This would have to trigger regardless of the state of the app (foreground, background, not running, etc). Most sources seem to indicate that Push Notifications with the content-available: 1parameter are able of doing this.
My question is, what happens if the user either presses Cancel when prompted to allow the app to receive push notifications, or turns them off afterwards ? Is he only not going to receive alerts anymore, or will the push notifications be entirely ignored (including the content update) ? And if so, is there any other iOS feature that would reliably allow me to update the app's content (regardless of app state and without the user being able to disable it) ?
This is about iOS 7 and above.
If user disallows the push notifications , you can not send them the notifications forcefully.
What kind of data you want to update afterwards?
Most of the data can be changed dynamically by web services...

Notification - when app is killed

I have implemented AWS SNS push notification service.
We have an issue which is explained below :
Scenario :
We are trying to save the notification message we receive as a part of requirement.
When the app is killed (swipe out from recent apps) we are able to receive notification, also we are able to save the notification message when we open the message directly from the notification panel it works fine,but when we open the app directly the notification message is not getting saved.
In short we are not able to know if we had received a notification message if we directly open the app rather than clicking the message from the notification panel.
Is this default behavior ? or is there any work around for this ?
Have looked into many posts on about Push Notifications but haven't seen any threads pointing to this scenario.
This is a normal behavior, and there is no workaround.
If your app is killed by the user, it won't be able to run ANY code before it's manually launched again.
If it's manually launched from a notification, this notification's payload will be handled by your AppDelegate.
On top of that, don't forget that Push notifications are Best Effort. That means that they are not reliable, they can be heavily delayed or never delivered at all. Don't rely on Push notifications to achieve any critical work.
If you need to keep a copy of your notifications in-app, keep them server side and fetch them as you would do with any other object.
In order to execute code when the app is killed by the user you need to implement VOIP using PushKit framework provided by apple.
VOIP push unlike regular push notification enables the app to become active even if the app is killed by user.

Unable to get push notification data when app hasn't yet been launch

I implemented the support for push notification and it is working like a charm. However, in one particular scenario, it doesn't give me the desired result.
When the app is running (state is either active or is in background), whenever a push notification is received, the function "didReceiveRemoteNotification" is called. BUT, when the app is terminated and a push notification is received, I believe "didReceiveRemoteNotification" NEVER gets called (can't test this scenario using Xcode as app hasn't been launched yet). This prevents me from loading a particular view controller to the user.
Can someone please tell me how I can get the data corresponding to the push notification when received while the app hasn't been launched yet? Is there some other function that I need to look into for this particular scenario ONLY?
Appreciate some help.
Per the documentation:
If the app is not running when a push notification arrives, the method
launches the app and provides the appropriate information in the
launch options dictionary. The app does not call this method to handle
that push notification. Instead, your implementation of the
application:willFinishLaunchingWithOptions: or
application:didFinishLaunchingWithOptions: method needs to get the
push notification payload data and respond appropriately.
In particular, the key you should be looking for in the launch options dictionary is UIApplicationLaunchOptionsRemoteNotificationKey.

How to show a local notification alert or a local notification banner when App is closed

How to show a local notification alert or a local notification banner when App is closed (either in the background or foreground), I can only modify the badge number when the app is closed...
You will not be able to present notifications while the app is in the background unless you implement Push Notifications with APNS and use UIRemoteNotification. Based on your experience and the scope of the project, it may be a bit complicated, but here is a good reference on getting started:
http://www.raywenderlich.com/32960/apple-push-notification-services-in-ios-6-tutorial-part-1
As of the writing of this answer, using iOS 8.4 (soon iOS9), you CAN
Display UILocalNotifications when the app is closed
Display push notifications whenever you want.
If your local notification is scheduled, it WILL fire, wheter or not the app is foreground/background/killed.
So now, your problem is this one : How do I schedule a local notification when the app is forground/background/kill ?
It's easy, in the Appdelegate, you can simply follow any tutorial and type your code there, it's pretty straightforward. The only different thing is, if the app is killed, you can't execute code unless the app is awaken with a push notification. And that's where the remote push notifications are useful, you can awaken your app (and execute code) with a push notification and do whatever you need from here on out.
You could :
- Display the content you want with the actual push notification. " You have 1 new message! "
- Send a "silent" push notification (invisible by the user) that still awakens your app, and execute code there, for example to update data or modify stuff in your app in general. If I'm not mistaken you're only allowed 15 minutes of work and a certain amount of data transfer when the app is awaken like that, but that has to be verified.
I hope I helped :)

iOS execute check before push notification is received

I am writing an application that utilizes Apple's Push Notification Service. Some of the push notifications are based on the users location and should only be delivered if the user is a certain distance from an object. I don't want to continually update the user's position to my server and do the check that way, first, because of security reasons and second, to cut down on the network usage. Is there a way, when the push notification is received by the device, to do a check before the user is notified, and if it doesn't meet the criteria, discard the notification? Thanks for the help!
Nope, sadly you can't execute any code on the client side without the user clicking on the notification when the app is not launched. You'll have to do your check server-side to decide wether or not sending a push.
Push notifications that are received while you app is not running (the most typical case) is outside your control. Once they are sent, they will be received and shown to the user (assuming the user has granted permissions)
You do have control over push notifications if they are received while your app is running.
Maybe you could use local notifications (notifications that are generated and received from user's device) instead. You would have full control over when they are generated.
Bear in mind, background processing in apps is disallowed except for 4 things
Location tracking (You can subscribe to trigger code when user changes geolocation)
Alarms
Playing music
Voice over IP
Anything outside these cannot be executed in the background.
Starting with iOS7, your code can be run, I quote, "roughly the same time that the user sees the notification":
Multitasking Enhancements
Apps that use push notifications to notify the user that new content
is available can fetch the content in the background. To support this
mode, include the UIBackgroundModes key with the remote-notification
value in your app’s Info.plist file. You must also implement the
application:didReceiveRemoteNotification:fetchCompletionHandler:
method in your app delegate.
See more info here:
https://developer.apple.com/library/ios/releasenotes/General/WhatsNewIniOS/Articles/iOS7.html#//apple_ref/doc/uid/TP40013162-SW10
and here:
https://developer.apple.com/library/ios/documentation/iPhone/Conceptual/iPhoneOSProgrammingGuide/BackgroundExecution/BackgroundExecution.html#//apple_ref/doc/uid/TP40007072-CH4-SW57
I haven't tried it yet but I wonder if you send a push notification payload which doesn't have the standard "alert", "badge" values in "apns" but in a custom value, it would still call your code but without displaying the push notification.

Resources