[英]Push Local Notification when App Foreground in Swift, iOS 8,9
[英]Disable One Signal Push Notification when app is in foreground ios 10 swift
我在 onesignal 啟動選項中禁用了 infocus、autoprompt、launch url 和 app alert,但是當應用程序處於前台時仍然顯示橫幅,缺少某些東西或者我寫了一些額外的東西,這是我的代碼,請查看下面的代碼和幫幫我。
//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)")
})
你應該將你的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
相同。
如果您試圖稍后在應用程序中提示用戶。
不是在 swift 中,而是在偽代碼 (c#) 中:
OneSignal.Current.StartInit(Secrets.OneSignalId)
.InFocusDisplaying(OSInFocusDisplayOption.None)
.HandleNotificationReceived(HandleNotificationReceived) .HandleNotificationOpened(HandleNotificationOpened)
.EndInit();
在 OneSignal 文檔中查找InFocusDisplaying
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.