gpt4 book ai didi

IOS 推送通知在前台 Swift 4.2

转载 作者:行者123 更新时间:2023-11-28 11:38:23 25 4
gpt4 key购买 nike

我正在开发 IOS 应用程序,我正在使用 Parse SDK 处理推送通知。当我的背部处于后台时,我能够收到通知。现在我想接收通知,即使我的应用程序在前台。我已经为它搜索了许多代码,但它不适合我当前的 AppDelegate 类。

即使我的应用程序在前台,如何获取推送通知?

 @UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

var window: UIWindow?


func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
// Thread.sleep(forTimeInterval: 3.0)
// Override point for customization after application launch.

let configuration = ParseClientConfiguration {
$0.applicationId = "asdasdasd"
$0.clientKey = "asdasdasdas"
$0.server = "https://parseapi.back4app.com"
}
Parse.initialize(with: configuration)

UNUserNotificationCenter.current().delegate = self as? UNUserNotificationCenterDelegate

UNUserNotificationCenter.current().requestAuthorization(options: [.alert, .sound, .badge]) {
(granted, error) in
print("Permission granted: \(granted)")
guard granted else { return }
self.getNotificationSettings()
}

return true
}

func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void)
{
completionHandler([.alert, .badge, .sound])
}

func getNotificationSettings() {
UNUserNotificationCenter.current().getNotificationSettings { (settings) in
print("Notification settings: \(settings)")
guard settings.authorizationStatus == .authorized else {
return
}
DispatchQueue.main.async(execute: {
UIApplication.shared.registerForRemoteNotifications()
})
}
}

func application(_ application: UIApplication,
didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
createInstallationOnParse(deviceTokenData: deviceToken)
}


func applicationWillResignActive(_ application: UIApplication) {

}

func applicationDidEnterBackground(_ application: UIApplication) {

}

func applicationWillEnterForeground(_ application: UIApplication) {

}

func applicationDidBecomeActive(_ application: UIApplication) {

}

func applicationWillTerminate(_ application: UIApplication) {

}

func application(_ application: UIApplication,
didFailToRegisterForRemoteNotificationsWithError error: Error) {
print("Failed to register: \(error)")
}
}
}

我从同一个论坛找到了这个解决方案,但我无法将以下代码片段放入我当前的代码中:

func userNotificationCenter(_ center: UNUserNotificationCenter,
didReceive response: UNNotificationResponse,
withCompletionHandler completionHandler: @escaping () -> Void) {
let userInfo = response.notification.request.content.userInfo


// Print full message.
print("tap on on forground app",userInfo)

completionHandler()
}

最佳答案

从 iOS 10 开始,Apple 允许用户在前台处理推送通知呈现。您可以在 willPresent 方法中处理它。并在 completionHandler 中处理推送通知,传递您想要的选项,即警报、角标(Badge)和声音。

确认您的 appDelegate 使用 UNUserNotificationCenterDelegate

将其放入您的didFinishLaunchingWithOptions 方法

UNUserNotificationCenter.current().delegate = self

之前

UNUserNotificationCenter.current().requestAuthorization(options: [.alert, .sound, .badge]) {
(granted, error) in
print("Permission granted: \(granted)")
guard granted else { return }
self.getNotificationSettings()
}

并添加这个方法

 func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void)
{
completionHandler([.alert, .badge, .sound])
}

iOS 14 更新.alert 已经过时,您现在应该使用 .banner,如下所示:

func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void)
{
completionHandler([.banner, .badge, .sound])
}

关于IOS 推送通知在前台 Swift 4.2,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54480813/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com