【问题标题】:Flutter FCM iOS Issue - APNS device token not set before retrieving FCM TokenFlutter FCM iOS 问题 - 在检索 FCM 令牌之前未设置 APNS 设备令牌
【发布时间】:2021-06-28 02:43:01
【问题描述】:

我在 Flutter 应用程序中使用 firebase_messaging v9.0.1。在基于https://pub.dev/packages/firebase_messaging/example 配置这个库时,我能够在前台和后台状态下收到 android 的通知。但是对于 iOS 来说同样是行不通的。我收到以下错误,

在检索发件人 ID '' 的 FCM 令牌之前未设置 APNS 设备令牌。此 FCM 令牌的通知将不会通过 APNS 传递。请确保在设置 APNS 设备令牌后重新检索 FCM 令牌。

我的 iOS 设备已连接到互联网,并且在运行时没有与网络相关的问题。

除了 iOS 的 FirebaseMessaging.instance.getToken() 之外,我还想调用任何其他函数吗? 请帮忙。

谢谢。

【问题讨论】:

  • 我也有同样的问题。

标签: ios flutter firebase-cloud-messaging apple-push-notifications


【解决方案1】:

我遇到了同样的问题,我一步一步地使用 firebase 文档,并意识到在调试和发布模式下缺少推送通知功能。

解决问题的步骤:

  1. 转到 xcode
  2. 选择跑步者
  3. 签名和功能
  4. 检查您需要具有后台模式和推送通知的调试和发布功能

https://firebase.flutter.dev/docs/messaging/apple-integration

【讨论】:

  • 我都设置好了,但还是报这个错误,有什么更新解决办法吗?
【解决方案2】:

我解决这个问题的方法是,我不是通过调试模式运行应用程序,而是存档应用程序并通过临时下载它,并以某种方式解决了问题

【讨论】:

  • 对我来说几乎一样。停止从 XCode 播放并从手机本身运行,它可以工作。
【解决方案3】:

我也收到此错误,但在我的情况下,我在 Firebase 中设置了 APN 证书而不是 APN 密钥。我必须在 Firebase 中更新我已过期的 APN 证书。这解决了错误,我可以在我的应用程序中使用 .getToken() 获取令牌。

【讨论】:

    【解决方案4】:

    尝试在真机上运行

    试试这个:

    AppDelegate.swift

    import UIKit
    import Flutter
    import Firebase
    import FirebaseMessaging
    import UserNotifications
    @UIApplicationMain
    @objc class AppDelegate: FlutterAppDelegate {
        var firebaseToken : String = "";
      override func application(
        _ application: UIApplication,
        didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?
      ) -> Bool {
        Messaging.messaging().delegate = self;
        FirebaseApp.configure()
        GeneratedPluginRegistrant.register(with: self)
        Messaging.messaging().isAutoInitEnabled = true;
        self.registerForFirebaseNotification(application: application);
        return true;
       // GeneratedPluginRegistrant.register(with: self)
        
      }
       override func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken : Data){
        print("X__APNS: \(String(describing: deviceToken))")
           Messaging.messaging().apnsToken = deviceToken;
        //Messaging.messaging().setAPNSToken(deviceToken, type:MessagingAPNSTokenType.prod )
        }
        
        override func application(_ application: UIApplication, didFailToRegisterForRemoteNotificationsWithError error: Error) {
            print("X_ERR",error);
        }
        
         func registerForFirebaseNotification(application : UIApplication){
        //    Messaging.messaging().delegate     = self;
            if #available(iOS 10.0, *) {
                //UNUserNotificationCenter.current().delegate = self ;
                let authOpt : UNAuthorizationOptions = [.alert, .badge, .sound];
                UNUserNotificationCenter.current().requestAuthorization(options: authOpt, completionHandler: {_, _ in})
                UNUserNotificationCenter.current().delegate = self ;
            }else{
                let settings : UIUserNotificationSettings = UIUserNotificationSettings(types: [.alert, .badge, .sound], categories: nil)                
                application.registerUserNotificationSettings(settings);
            }
            application.registerForRemoteNotifications();
        }
    }
    
    extension AppDelegate : MessagingDelegate{
        func messaging(_messagin : Messaging, didRecieveRegistrationToken fcmToken : String){
            self.firebaseToken = fcmToken;
            print("X__FCM: \(String(describing: fcmToken))")
        }
        func messaging(_messagin : Messaging, didRecieve remoteMessage : Any){
            //self.firebaseToken = fcmToken;
            print("REMOTE__MSG: \(remoteMessage)")
        }
        func application(_ application : UIApplication,didRecieveRemoteNotification uinfo: [AnyHashable : Any], fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void){
            print("WITH__APN: \(uinfo)")
        }
    }
    

    podfile

    target 'Runner' do
      use_frameworks!
      use_modular_headers!
    
    #  pod 'FirebaseFirestore', :git => 'https://github.com/invertase/firestore-ios-sdk-frameworks.git', :tag => '7.11.0'
    
      pod 'Firebase/Auth'
      pod 'Firebase/Analytics'
      pod 'FBSDKLoginKit'
      pod 'Firebase/Messaging'
    
      flutter_install_all_ios_pods File.dirname(File.realpath(__FILE__))
    end
    

    pubspec.yaml

      cloud_firestore: ^2.0.0
      firebase_auth: ^1.1.2
      firebase_core: ^1.1.0
      firebase_messaging: ^9.1.3
      flutter_local_notifications: ^5.0.0+3
    

    回复here

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-11-27
      • 2020-02-03
      • 2022-11-02
      • 2020-09-24
      • 2016-09-24
      • 1970-01-01
      • 2016-11-08
      • 1970-01-01
      相关资源
      最近更新 更多