如果用户不允许通知迅速,则不会调用didRefreshRegistrationToken [英] didRefreshRegistrationToken is not being called if user Don't Allow notifications swift

查看:125
本文介绍了如果用户不允许通知迅速,则不会调用didRefreshRegistrationToken的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试了很多可以解决此问题的问题之后,仍未解决我的问题:

My problem is not being solved after I've tried a lot of questions which might have the solution for this issue:

我想获得FCM Token.在AppDelegate.swift中,我已导入

I want to get FCM Token. In AppDelegate.swift I've imported

import UserNotifications
import Firebase
import FirebaseInstanceID
import FirebaseMessaging

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

After that I've extended it with UNUserNotificationCenterDelegate, MessagingDelegate and then in didFinishLaunchingWithOptions I've added:

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()

然后,我添加了以下四个功能:

And after that I've added these four functions:

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,因此应该调用它,但是它没有调用该方法.那我想念什么吗?

Now the issue is when I run the app it shows the alert which ask for permissions for sending notification. Allowing it or disallowing it is not my concern I have implemented MessagingDelegate method didRefreshRegistrationToken so it should be called, but it is not calling that method. So am I missing something?

推荐答案

您必须添加一个类似的观察者

You have to add an Observer like that

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

并像这样在您的Appdelegate中添加此方法

And add this method in your Appdelegate like that

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

希望有帮助.

这篇关于如果用户不允许通知迅速,则不会调用didRefreshRegistrationToken的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆