当我的应用程序在前台运行时,会出现一个提示。我该如何防止在接收推送通知时出现此提示?
当我的应用程序在前台运行时,会出现一个提示。我该如何防止在接收推送通知时出现此提示?
AppDelegate
的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会将通知显示为警报对话框。要更改此设置,请在initWithLaunchOptions中传递kOSSettingsKeyInFocusDisplayOption
,并将其值设置为OSNotificationDisplayTypeNotification
或OSNotificationDisplayTypeNone
。
OneSignal.inFocusDisplayType = OSNotificationDisplayType.none
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey : Any]? = nil) -> Bool {
OneSignal.inFocusDisplayType = OSNotificationDisplayType.none
return true }
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
}
kOSSettingsKeyInAppAlerts
已经不再使用,应改用kOSSettingsKeyInFocusDisplayOption
。 - jkasten