问题
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
回答1:
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.
回答2:
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.
来源:https://stackoverflow.com/questions/20170834/how-to-detect-push-notification-sent-to-other-apps