Determining app state when `UIAlertView` or `UIAlertController` are displayed and dismissed - ios

When a privacy alert is shown in iOS (for example asking for access to Photos), UIApplicationWillResignActiveNotification is called as the alert is displayed and the app becomes inactive. Then when a choice is made on the alert and the app resumes, UIApplicationDidBecomeActiveNotification is called. This is also the case when other alerts are shown for example when a notifications alert is displayed i.e. registerUserNotificationSettings.
However, when calling an UIAlertView or UIAlertController, UIApplicationWillResignActiveNotification and UIApplicationDidBecomeActiveNotification are not called when the focus changes from the app to the alert.
Question:
What is going on? What is the equivalent notifications triggered for
the application’s inactive and active state when UIAlertView or
UIAlertController are displayed and dismissed?

When your own code explicitly displays a UIAlertController (or UIAlertView), there is no notification. Your app already knows you are showing the alert because you just showed it.
Other system alerts, like the privacy alerts, are not shown by your app. They are shown by iOS. That is the difference. Your app isn't active because the OS has now active showing the alert. Your app becomes active again when the system alert is done.

Related

UIApplicationState of app is inactive after following interactive push notification when app is closed

App is not running. Interactive push notification comes in. User taps a button on it. App is launched, notification action is handled, but app state is still inactive. Even when I background/foreground the app, it's still inactive. If I follow the notification directly (tapping the notification, not the interactive button) from the same notification, the app gets launched and is in active state. Why doesn't it always go to active state?
Have some thoughts on this.
First of all, it's not a given that a notification will result in a user opening up the app.
Second, there are notification callbacks in the delegate. Is it possible to manually set the application state?
If not, and I don't want to jump to conclusions, but this sounds like it may be a bug, and you may want to file a radar.
Although, you may be able to work around it. Look at those delegate methods.

Present UIAlertView For User Once Application Becomes Active AFTER Receiving A Push Notification

I have been doing some research, but can't seem to get this to work. Is there a way to present a UIAlertView in your app after a person received a push notification while the app was in the background, or inactive? I have tried putting code into the didReceiveRemoteNotification, but it only works when the application is active. Can someone point me in the right direction?
According to the documentation -[UIApplicationDelegate application:didReceiveRemoteNotification] get's only called when the app is in the foreground. Starting with iOS7 you should actually use -[UIApplicationDelegate application:didReceiveRemoteNotification:fetchCompletionHandler:] which will be called both when in foreground and in background.
So and so you are by design prohibited to invoke any UI change when the app is in the background (e.g. showing a UIAlertView will be ignored). You could however set yourself a flag -[UIApplicationDelegate application:didReceiveRemoteNotification:fetchCompletionHandler:] and then when check for it and present the UIAlertView in -[UIApplicationDelegate applicationWillEnterForeground:].

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.

PushNotification in iOs and Application icon is clicked

I receive PushNotification as badge as well as alert and are displayed as badges on the app icon.
I receive the payload successfully when I click on alert, which appears at top area of the screen.
But, if the notification is received and I click on the application icon i don't get the payload in any of below mentioned the methods.
application:didFinishLaunchingWithOptions:
application:didReceiveRemoteNotification:
So, what could be the problem or have I missed something in handling such case.
That would be expected behavior. You don't want to show the push notification for the user if he himself did not actively chose to to tap on the alert. If user is in your app while receiving notification you can ask the user if he wants to view it or dismiss it.

Resources