I want to perform a simple task in my app when a local user notification is triggered while the app is not in the foreground. Can I do this?
I can't use UNNotificationAction, because I don't want to rely on the user to do anything. And it seems I can't use UNUserNotificationCenterDelegate because it only has a method to handle notifications while the app is in foreground.
I have came to conclusion that what I asked is not possible. Here is my alternative solution: I wanted to perform an action within my app when a location based user notification was fired. To accomplish what I needed, I set up the monitoring for my CLCircularRegion. The delegate for CLLocationManager launches the app in the background upon entry to the region, and I can do what I want inside delegate method.
Related
I am working with an iOS app using UserNotifications.
When the user taps on a notification, this method is fired:
userNotificationCenter(_:didReceive:withCompletionHandler:)
I want to know if there is a method called when the notification arrives, independently of when the user reacts.
Yes you can : https://developer.apple.com/documentation/usernotifications/unusernotificationcenterdelegate/1649518-usernotificationcenter
Of course the app has to be open for that. you can not execute code if the app is closed.
In some cases you can execute code if the app is in background but that required special authorisation.
I've been working with notifications for quite a while now, and I could've sworn that userNotificationCenter(_:didReceive:withCompletionHandler:) used to only be processed if the app was opened via the notification.
However, upon looking into actionable notifications, I found this:
When the user selects an action, the system launches your app in the background and notifies the shared UNUserNotificationCenter object, which notifies its delegate. Use your delegate object's userNotificationCenter(_:didReceive:withCompletionHandler:) method to identify the selected action and provide an appropriate response.
Does this mean that the seemingly universal didReceive delegate method now works even while the device is locked?
Or must the app always be opened to actually do anything meaningful in response to notification actions (writing to local database, sending HTTPS requests, etc.)?
Yes, the delegate works even if the device is locked. The app is only woken up if the user chose an action. Also, I am not sure how much time you will get before you invoke the completion block.
Important
If your response to action involves accessing files on disk,
consider a different approach. Users can respond to actions while the
device is locked, which would make files encrypted with the complete
option unavailable to your app. If that happens, you may need to save
changes temporarily and integrate them into your app's data structures
later.
From this Delegate Method : userNotificationCenter(_:didReceive:withCompletionHandler:) notification is works when your device is locked, but if you want to perform any action.
For Example: Suppose through Notification you want to pick any phone call within application then you have to setup another things as well.
you have to set up PushKit Framework within your application . As shown in images:
I'm building an app and I have created a method to detect if the user has new pictures in his photo librery throw ALAssetsLibrary, the method works fine.
I want to run this method from time to time to check for new pictures. If the user has new pictures, to notify him by a notificaiton.
I'm searching for a trigger to activate this method from time to time while the app is in background state or not running at all.
I know by sending a silent notification in ios 7, I can activate the app on the background state.
Once a silent notification is received , can I activate the method somehow? or the silent notoficaiton is just for receiving data and update the app.
Do you think of any other way that I can activate this function without the user will know about it ?
Not Possible currently!
1- When a silent notification is received the app is only intended to upload/download data hence we can only work with NSURLSessionTask. Access to other API's which require asynchronous blocks to execute like ALAssetLibrary is not available.
2- No other option of scanning library without the user knowing. You can display a notification to User and then on User's discretion app can be launched to perform desired scanning.
I'm working with UILocalNotifications for the first time. Mostly working with repeating notifications. Most all makes sense, except one thing.
Apple Documentation states several cases for handling local notifications when they fire.
First, a case for when the user "taps the notification" when outside of the App:
If the notification is an alert and the user taps the action button
(or, if the device is locked, drags open the action slider), the
application is launched. In the
application:didFinishLaunchingWithOptions: method the application
delegate can obtain the UILocalNotification object from the passed-in
options dictionary by using the
UIApplicationLaunchOptionsLocalNotificationKey key. The delegate can
inspect the properties of the notification and, if the notification
includes custom data in its userInfo dictionary, it can access that
data and process it accordingly.
It also states a case for what happens when the user is inside the App:
If the application is foremost and visible when the system delivers
the notification, no alert is shown, no icon is badged, and no sound
is played. However, the application:didReceiveLocalNotification: is
called if the application delegate implements it. The
UILocalNotification instance is passed into this method, and the
delegate can check its properties or access any custom data from the
userInfo dictionary.
In both of those cases the developer can access the uilocalnotification and then decide what to do with it. However, in a third case - when the user, outside of the App, sees and ignores the notification and then later launches the App, no method is called that allows the application to know which notifications have previously fired?
At first I thought that this statement was describing that behavior, but now I am not sure:
On the other hand, if the local notification only badges the
application icon, and the user in response launches the application,
the application:didFinishLaunchingWithOptions: method is invoked, but
no UILocalNotification object is included in the options dictionary.
How can I handle the third case? How can I know which local notifications have fired? Do I need to iterate through my list and check all their times myself? Is there a better way to accomplish this?
You need to keep track of what is happening with your notifications. What I mean with this is that, because the notification has fired, and the user didn't launch the app because of a notification nor was your app running at the time of the notification, you need to check your sources to verify if a previously scheduled notification fired date has already passed.
What are the requirements and how would I go about implementing the Application Push Notification Service to trigger methods when the application is in the background, or when the phone is closed. Would this even be possible?
Thank you
No thats not possible.
When the app is in the foreground it will receive the push notification directly and can do whatever it wants to in response to that.
However when its not in the foreground the notification is displayed to the user (if they haven't disabled them) and/or displayed in the notification center (if they haven't disabled that). Your app will be brought to the foreground to execute if and only if the user selects the notification.
The application does not receive the notification directly if its not in the foreground.
Read the Apple Docs:
http://developer.apple.com/library/mac/#documentation/NetworkingInternet/Conceptual/RemoteNotificationsPG/Introduction/Introduction.html#//apple_ref/doc/uid/TP40008194-CH1-SW1
It boils down to lots of setup, and then implementing application:didReceiveRemoteNotification. You can read an in-depth example here. Part two goes into the actual application:didReceiveRemoteNotification implementation.