Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/112.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/firebase/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 如果用户不';不允许快速通知_Ios_Firebase_Swift3_Firebase Cloud Messaging - Fatal编程技术网

Ios 如果用户不';不允许快速通知

Ios 如果用户不';不允许快速通知,ios,firebase,swift3,firebase-cloud-messaging,Ios,Firebase,Swift3,Firebase Cloud Messaging,在我尝试了许多可能解决此问题的问题后,我的问题没有得到解决: 我想获得FCM令牌。在AppDelegate.swift中,我已导入 import UserNotifications import Firebase import FirebaseInstanceID import FirebaseMessaging 在此之后,我使用UnuseNotificationCenterDelegate、MessagingDelegate对其进行了扩展,然后在didFinishLaunchingWithO

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

我想获得
FCM令牌
。在
AppDelegate.swift
中,我已导入

import UserNotifications
import Firebase
import FirebaseInstanceID
import FirebaseMessaging
在此之后,我使用
UnuseNotificationCenterDelegate、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)")
    }
}
希望有帮助。

如中所述,您可以:

  • 提供符合
    firmMessageDelegate
    协议的代表
  • 侦听名为
    kFIRMessagingRegistrationTokenRefreshNotification的NSNotification
  • 如果选择第一个选项,则需要设置委托

    Messaging.messaging().delegate = self
    
    您可以在中看到这一点