[英]iOS push notification received when app is running in foreground
From my understanding, when app is running or in the foreground and a push notification is received, the app should NOT show any alert but the app delegate will call the didReceiveRemoteNotification
delegate method and I should handle the push notification in that callback.根据我的理解,当应用程序正在运行或在前台收到推送通知时,应用程序不应显示任何警报,但应用程序委托将调用didReceiveRemoteNotification
委托方法,我应该在该回调中处理推送通知。
The push notification should ONLY display alerts/banners when the app is in the background.推送通知应仅在应用程序处于后台时显示警报/横幅。
However, our app gets push notification alert with an OK button when the app is running or in the foreground sometime, not all of the time.但是,我们的应用程序会在应用程序运行时或在前台运行时收到带有 OK 按钮的推送通知警报,并非总是如此。 I'm wondering if it is something new in iOS 7 (I have never heard of this) or is it because I'm using UrbanAirship
for push notification for our iOS app using alias
of the user.我想知道它是否是 iOS 7 中的新内容(我从未听说过),还是因为我使用UrbanAirship
使用用户alias
为我们的 iOS 应用程序推送通知。 The app will display the push alert when running and run the callback in didReceiveRemoteNotification
.该应用程序将在运行时显示推送警报并在didReceiveRemoteNotification
运行回调。
Scratching my head over this.这让我抓狂。 Does anyone know why?有谁知道为什么?
When the App is in foreground, it should not display anything.当应用程序在前台时,它不应显示任何内容。
If you see alertView, it means you provided code for it.如果您看到 alertView,则表示您为其提供了代码。
Something along the following lines:大致如下:
-(void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo
{
UIApplicationState state = [application applicationState];
if (state == UIApplicationStateActive) {
//app is in foreground
//the push is in your control
} else {
//app is in background:
//iOS is responsible for displaying push alerts, banner etc..
}
}
If you have implemented pushNotificationDelegate
如果你已经实现了pushNotificationDelegate
[UAPush shared].pushNotificationDelegate = self;
then override, and leave it blank然后覆盖,并将其留空
- (void)displayNotificationAlert:(NSString *)alertMessage
{
//do nothing
}
You are most likely seeing the additional push notification because of the way Urban Airship is configured in your code.由于在您的代码中配置 Urban Airship 的方式,您很可能会看到额外的推送通知。 From Urban Airship's docs :来自Urban Airship 的文档:
The sample UI includes an implementation of UAPushNotificationDelegate that handles alerts, sounds, and badges. However, if you wish to customize this behavior, you can provide your own implementation:
[UAPush shared].pushNotificationDelegate = customPushDelegate;
There is more information on the proper way to handle push notifications with Urban Airship in their support articles . 在他们的支持文章中,有更多关于使用 Urban Airship 处理推送通知的正确方法的信息。 Since you are using UA, I would recommend that you use their delegates etc. to handle incoming push notifications while in the foreground rather than implementing your own code in the didReceiveRemoteNotification
app delegate method.由于您使用的是 UA,我建议您在前台使用他们的委托等来处理传入的推送通知,而不是在didReceiveRemoteNotification
应用程序委托方法中实现您自己的代码。
Hopefully that helps...if not, please post your code so that we can decipher what is going on.希望这有帮助...如果没有,请发布您的代码,以便我们可以破译正在发生的事情。 This would be very odd behavior indeed!这确实是非常奇怪的行为!
This answer is for swift users looking for the same thing此答案适用于寻找相同内容的快速用户
let state = application.applicationState
if (state == .active) {
//app is in foreground
//the push is in your control
} else {
//app is in background:
//iOS is responsible for displaying push alerts, banner etc..
}
Please refer to the checked answer if you guys have any more doubts.如果你们还有疑问,请参考检查答案。 Apple Documentation on handling Notifications Apple 关于处理通知的文档
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.