Parse iOS background notifications without clicking on them - swift

When my app is in the background state and I receive a notification; I want to parse the payload of the notification without clicking on the notification.
Is it possible to get notified when a background notification arrives?

If you've truly sent a background notification via the content-available payload key being set to a value of 1, then your app delegate will call application(_:didReceiveRemoteNotification:fetchCompletionHandler)

Related

Notiication when user selects entry from notification center

When a user selects an entry from the push notification center, it triggers the
application:didReceiveRemoteNotification:
method on the application delegate. Is there a way to tell when a push notification is triggered while the user is in the app, and when they select an older push from their notification center?
Is there a way to tell when a push notification is triggered while the user is in the app[...]?
For this I always understood that
application:didReceiveRemoteNotification
is called for every notification received after you start up the app, even if it is on foreground. Although I haven't had the need to do this (and can't confirm it at the moment), so I am calling on theory only. But that's what I can understand from the following on Local and Push Notification Guide.
iOS Note: In iOS, you can determine whether an application is launched as a result of the user tapping the action button or whether the notification was delivered to the already-running application by examining the application state. In the delegate’s implementation of the application:didReceiveRemoteNotification: or application:didReceiveLocalNotification: method, get the value of the applicationState property and evaluate it. If the value is UIApplicationStateInactive, the user tapped the action button; if the value is UIApplicationStateActive, the application was frontmost when it received the notification.
has for
Is there a way to tell when [...](a user) select an older push from their notification center?
For this you could add a order(or time) variable to your push payload to be able to understand if the user is selecting/activating the app trough a push older that a previous one selected.
Check this page for more on adding extra data to the Push Notification.

How to invoke different screens based on push notification received in iPhone?

My iPhone application has different view controllers and I would like to invoke them based on the push notification received from backend. For example if I have view controllers "A" and "B", I would launch "A" when user receives a message regarding some activity in "A" (e.g. Please complete the activity in "A") and
would launch screen "B" if user receives "Please complete the activity in "B").
Can anybody tell me if this is possible in iPhone and if so how?
Sure. Your app delegate is notified of a push notification either through the application:didReceiveRemoteNotification: method (if the notification arrives while the app is active in the foreground of if the app is in the background and the user brings it to the foreground by acting on the push notification) or application:didFinishLaunchingWithOptions: (if the app was neither in the background nor in the foreground and the user launches it by acting on the push notification).
In both cases, the dictionary that is passed to you as a parameter of either method contains the notification's payload/contents. You can use it to identify which type of notification your app received and then act accordingly by displaying the appropriate view controller.

Get custom payload of push notification while app is running background

Hey can anybody tell how to get custom payload of push notification when app is in background?
If your app is in the background and you receive a push notification and the user taps app "View", the application is launched and application:didFinishLaunchingWithOptions: would get called. In this case we can access custom notification payload via UIApplicationLaunchOptionsRemoteNotificationKey from userInfo dictionary.
If your app is in the background and you receive a push notification and the user taps app icon, it brings the application to the foreground.
When that happens, only applicationDidEnterForeground: is called, and there is no way to access the payload of the push notification.
My requirement is to register for Badge only, no alert so no 'View' button to launch app when user receives notification in background. When user receives notification and application is in background, user taps app icon applicationDidEnterForeground: is called. So we have no way to access launchOptions dictionary or userInfo dictionary from where we can get notification payload.
Am I missing something here?
application:didReceiveRemoteNotification: is called when the user taps on the notification in the statusbar drop down if your app is running in the background. The link you referred to in your answer explains how to use it.

How do i find out what state the application is in as the result of a notification?

I have a UILocalNotification set up, and as far as i can see it i have 5 different scenarios:
The app is not running, the user chooses to view the notification, so it launches the app.
The app is not running, the user chooses to close the notification, then opens the app at a later date.
The app is running in the background, the user chooses to view the notification, so it brings the app to the foreground.
The app is running in the background, the user chooses to close the notification, then opens the app bringing it to the foreground at a later date.
The app is running in the foreground.
How do i deal with these 5 different scenarios?
Put your code into application:didFinishLaunchingWithOptions:. In
the actions NSDictionary you will find the information about the
notification.
You can again check in application:didFinishLaunchingWithOptions: if the local
notification is still active and take appropriate action.
Put your code into applicationWillEnterForeground:
Again the same spot, just check if there are active local notifications.
Here you can check in application:didReceiveLocalNotification: and either notify the user or not.
Not exactly sure what you're after, but the following might answer your question.
From the documentation:
When the system delivers a local notification, several things can happen, depending on the application state and the type of notification. If the application is not frontmost and visible, the system displays the alert message, badges the application, and plays a sound—whatever is specified in the notification. 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. 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.
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.

Push notification during call - iPhone

What happens if I send a push notification to the iPhone and it's during a call, or during incoming call?
would it popup on the screen and show the notification to the user?
Yes, it gets displayed on screen and the user will need to close the notification before they can click the "End Call" button. It's a little distracting, but does ensure that the notification has a chance at being viewed. After all, it might be an important notification.