【问题标题】:How do I prevent Alert when App is on Foreground with Incoming OneSignal Push Notification?当应用程序处于前台并带有传入的 OneSignal 推送通知时,如何防止警报?
【发布时间】:2017-03-03 03:03:12
【问题描述】:

当我的应用程序在前台时,会出现一个警报。接收推送通知时如何防止出现这种情况?

【问题讨论】:

    标签: ios push-notification onesignal


    【解决方案1】:

    AppDelegatedidFinishLaunchingWithOptions 方法中,您必须添加kOSSettingsKeyInAppAlerts = NO

    [OneSignal initWithLaunchOptions:launchOptions appId:ONESIGNAL_APPID handleNotificationReceived:nil handleNotificationAction:nil
                                settings:@{kOSSettingsKeyInAppAlerts:@NO}];
    

    【讨论】:

    • 请注意,kOSSettingsKeyInAppAlerts 现在已被弃用,而应使用kOSSettingsKeyInFocusDisplayOption
    【解决方案2】:

    对于 Swift 3.0

    // Initialize OngeSignal with Settings for Push Notifications
        OneSignal.initWithLaunchOptions(launchOptions, appId: Constants.OneSignalAppID, handleNotificationReceived: nil, handleNotificationAction: {
            (result) in
            // Do Something with Notification Result
        }, settings: [kOSSettingsKeyInFocusDisplayOption : OSNotificationDisplayType.none.rawValue])
    

    【讨论】:

      【解决方案3】:

      默认情况下,OneSignal 会在应用未对焦时将通知显示为警报对话框。将此通道 kOSSettingsKeyInFocusDisplayOption 的值 OSNotificationDisplayTypeNotificationOSNotificationDisplayTypeNone 更改为 initWithLaunchOptions 上的设置。

      【讨论】:

        【解决方案4】:

        我是这样实现的。在您的 AppDelegate didFinishLaunchingWithOptions 中添加以下代码

        OneSignal.inFocusDisplayType = OSNotificationDisplayType.none
        

        在最后一行

        func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey : Any]? = nil) -> Bool {
        
        OneSignal.inFocusDisplayType = OSNotificationDisplayType.none
        return true }
        

        我们有这三个选项

        public enum OSNotificationDisplayType : UInt {
        
        
        /*Notification is silent, or app is in focus but InAppAlertNotifications are disabled*/
        case none
        
        
        /*Default UIAlertView display*/
        case inAppAlert
        
        
        /*iOS native notification display*/
        case notification
        }
        

        这里是OneSignal Documentation

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2017-05-02
          • 1970-01-01
          • 2021-12-13
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2017-06-26
          • 2018-07-04
          相关资源
          最近更新 更多