arturo
arturo

Reputation: 51

notification alert disable swift

for testing I use OneSignal service for send push notification on my device and I handle it in AppDelegate in this way:

func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
        // Override point for customization after application launch.

        OneSignal.initWithLaunchOptions(launchOptions, appId: “[app ID]”)//this method I register device on apple server

        return true
    }

func application(application: UIApplication,
                       didReceiveRemoteNotification userInfo: [NSObject : AnyObject],
                                                    fetchCompletionHandler completionHandler: (UIBackgroundFetchResult) -> Void){

        print(“ARRIVED")
        handleNotificationContent()// it’s not important for my question
}

My problem is that when I receive a notification and the app is in foreground , alert shows automatically and I don’t want to show it. How do I solve this problem?

Upvotes: 0

Views: 2961

Answers (2)

J. Goce
J. Goce

Reputation: 279

This code worked for me.

This is applicable for Swift 2.2 and Xcode 7.3.1

//Initialize One Signal using this code

OneSignal.initWithLaunchOptions(launchOptions, appId: oneSignalId, handleNotificationReceived: { (notification) in

     //Put your business logic here like adding an alert controller or posting an NSNotification.

}, handleNotificationAction: { (nil) in

     // This block gets called when the user reacts to a notification received

}, settings: [kOSSettingsKeyAutoPrompt : false, kOSSettingsKeyInAppAlerts: false])

     //set kOSSettingsKeyAutoPrompt to false

Upvotes: 2

Dejan Skledar
Dejan Skledar

Reputation: 11435

You need to set the kOSSettingsKeyInFocusDisplayOption to None in initWithLaunchOptions, to disable the automatic display of inapp alerts.

OneSignal Api Reference

Upvotes: 0

Related Questions