当应用程序在前台使用传入的 OneSignal 推送通知时,如何防止警报?

How do I prevent Alert when App is on Foreground with Incoming OneSignal Push Notification?

当我的应用程序在前台时,会出现一个警告。如何防止在接收推送通知时出现这种情况?

默认情况下,OneSignal 在应用处于焦点状态时将通知显示为警告对话框。要将值 OSNotificationDisplayTypeNotificationOSNotificationDisplayTypeNone 的此传递 kOSSettingsKeyInFocusDisplayOption 更改为 initWithLaunchOptions 上的设置。

AppDelegatedidFinishLaunchingWithOptions 方法中,您必须添加 kOSSettingsKeyInAppAlerts = NO

[OneSignal initWithLaunchOptions:launchOptions appId:ONESIGNAL_APPID handleNotificationReceived:nil handleNotificationAction:nil
                            settings:@{kOSSettingsKeyInAppAlerts:@NO}];

对于Swift3.0

// Initialize OngeSignal with Settings for Push Notifications
    OneSignal.initWithLaunchOptions(launchOptions, appId: Constants.OneSignalAppID, handleNotificationReceived: nil, handleNotificationAction: {
        (result) in
        // Do Something with Notification Result
    }, settings: [kOSSettingsKeyInFocusDisplayOption : OSNotificationDisplayType.none.rawValue])

我是这样实现的。在您的 AppDelegate didFinishLaunchingWithOptions

中添加以下代码
OneSignal.inFocusDisplayType = OSNotificationDisplayType.none

的最后一行
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey : Any]? = nil) -> Bool {

OneSignal.inFocusDisplayType = OSNotificationDisplayType.none
return true }

我们有这 3 个选项

public enum OSNotificationDisplayType : UInt {


/*Notification is silent, or app is in focus but InAppAlertNotifications are disabled*/
case none


/*Default UIAlertView display*/
case inAppAlert


/*iOS native notification display*/
case notification
}

这里是OneSignal Documentation