我在onesignal启动选项中禁用了infocus、自动提示、启动url和应用程序警报,但当应用程序位于前台时仍然显示横幅,是缺少某些东西还是我写了一些额外的东西,这是我的代码,请查看下面的代码并帮助我。
//For One Signal
let notificationReceivedBlock: OSHandleNotificationReceivedBlock = { notification in
print("Received Notification: \(notification!.payload.notificationID)")
}
let notificationOpenedBlock: OSHandleNotificationActionBlock = { result in
// This block gets called when the user reacts to a notification received
let payload: OSNotificationPayload = result!.notification.payload
var fullMessage = payload.body
print("Message = \(String(describing: fullMessage))")
if payload.additionalData != nil {
if payload.title != nil {
let messageTitle = payload.title
print("Message Title = \(messageTitle!)")
}
let additionalData = payload.additionalData
if additionalData?["actionSelected"] != nil {
fullMessage = fullMessage! + "\nPressed ButtonID: \(String(describing: additionalData!["actionSelected"]))"
}
}
}
let onesignalInitSettings = [kOSSettingsKeyAutoPrompt: false,
kOSSettingsKeyInAppLaunchURL: false,
kOSSettingsKeyInAppAlerts : false,
kOSSettingsKeyInFocusDisplayOption: OSNotificationDisplayType.none.rawValue] as [String : Any]
OneSignal.initWithLaunchOptions(launchOptions,
appId: oneSignalAppID,
handleNotificationReceived: notificationReceivedBlock,
handleNotificationAction: notificationOpenedBlock,
settings: onesignalInitSettings)
OneSignal.inFocusDisplayType = OSNotificationDisplayType.none
OneSignal.promptForPushNotifications(userResponse: { accepted in
print("User accepted notifications: \(accepted)")
})
不是用 swift 而是用伪代码(c#):
OneSignal.Current.StartInit(Secrets.OneSignalId)
.InFocusDisplaying(OSInFocusDisplayOption.None)
.HandleNotificationReceived(HandleNotificationReceived) .HandleNotificationOpened(HandleNotificationOpened)
.EndInit();
在 OneSignal 文档中查找
InFocusDisplaying
在 Swift 5 和更新版本的 OneSignal SDK 中,您只需使用以下命令即可禁用推送通知:
OneSignal.disablePush(true)
您可以随时通过以下方式启用它:
OneSignal.disablePush(false)
只是不要忘记:
import OneSignal
您应该将 onesignalInitSettings 更改为:
let onesignalInitSettings = [kOSSettingsKeyAutoPrompt: false, kOSSettingsKeyInAppLaunchURL: false]
查看 Swift 中的 github 示例:https://github.com/OneSignal/OneSignal-iOS-SDK/tree/master/Examples/SwiftExample
另外,请致电:
OneSignal.promptForPushNotifications(userResponse: { accepted in
print("User accepted notifications: \(accepted)")
})
在 AppDelegate 中,这样会在用户首次打开应用程序时提示用户,其方式与在 onesignalInitSettings 中设置
kOSSettingsKeyAutoPrompt: true
相同。
如果您稍后尝试在应用程序中提示用户。