【问题标题】:Flutter firebase messaging, ios application don't receive tokenFlutter firebase消息传递,ios应用程序未收到令牌
【发布时间】:2018-10-10 21:23:00
【问题描述】:

我已经在 Flutter 应用中配置了 Firebase 消息传递。它使用firebase messaging plugin。我已根据自述文件中的“iOS 集成”部分进行配置。 Firebase 在 main.dart 中启动

void main() async {
  final FirebaseMessaging _firebaseMessaging = new FirebaseMessaging();
  _firebaseMessaging.requestNotificationPermissions();
  _firebaseMessaging.configure(onMessage: processMessage,
      onLaunch: processLaunch,
      onResume: processResume);
  String token = await _firebaseMessaging.getToken();
  print("fcm token is: $token");
  runApp(TestApp());
}

Future<dynamic> processMessage(Map<String, dynamic> map) async {
  print("received message:");
  print(map);
}

Future<dynamic> processLaunch(Map<String, dynamic> map) async {
  print("processing launch");
  print(map);
}

Future<dynamic> processResume(Map<String, dynamic> map) async {
  print("processing resume");
  print(map);
}

问题是应用没有收到令牌。所以我将应用程序部署到物理设备,应用程序启动,但没有看到任何与 fcm 相关的输出,并且没有显示 ui。我在 IDEA 中看到以下日志:

5.10.0 - [Firebase/Core][I-COR000003] The default Firebase app has not yet been configured. Add `[FIRApp configure];` (`FirebaseApp.configure()` in Swift) to your application initialization. Read more: .
5.10.0 - [Firebase/Messaging][I-FCM001000] FIRMessaging Remote Notifications proxy enabled, will swizzle remote notification receiver handlers. If you'd prefer to manually integrate Firebase Messaging, add "FirebaseAppDelegateProxyEnabled" to your Info.plist, and set it to NO. Follow the instructions at:
https://firebase.google.com/docs/cloud-messaging/ios/client#method_swizzling_in_firebase_messaging

哪里有问题?

【问题讨论】:

    标签: ios firebase dart flutter firebase-cloud-messaging


    【解决方案1】:

    当我将 firebase 代码移至 TestApp 时它开始工作,似乎 main.dart 是 fcm 代码的错误位置。我把它放在initState里面。

    【讨论】:

    • 对不起.. TestApp 到底是什么意思?
    • @Amir Aslam 这是主要小部件
    【解决方案2】:

    截至 2018 年 10 月 11 日,getToken 实现存在错误(请参阅问题 1769920378)。

    有一个pull request 来修复它,但它仍在等待中。

    目前,为了避免getToken 出现问题,我建议收听onTokenRefresh(我就是这样做的)。

    我没有测试下面的代码,但这只是一个示例,说明您必须如何调整您的代码。

    String _token;
    
    void main() async {
      final FirebaseMessaging _firebaseMessaging = new FirebaseMessaging();
      _firebaseMessaging.requestNotificationPermissions();
    
      Stream<String> fcmStream = _firebaseMessaging.onTokenRefresh;
      fcmStream.listen((token) {
        // saveToken(token);
        print("fcm token is: $token");
        _token = token;     
      });
    
      _firebaseMessaging.configure(onMessage: processMessage,
          onLaunch: processLaunch,
          onResume: processResume);
    
      runApp(TestApp());
    }
    

    【讨论】:

    • 感谢您的回复@Feu,在我的情况下,当我将 firebase 相关代码移入 TestAppinitState 方法时,它开始工作
    猜你喜欢
    • 2020-10-19
    • 2019-11-08
    • 2019-10-08
    • 2020-07-16
    • 2020-11-07
    • 2020-12-31
    • 1970-01-01
    • 2020-06-16
    • 2021-03-21
    相关资源
    最近更新 更多