如何检测发送到其他应用程序的推送通知?

问题描述:

我似乎无法找到检测通知何时进入的方法。例如,如果我的应用程序已打开并且用户获得了Facebook推送通知\,是否有任何UIApplication通知或其他会告诉我的内容该应用程序不再处于前台,Facebook警报位于顶部?如果通知是横幅或警报,则最好触发某些内容

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.

如果你想检测你的应用是否不在前台,你可以使用 applicationWillResignActive:方法 UIApplicationDelegate 或注册 UIApplicationWillResignActiveNotification 通知。

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.