简体   繁体   English

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

[英]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.

相关问题 应用程序处于前台时未收到推送通知 ios flutter - Push Notification not received when app is in foreground ios flutter 在 iOS 上收到通知时确定应用程序是否在前台运行 - Determine if an app is running in the foreground when a notification is received on iOS iOS在前台运行时如何更改推送通知标题? - iOS how to change push notification title when app is running in foreground? 前台应用程序抖动时未收到 Firebase 推送通知? - Firebase Push Notification not received when flutter app in foreground? 当应用程序在 iOS 前台时如何处理 Flutter FCM 推送通知? - How to handle Flutter FCM push notification when app in foreground for iOS? 在Swift,iOS 8,9中的App Foreground时推送本地通知 - Push Local Notification when App Foreground in Swift, iOS 8,9 iOS如何在应用程序处于前台时处理解析JSON推送通知 - iOS How to Handle Parse JSON Push Notification when App is in Foreground 当应用程序在前台时,IOS Play Sound接收推送通知 - IOS Play Sound on receiving push notification when app in foreground 当应用程序在前台时,iOS Swift 接收推送通知 - iOS Swift Receive Push Notification When App in Foreground App在前景上并收到推送通知消息(IOS)时发生钛合金崩溃 - Titanium Crash when App is on Foreground and receives a Push Notification message (IOS)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM