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

问题描述:

我似乎无法找到一种方法来检测何时收到通知.例如,如果我的应用程序处于打开状态并且用户收到 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.

如果你想检测你的应用是否不再处于前台,你可以使用UIApplicationDelegateapplicationWillResignActive:方法或注册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.