iOS 在应用处于活动状态时处理推送通知点击

iOS handling Push notification tap when app is active

当应用程序处于活动状态时,如何处理推送通知点击? didReceiveRemoteNotification userInfo: [AnyHashable : Any] 在 iOS 设备收到推送通知时调用,当用户点击通知时调用相同的函数。我如何在这个函数中区分这个函数是如何调用的?我正在使用 OneSignal 的推送通知,以防有必要了解问题。

你可以处理点击,如果你的目标是iOS10以上。

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] 中的标志对其进行管理。

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仍会显示通知。