【问题标题】:Disable One Signal Push Notification when app is in foreground ios 10 swift当应用程序处于前台时禁用一个信号推送通知 ios 10 swift
【发布时间】:2018-02-07 16:38:05
【问题描述】:

我在 onesignal 启动选项中禁用了对焦、自动​​提示、启动 url 和应用程序警报,但是当应用程序处于前台时仍然显示横幅,是缺少什么或者我写了一些额外的东西,这是我的代码,请查看下面的代码并帮助我。

    //For One Signal
    let notificationReceivedBlock: OSHandleNotificationReceivedBlock = { notification in

        print("Received Notification: \(notification!.payload.notificationID)")
    }

    let notificationOpenedBlock: OSHandleNotificationActionBlock = { result in
        // This block gets called when the user reacts to a notification received
        let payload: OSNotificationPayload = result!.notification.payload

        var fullMessage = payload.body
        print("Message = \(String(describing: fullMessage))")
        if payload.additionalData != nil {
            if payload.title != nil {
                let messageTitle = payload.title
                print("Message Title = \(messageTitle!)")
            }

            let additionalData = payload.additionalData
            if additionalData?["actionSelected"] != nil {
                fullMessage = fullMessage! + "\nPressed ButtonID: \(String(describing: additionalData!["actionSelected"]))"
            }
        }
    }

    let onesignalInitSettings = [kOSSettingsKeyAutoPrompt: false,
                                 kOSSettingsKeyInAppLaunchURL: false,
                                 kOSSettingsKeyInAppAlerts : false,
                                 kOSSettingsKeyInFocusDisplayOption: OSNotificationDisplayType.none.rawValue] as [String : Any]

    OneSignal.initWithLaunchOptions(launchOptions,
                                    appId: oneSignalAppID,
                                    handleNotificationReceived: notificationReceivedBlock,
                                    handleNotificationAction: notificationOpenedBlock,
                                    settings: onesignalInitSettings)

    OneSignal.inFocusDisplayType = OSNotificationDisplayType.none

    OneSignal.promptForPushNotifications(userResponse: { accepted in
        print("User accepted notifications: \(accepted)")
    })

【问题讨论】:

  • 你修好了吗?我也有同样的问题

标签: ios swift3 xcode8 ios10 onesignal


【解决方案1】:

您应该将 onesignalInitSettings 更改为:

let onesignalInitSettings = [kOSSettingsKeyAutoPrompt: false, kOSSettingsKeyInAppLaunchURL: false]

查看 Swift 中的 github 示例:https://github.com/OneSignal/OneSignal-iOS-SDK/tree/master/Examples/SwiftExample

另外,调用:

OneSignal.promptForPushNotifications(userResponse: { accepted in print("User accepted notifications: \(accepted)") })

像这样在 AppDelegate 中会在用户首次打开应用时提示用户,其方式与在 onesignalInitSettings 中设置 kOSSettingsKeyAutoPrompt: true 相同。

如果您稍后试图在应用中提示用户。

【讨论】:

    【解决方案2】:

    不是 swift 而是伪代码(c#):

     OneSignal.Current.StartInit(Secrets.OneSignalId)
                        .InFocusDisplaying(OSInFocusDisplayOption.None)
                        .HandleNotificationReceived(HandleNotificationReceived)                         .HandleNotificationOpened(HandleNotificationOpened)
                        .EndInit();
    

    在 OneSignal 文档中查找 InFocusDisplaying

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-07-04
      • 2021-04-19
      • 2018-08-03
      • 1970-01-01
      • 2017-05-11
      • 2017-06-26
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多