Firebase (FCM) registration token in Flutter

We Are Going To Discuss About Firebase (FCM) registration token in Flutter. So lets Start this Java Article.

Firebase (FCM) registration token in Flutter

  1. Firebase (FCM) registration token in Flutter

    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

  2. Firebase (FCM) registration token in Flutter

    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

Solution 1

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<String, dynamic> message) async {
      print('on message $message');
    },
    onResume: (Map<String, dynamic> message) async {
      print('on resume $message');
    },
    onLaunch: (Map<String, dynamic> 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

Original Author Rahul Mahadik Of This Content

Solution 2

With firebase_messaging: ^10.0.0, you can directly get the token using

String? token = await FirebaseMessaging.instance.getToken();

or

FirebaseMessaging.instance.getToken().then((value) {
  String? token = value;
});

Original Author CopsOnRoad Of This Content

Solution 3

As you can the use the firebase Messaging Plugin to send the Notification. Through this code you can print the Token in Console.

final FirebaseMessaging _firebaseMessaging = FirebaseMessaging();
_firebaseMessaging.configure(
    onLaunch: (Map<String, dynamic> message) {
      print('onLaunch called');
    },
    onResume: (Map<String, dynamic> message) {
      print('onResume called');
    },
    onMessage: (Map<String, dynamic> message) {
      print('onMessage called');
    },
  );
  _firebaseMessaging.subscribeToTopic('all');
  _firebaseMessaging.requestNotificationPermissions(IosNotificationSettings(
    sound: true,
    badge: true,
    alert: true,
  ));
  _firebaseMessaging.onIosSettingsRegistered
      .listen((IosNotificationSettings settings) {
    print('Hello');
  });
  _firebaseMessaging.getToken().then((token) {
    print(token); // Print the Token in Console
  });
}

Original Author Neha Bhardwaj Of This Content

Solution 4

We need to add this package in pubspec.yaml file

firebase_messaging: ^4.0.0+1

Perform packages get

Now import this in your code

import 'package:firebase_messaging/firebase_messaging.dart';

Create instance of FirebaseMessaging

  FirebaseMessaging _firebaseMessaging = FirebaseMessaging();

Now we just to add the function which I have created in the answer in the link below

https://stackoverflow.com/a/60523014/11887774

Original Author Aman Raj Srivastava Of This Content

Conclusion

So This is all About This Tutorial. Hope This Tutorial Helped You. Thank You.

Also Read,

Siddharth

I am an Information Technology Engineer. I have Completed my MCA And I have 4 Year Plus Experience, I am a web developer with knowledge of multiple back-end platforms Like PHP, Node.js, Python and frontend JavaScript frameworks Like Angular, React, and Vue.

Leave a Comment