使用Incoming OneSignal推送通知时,如何在应用程序处于前台时阻止警报?

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

didFinishLaunchingWithOptions方法中,你必须添加kOSSettingsKeyInAppAlerts = NO

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

对于Swift 3.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]) 

默认情况下,当应用程序处于焦点时,OneSignal会将通知显示为警报对话框。 要更改此值,请将值为OSNotificationDisplayTypeNotificationOSNotificationDisplayTypeNone给initWithLaunchOptions上的设置。

我是这样实现的。 在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文档