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

Manuel in-app notification alert using OneSignal iOS10

我已经设置了 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])

在您需要将应用内提醒设置为 true 的 appdelegate 中。 p.s。 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
}

在delegate中,可以设置单独的选项,然后需要调用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)
    }
}