简体   繁体   中英

Flutter firebase messaging, ios application don't receive token

I have configured firebase messaging in flutter app. It use firebase messaging plugin . I have configured according to "iOS Integration" section from readme. Firebase is inited in 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);
}

The problem is app don't receive token. So I deploy application to physical device, application starts, but don't see any output related to fcm, and ui not shown. I see following logs in 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

Where can be problem?

It started to work when I moved firebase code to TestApp , seems like main.dart is wrong place for fcm code. I placed it inside initState .

As of October 11 2018, the getToken implementation is faulty (see issues 17699 and 20378 ).

There's a pull request to fix it, but it's still pending.

For now, in order to avoid problems with getToken , I would suggest listening to onTokenRefresh (that's what I do).

I didn't test the code below, but it's just an example of how you would have to adapt your code.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM