当应用程序处于活动状态时,如何处理推送通知?
当iOS设备收到推送通知时,将调用didReceiveRemoteNotification userInfo: [AnyHashable : Any]
,而当用户点击通知时,将调用相同的功能。如何在此函数中区分该函数的调用方式?我正在使用OneSignal
的推送通知,以防有必要了解问题。
答案 0 :(得分:1)
OneSingal
的闭包通知您有关通知和用户操作的信息。这就是我的用法
func initOneSignalNotifications(withLaunchOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) {
let onesignalInitSettings = [kOSSettingsKeyAutoPrompt: false]
OneSignal.initWithLaunchOptions(launchOptions, appId: Constants.oneSignalKey, handleNotificationReceived: { (receivedNotification) in
//Notification is received
}, handleNotificationAction: { (notificationResult) in
//Notification was tapped
}, settings: onesignalInitSettings)
OneSignal.inFocusDisplayType = OSNotificationDisplayType.notification;
OneSignal.promptForPushNotifications(userResponse: { accepted in
FileHandler.log(message: "Notification permission granted: \(accepted)", tag: .application, logLevel: .info)
})
}
此处inFocusDisplayType
表示打开您的应用后,OneSignal
仍会显示一条通知。
答案 1 :(得分:0)
如果目标在iOS 10以上,则可以处理单击。
func userNotificationCenter ( _ center : UNUserNotificationCenter, didReceive
response : UNNotificationResponse, withCompletionHandler completionHandler :
@escaping () -> Void ) {
// perform notification received/click action as per third party SDK as per their document
}
}
否则,您需要使用didReceiveRemoteNotification userInfo: [AnyHashable : Any]
中的标志进行管理。