Firebase (FCM) registration token in Flutter

前端 未结 4 1441
暗喜
暗喜 2021-02-12 14:26

I am trying to send notification from Java Rest Api (using Firebase Admin sdk) to my Flutter application and it seems it requ

4条回答
  •  清歌不尽
    2021-02-12 15:18

    Add this to your package's pubspec.yaml file:

    dependencies:
      firebase_messaging: ^6.0.16
    

    You can install packages from the command line:

    with Flutter:

    $ flutter packages get
    

    Now in your Dart code, you can use:

    import 'package:firebase_messaging/firebase_messaging.dart';
    

    Implementation:

    FirebaseMessaging _firebaseMessaging = FirebaseMessaging();
    
    
    
    @override
    void initState() {
      super.initState();
      firebaseCloudMessaging_Listeners();
    }
    
    void firebaseCloudMessaging_Listeners() {
      if (Platform.isIOS) iOS_Permission();
    
      _firebaseMessaging.getToken().then((token){
        print(token);
      });
    
      _firebaseMessaging.configure(
        onMessage: (Map message) async {
          print('on message $message');
        },
        onResume: (Map message) async {
          print('on resume $message');
        },
        onLaunch: (Map message) async {
          print('on launch $message');
        },
      );
    }
    
    void iOS_Permission() {
      _firebaseMessaging.requestNotificationPermissions(
          IosNotificationSettings(sound: true, badge: true, alert: true)
      );
      _firebaseMessaging.onIosSettingsRegistered
          .listen((IosNotificationSettings settings)
      {
        print("Settings registered: $settings");
      });
    }
    

    For more details step by information please refer this link

    Hope this helps you

提交回复
热议问题