gpt4 book ai didi

ios - 当用户在前台且应用程序全新安装时,不会调用 willPresent 和 didReceive 通知委托(delegate)

转载 作者:行者123 更新时间:2023-11-29 05:43:14 24 4
gpt4 key购买 nike

我已经实现了 FirebaseCloudMessaging,当应用程序处于后台时收到通知,但当我安装新的应用程序时willPresent code> 和 didReceive 通知 delegate 在 30 到 35 分钟后不会被调用,它将开始调用。

只有当我通过删除旧应用程序来安装应用程序时才会发生这种情况。

这是我的代码,你可以检查我哪里出错了

import UIKit
import Firebase
import UserNotifications

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

var window: UIWindow?
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {

// Register for push notification
self.registerForNotification()
FirebaseApp.configure()
Messaging.messaging().delegate = self
return true
}
}

extension AppDelegate: UNUserNotificationCenterDelegate {

//MARK: - Register For RemoteNotification
func registerForNotification() {
UNUserNotificationCenter.current().delegate = self
UNUserNotificationCenter.current().requestAuthorization(options: [.badge, .alert, .sound]) { granted, error in }
UIApplication.shared.registerForRemoteNotifications()
}

func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
#if DEVELOPMENT
Messaging.messaging().setAPNSToken(deviceToken, type: .sandbox)
#else
Messaging.messaging().setAPNSToken(deviceToken, type: .prod)
#endif
}

func application(_ application: UIApplication, didFailToRegisterForRemoteNotificationsWithError error: Error) {
debugPrint("Unable to register for remote notifications: \(error.localizedDescription)")
}

//MARK: - UNUserNotificationCenterDelegate
func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable : Any]) {
debugPrint(userInfo)
}

func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {

let userInfo = notification.request.content.userInfo
debugPrint(userInfo)
completionHandler([.badge, .alert, .sound])
}


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

extension AppDelegate: MessagingDelegate {

//MARK: - MessagingDelegate
func messaging(_ messaging: Messaging, didReceiveRegistrationToken fcmToken: String) {
print(fcmToken)
}

func messaging(_ messaging: Messaging, didReceive remoteMessage: MessagingRemoteMessage) {
print(remoteMessage.appData)
}
}

感谢帮助

最佳答案

用此代码替换 registerForNotification 函数代码。也许会有帮助

UNUserNotificationCenter.current().delegate = self
let authOptions: UNAuthorizationOptions = [.alert, .badge, .sound]
UNUserNotificationCenter.current().requestAuthorization(options: authOptions) { (isAllow, error) in

if isAllow {

Messaging.messaging().delegate = self

}

}

UIApplication.shared.registerForRemoteNotifications()

当用户允许通知时,将调用此委托(delegate)方法

 func messaging(_ messaging: Messaging, didReceiveRegistrationToken fcmToken: String) {

print(fcmToken)



}

点击通知时

  func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) {
//when notification is tapped

}

当应用程序处于前台时将调用此方法

  func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {

//this method is called when notification is about to appear in foreground

completionHandler([.alert, .badge, .sound]) // Display notification as

}

关于ios - 当用户在前台且应用程序全新安装时,不会调用 willPresent 和 didReceive 通知委托(delegate),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56372910/

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