当应用在前台运行时,如何禁用One Signal推送通知iOS 10 Swift?

7
我已经在OneSignal的启动选项中禁用了Infocus、Autoprompt、Launch URL和App Alert,但当应用程序在前台时仍然显示横幅广告。我是否漏掉了什么,或者我多写了什么代码?以下是我的代码,请查看并帮助解决问题。
    //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)")
    })

你修好了吗?我也遇到了同样的问题。 - Utku Dalmaz
3个回答

0

不是用 Swift 而是伪代码(c#):

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

在OneSignal文档中查找InFocusDisplaying


0
在Swift 5及更高版本的OneSignal SDK中,您只需执行以下操作即可禁用推送通知:
OneSignal.disablePush(true)

你可以随时启用它,方法如下:
OneSignal.disablePush(false)

不要忘记:
import OneSignal

-1

你应该将onesignalInitSettings更改为:

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

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

此外,在AppDelegate中调用:

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

像这样在用户第一次打开应用程序时提示用户,就像在onesignalInitSettings中设置kOSSettingsKeyAutoPrompt: true一样。

如果您想在应用程序的后续时间提示用户,请参考上述代码。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接