繁体   English   中英

应用在前台运行时收到 iOS 推送通知

[英]iOS push notification received when app is running in foreground

根据我的理解,当应用程序正在运行或在前台收到推送通知时,应用程序不应显示任何警报,但应用程序委托将调用didReceiveRemoteNotification委托方法,我应该在该回调中处理推送通知。

推送通知应仅在应用程序处于后台时显示警报/横幅。

但是,我们的应用程序会在应用程序运行时或在前台运行时收到带有 OK 按钮的推送通知警报,并非总是如此。 我想知道它是否是 iOS 7 中的新内容(我从未听说过),还是因为我使用UrbanAirship使用用户alias为我们的 iOS 应用程序推送通知。 该应用程序将在运行时显示推送警报并在didReceiveRemoteNotification运行回调。

这让我抓狂。 有谁知道为什么?

当应用程序在前台时,它不应显示任何内容。

如果您看到 alertView,则表示您为其提供了代码。

大致如下:

-(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..
    }
}

如果你已经实现了pushNotificationDelegate

[UAPush shared].pushNotificationDelegate = self;

然后覆盖,并将其留空

- (void)displayNotificationAlert:(NSString *)alertMessage
{
  //do nothing
}

由于在您的代码中配置 Urban Airship 的方式,您很可能会看到额外的推送通知。 来自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;

在他们的支持文章中,有更多关于使用 Urban Airship 处理推送通知的正确方法的信息。 由于您使用的是 UA,我建议您在前台使用他们的委托等来处理传入的推送通知,而不是在didReceiveRemoteNotification应用程序委托方法中实现您自己的代码。

希望这有帮助...如果没有,请发布您的代码,以便我们可以破译正在发生的事情。 这确实是非常奇怪的行为!

此答案适用于寻找相同内容的快速用户

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..
        }

如果你们还有疑问,请参考检查答案。 Apple 关于处理通知的文档

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM