使用OneSignal iOS10的Manuel应用内通知提醒

时间:2016-10-15 17:41:52

标签: ios ios10 onesignal

我已设置OneSignal,以便显示iOS 10默认的应用内通知。但是,在某些情况下,不应显示应用内通知。例如。如果用户已与通知中的信息位于同一页面上。

如何使用OneSignal手动显示默认的iOS 10应用内通知?

这是我的代码:

    OneSignal.initWithLaunchOptions(launchOptions, appId: "your-app-id-here", handleNotificationReceived: ({ (notification) in
        LGNotificationHandler.handleNotification(notification)
    }), handleNotificationAction: ({ (result) in
        LGNotificationHandler.handleNotificationAction(result: result)
    }), settings: [
        kOSSettingsKeyAutoPrompt: false,
        kOSSettingsKeyInAppAlerts: false,
        kOSSettingsKeyInFocusDisplayOption: OSNotificationDisplayType.notification.rawValue])

1 个答案:

答案 0 :(得分:0)

在appdelegate中,您需要将应用内警报设置为true。 附: DLog仅用于登录调试模式,可以忽略。

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
    DLog("--- appDelegate start")

    // MARK: - OneSignal Notifications
    OneSignal.initWithLaunchOptions(launchOptions, appId: "your-app-id-here", handleNotificationReceived: ({ (notification) in
        DLog("handleNotificationReceived: \(notification)")
    }), handleNotificationAction: ({ (result) in            
        DLog("handleNotificationAction: \(result)")
        LGNotificationHandler.handleNotificationAction(result)
    }), settings: [
        kOSSettingsKeyAutoPrompt: false,
        kOSSettingsKeyInAppAlerts: true,
        kOSSettingsKeyInFocusDisplayOption: OSNotificationDisplayType.none.rawValue
    ])

    // You will get a warning here if you are using the latest OneSignal library, but can be safely ignored for the time being.
    OneSignal.setNotificationCenterDelegate(self)

    DLog("--- appDelegate end")
    return true
}

在代表中,您可以设置个别选项,然后需要拨打completionHandler(options),如下所示:

// =========================================================================
// MARK: - OSUserNotificationCenterDelegate
extension AppDelegate: OSUserNotificationCenterDelegate {
    func userNotificationCenter(_ center: Any!, willPresentNotification notification: Any!, withCompletionHandler completionHandler: ((UInt) -> Swift.Void)!) {
        // set the options here
        var options: UInt = 0

        options |= UNNotificationPresentationOptions.badge.rawValue  // if you want to show the badge number
        options |= UNNotificationPresentationOptions.alert.rawValue  // if you want to show alert dialog box
        options |= UNNotificationPresentationOptions.sound.rawValue  // if you want notification to play sound

        // here we perform the notifications
        completionHandler(options)
    }
}