Check if application was opened by tapping a notification - ios

I am having some problems with the behavior push notifications in my app.
If the app is not running (not even in the background), when I touch a notification I can parse its payload and do a custom action, by reading the "launchOptions" dictionary.
When my app is running in the background, I am not being able to detect when a user foregrounds the app by tapping the notification, as application:didReceiveRemoteNotification:fetchCompletionHandler: is called immediately in the background, and when I foreground the app I have no way to check if it was done by tapping the app icon or the notification.
I know this is possible because Facebook's Messenger app does this - if I tap the notification it brings me to the chat, else it brings me to where I was previously.
Thanks.

If the user taps the app icon, application:didReceiveRemoteNotification:fetchCompletionHandler does not get called.
If they tap the notification, application:didReceiveRemoteNotification:fetchCompletionHandler does get called and application.applicationState will be UIApplicationStateInactive.
Check out this answer for a code snippet that you can use to quickly see this in action to have it make more sense.
Also, to achieve your goal be sure to do any updating of views when the application state is UIApplicationStateInactive, not UIApplicationStateBackground. The application state will be UIApplicationStateBackground when the push notification comes in. The application state will be UIApplicationStateInactive only when the user launches the app by tapping the notification.
I've seen a lot of answers with if statements like
if (application.applicationState != UIApplicationStateActive) { ... }
but that won't allow you to distinguish between a push notification being delivered and the user tapping a notification.

Related

iOS Display local notifications pop-up while in background

I'm having a little problem in my app : I use to send local notifications to the user when actions are performed in my app, while it's running in background. Notifications are displayed perfectly in the notification center, but there is no pop-up when the notification occurs.
If I'm right, apps running in foreground can't display pop-up on the user screen (only in the notification center) but when the app is running in background, in fact there is pop-up displayed automatically ?
Thanks for your help !
EDIT : In fact, I'm speaking about a notification banner to be displayed too when the notification appear in the notification center.
When your app will go in background then you will see an immediate notification on your phone but you have to tap that notification to trigger didReceiveLocalNotification delegate.
If you receive local notification in foreground then didReceiveLocalNotification will be triggered automatically.
Above scenario is tested and verified.
Update: You must read this documentation: http://www.thekspace.com/home/component/content/article/62-uilocalnotification-demystified.html

How to bring application to foreground in ios?

I am detecting for iBeacon in background. When my device comes in a particular region application should comes to foreground.
It cannot be done without user interaction. The only option is you can generate a push notification to tell the user to bring the application to foreground.
This is from the Apple documentation about this issue:
When the operating system delivers push notification (iOS or OS X)
and the target application is not running in the foreground, it
presents the notification (alert, icon badge number, sound). If there
is a notification alert and the user taps or clicks the action button
(or moves the action slider), the application launches and calls a
method to pass in the local-notification object or remote-notification
payload. If the application is running in the foreground when the
notification is delivered, the application delegate receives a local
or push notification.
To answer to some comments about WhatsApp, with it, when you receive a classic vocal call, IOS use CallKit to display your call and wake up your phone, but it's not inside app. I try to make a video call with WhatsApp, and in this case, there is a notification. Press notification open app and answer to the call.
Conclusion : It's impossible to wake up app from background to foreground in IOS, but it's not really a problem because you can use notification to display what you want and get the user to your app after a touch on your notification. All of iPhone users are familiar with this kind of interaction, it's better to deal with it.

How to detect push notification sent to other apps?

I cannot seem to find a way to detect when a notification comes in. For example, if my app is open and the user gets a Facebook push notification\, is there any UIApplication notification or something else that will tell me that the app is no longer in the foreground and the Facebook alert is on top? Preferably something that is fired if the notification is a banner or an alert
You cannot detect a push notification sent to another app.
If you want to detect whether your app is not in foreground anymore, you can use the applicationWillResignActive: method of UIApplicationDelegate or register for the UIApplicationWillResignActiveNotification notification.
However, when the notification banner appears on top, your application is still in foreground. When the notification is prompted as an alert instead (the user can set this in the preferences), the application loses the focus and it won't be in foreground anymore.
The notification banner at the top is a window displayed by SpringBoard (another process) which does not become key until the user touches it. If the user slides the notification down and the notification center opens, your app becomes inactive and your app delegate hears about this. If the user taps on the notification, your app goes to the background and the other is open. If the user has elected to see alerts instead of banners, once the alert is displayed, your app will resign active. You can listen to UIApplicationWillResignActiveNotification notifications to know when the app resigns active.

How to tell whether user opened app with notification with iOS 7 remote-notification background mode?

According to didReceiveRemoteNotification when in background , we used to be able to handle the user opening the app by clicking the action button on a push notification (or swiping on the push notification, depending on how the user sees push notifications) by implementing -application:didReceiveRemoteNotification: and then checking inside the method whether the application's applicationState was not active.
In iOS 7, there's the new remote-notification background mode, which allows the app to perform background fetch when a remote notification is displayed to the user (without the user necessarily doing anything to the notification). To support this mode, you are supposed to implement the -application:didReceiveRemoteNotification:fetchCompletionHandler: method.
The documentation for -application:didReceiveRemoteNotification: says that if your application delegate implements the application:didReceiveRemoteNotification:fetchCompletionHandler: method, then "the app object calls that method instead of this one." Which means we cannot use -application:didReceiveRemoteNotification: to handle remote notifications anymore, since it's not going to be called.
We should probably put handling logic in application:didReceiveRemoteNotification:fetchCompletionHandler:, but the previous trick for handling it doesn't make sense anymore -- previously, we depended on the fact that the only way for -application:didReceiveRemoteNotification: to be called when the app is not active was if the user tapped the action button on the notification to open the app. However, now, the whole point of the remote-notification background mode is that it can call application:didReceiveRemoteNotification:fetchCompletionHandler: in the background every time a remote notification is received, before the user does anything to it.
So then, how can we now tell when the user opens the app using the action button on the notification?
You still check the application state in application:didReceiveRemoteNotification:fetchCompletionHandler:
UIApplicationStateBackground - App is in the background receiving a push notification
UIApplicationStateInactive - App is opening from the user tapping a notification
I was use this delegate function to add the 『Notification Number』.
Cause our Server not send the Badge to our Clients.
Then I used the strange method to add the 『Notification Number』 with this delegate function, and I also add a code to switch UIViewController in this function.
I found out when I use the server push Notification to my test App, and the status of test App is in the background, even I am using Twitter or Safari.
My test App also switch UIViewController to another UIViewController after I push Notification from the server.

When my app is in background, push notifications are handled only if I touch the top notification banner

I've implemented
application:didReceiveRemoteNotification:
to store data in my app when a push notification is received.
However when my app is in background and I receive a notification, the data is stored only if I touch the notification banner appearing on top:
Instead, if I touch the app icon to reopen it, the content of the notification is not stored:
Since I'm receiving the notifications only when I use the distribution profile, I'm not sure if application:didReceiveRemoteNotification: is invoked only when I push the notification banner on top.
I thought it is always invoked at the time a notification is received, and not after a user action on the device.
UPDATE.
I don't know if this can help but, just to let you know, I haven't implemented any of these methods:
– applicationDidEnterBackground:
– applicationWillEnterForeground:
- applicationDidBecomeActive:
I think I've found out why. From documentation:
If the action button is tapped (on a device running iOS), the system
launches the application and the application calls its delegate’s
application:didFinishLaunchingWithOptions: method (if implemented); it
passes in the notification payload (for remote notifications) or the
local-notification object (for local notifications).
If the application icon is tapped on a device running iOS, the
application calls the same method, but furnishes no information about
the notification.
http://developer.apple.com/library/ios/#documentation/NetworkingInternet/Conceptual/RemoteNotificationsPG/IPhoneOSClientImp/IPhoneOSClientImp.html
However, I'm wondering if there is a way to load the payload even if the app has been re-opened by touching the icon.

Resources