gpt4 book ai didi

ios - 如果用户不允许快速通知,则不会调用 didRefreshRegistrationToken

转载 作者:行者123 更新时间:2023-11-28 19:29:54 26 4
gpt4 key购买 nike

在我尝试了很多可能有解决这个问题的问题后,我的问题没有得到解决:

我想获得FCM Token。在 AppDelegate.swift 我导入了

import UserNotifications
import Firebase
import FirebaseInstanceID
import FirebaseMessaging

之后,我用 UNUserNotificationCenterDelegate, MessagingDelegate 对其进行了扩展,然后在 didFinishLaunchingWithOptions 中添加了:

if #available(iOS 10.0, *) {
// For iOS 10 display notification (sent via APNS)
UNUserNotificationCenter.current().delegate = self
let authOptions: UNAuthorizationOptions = [.alert, .badge, .sound]
UNUserNotificationCenter.current().requestAuthorization(
options: authOptions,
completionHandler: {_, _ in })
// For iOS 10 data message (sent via FCM
Messaging.messaging().delegate = self
} else {
let settings: UIUserNotificationSettings =
UIUserNotificationSettings(types: [.alert, .badge, .sound], categories: nil)
application.registerUserNotificationSettings(settings)
}

application.registerForRemoteNotifications()

FirebaseApp.configure()

然后我添加了这四个函数:

func application(_ application: UIApplication,
didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
Messaging.messaging().apnsToken = deviceToken as Data

}

func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) {
print("Here")
}

func messaging(_ messaging: Messaging, didReceive remoteMessage: MessagingRemoteMessage) {
print("Received data message: \(remoteMessage.appData)")
}

func messaging(_ messaging: Messaging, didRefreshRegistrationToken fcmToken: String) {
print("Firebase registration token: \(fcmToken)")
}

现在的问题是,当我运行该应用程序时,它会显示警报,要求获得发送通知的权限。允许或不允许它不是我关心的问题我已经实现了 MessagingDelegate 方法 didRefreshRegistrationToken 所以它应该被调用,但它没有调用该方法。那我是不是漏掉了什么?

最佳答案

你必须像那样添加一个观察者

NotificationCenter.default.addObserver(self, selector: #selector(self.tokenRefreshNotification), name: NSNotification.Name.InstanceIDTokenRefresh, object: nil)

然后像那样在您的 Appdelegate 中添加此方法

func tokenRefreshNotification(_ notification: Notification) {
if let refreshedToken = InstanceID.instanceID().token() {
print("InstanceID token: \(refreshedToken)")
}
}

希望对您有所帮助。

关于ios - 如果用户不允许快速通知,则不会调用 didRefreshRegistrationToken,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46339505/

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