Disable One Signal Push Notification when app is in foreground ios 10 swift

2.5k Views Asked by At

I had disable infocus, autoprompt, launch url and app alert in onesignal launch options, but still banner is displayed when app is in foreground, is am a missing something or i had written something extra, here is my code, please review below code and help me out.

    //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)")
    })
3

There are 3 best solutions below

0
On

Not be in swift but in pseudo code (c#):

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

Look for InFocusDisplaying in OneSignal docs

0
On

You should change your onesignalInitSettings to just be:

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

Check out the github example in Swift: https://github.com/OneSignal/OneSignal-iOS-SDK/tree/master/Examples/SwiftExample

Also, calling:

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

in the AppDelegate like this will prompt the user when they first open the app in the same way as setting kOSSettingsKeyAutoPrompt: true in the onesignalInitSettings.

In case you were trying to prompt users at a later time in the app.

0
On

In Swift 5 and newer OneSignal SDK, you can disable push notifications with just:

OneSignal.disablePush(true)

and you can enable it whenever you want by:

OneSignal.disablePush(false)

just don't forget to:

import OneSignal