detect why iOS app went into background, hardware home or incoming push notification - ios

I want my app to behave differently if the user pushes hardware home button, versus other reasons it may go into background. I think I have sorted out the main cases, except I can't tell the difference between user tapping on incoming push notification from another app and hitting the hardware home. In iOS 5, I was getting an applicationWillResignActive when the notify first appeared, and then applicationDidEnterBackground when the user tapped. In iOS 6, I cannot find any event triggered by the appearance of a banner notification from another app. The applicationWillResignActive and applicationDidEnterBackground come back to back, just as in hitting hardware home. I tried listening to UIWindowDidBecomeVisibleNotification and other UIWindow notification, but nothing is fired. Any ideas?

Related

iOS when receive a remote notification can we know it and do something?

There is several situations when receiving a remote notification:
app is not launched at all
app is foreground
app is background
what I am looking for is that user haven't click the notification to launch or active the app. can we catch the receiving and do some thing in such three situation?
I know 2 is ok, how about 1 and 3, and how to do it? thx a lot.
somebody mentions widget or NotificationService? is it possible?
You can try adding a Notification Service Extension to your app.
iOS will launch your extension (NOT your app) in background when it receives a push notification. Obviously what you can do inside an extension is fairly limited.
There are two ways to be notified when your app moves to the background: implement the applicationWillResignActive() method in your app delegate, or register for the UIApplication.willResignActiveNotification notification anywhere in your app. This particular notification is sent as soon as your app loses focus, meaning that it's triggered when the user taps the home button once (to return to the home screen) or double taps the home button (to enter multi-tasking).

Is it possible to have a "notification only" app, or NOT show the watch app after a notification is displayed?

The iOS app I'm working on for a client currently only uses Apple Watch for notifications.
When a local notification fires while the user's phone is locked, it displays the notification on the phone. The user can dismiss the notification or click on one of the action buttons on it.
In either case, I want to dismiss the watch app and go back to whatever state it was in (back to the watch desktop, or springboard, or whatever it's called.) I don't want to present the UI for the watch app, since there isn't one, at least not yet. I don't know if my client is going to define a watch app UI or not.
As it is now, once the user clicks the dismiss button or an action button on the notification, the user is left with a stupid-looking placeholder watch app that simply has a button called "button" (which I added for testing.)
I'm afraid that you cannot change this. iOS decides which device the notification is shown on and you have no control over this.
If the iPhone is locked then iOS assumes that you are using your watch and will deliver the notification to it, however if your iPhone is unlocked it tends to deliver to notification to the iPhone.

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.

Distinguish notification center hiding and returning from background with DidBecomeActive notification

I'm confused about UIApplicationDidBecomeActiveNotification. I can register an observer for this notification, and receive signals when the app becomes active from the background or when being launched. However, when I pull down the hidden top popup view (for general information: notifications, weather, stock market) then pull up to hide it, the signal is also generated.
I use a callback method hooked with this signal to refresh my app, so this can be annoying for users of my app.
Can anybody help me differentiate these cases?
Maybe you should try UIApplicationWillEnterForegroundNotification, the apple doc has said:
https://developer.apple.com/library/ios/documentation/UIKit/Reference/UIApplication_Class/Reference/Reference.html
UIApplicationDidBecomeActiveNotification: Posted when the application becomes active.
An application is active when it is receiving events. An active application can be said to have focus. It gains focus after being launched, loses focus when an overlay window pops up or when the device is locked, and gains focus when the device is unlocked.

Resources