【问题标题】:iOS push notification received when app is running in foreground应用在前台运行时收到 iOS 推送通知
【发布时间】:2014-10-21 07:53:31
【问题描述】:

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

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

但是,当应用程序正在运行或在前台运行时,我们的应用程序有时会收到带有 OK 按钮的推送通知警报,而不是所有时间。我想知道这是否是 iOS 7 中的新功能(我从未听说过),还是因为我使用UrbanAirship 为我们的 iOS 应用程序使用用户的alias 推送通知。应用程序在运行时会显示推送警报,并在didReceiveRemoteNotification 中运行回调。

为此挠头。有谁知道为什么?

【问题讨论】:

  • 如果您没有代码在收到推送通知时显示 UIAlertView,听起来可能是 Urban Airship 正在这样做。

标签: ios objective-c apple-push-notifications urbanairship.com


【解决方案1】:

当应用在前台时,它不应该显示任何东西。

如果您看到 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
}

【讨论】:

  • 这正是我的问题。我收到 2 个警报视图。一个来自我的代码(与您的代码相同),一个来自无处。我在想是否城市飞艇负责第二次警报
  • 是的,UA 是罪魁祸首。覆盖推送通知工作的 UA 处理。谢谢
【解决方案2】:

由于 Urban Airship 在代码中的配置方式,您很可能会看到额外的推送通知。来自Urban Airship's docs

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 in their support articles 处理推送通知的正确方法的信息。由于您使用的是 UA,我建议您在前台使用他们的委托等来处理传入的推送通知,而不是在 didReceiveRemoteNotification 应用委托方法中实现您自己的代码。

希望对您有所帮助...如果没有,请发布您的代码,以便我们破译发生了什么。这确实是非常奇怪的行为!

【讨论】:

【解决方案3】:

此答案适用于寻找相同事物的快速用户

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 Documentation on handling Notifications

【讨论】:

    猜你喜欢
    • 2017-05-11
    • 1970-01-01
    • 2020-12-23
    • 1970-01-01
    • 2019-07-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-01-30
    相关资源
    最近更新 更多