通知警报禁用swift

时间:2016-09-13 11:28:44

标签: ios swift push-notification notifications onesignal

进行测试我使用OneSignal服务在我的设备上发送推送通知,我以这种方式在AppDelegate中处理它:

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
}

我的问题是,当我收到通知并且应用程序在前台时,警报会自动显示,我不想显示它。 我该如何解决这个问题?

2 个答案:

答案 0 :(得分:2)

这段代码对我有用。

这适用于Swift 2.2和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

答案 1 :(得分:0)

您需要在kOSSettingsKeyInFocusDisplayOption中将None设置为initWithLaunchOptions,以禁用自动显示inapp警报。

OneSignal Api Reference