Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Firebase (FCM) registration token in Flutter

I am trying to send notification from Java Rest Api (using Firebase Admin sdk) to my Flutter application and it seems it requires device token to send notification and I cannot find how to get that token. I am new to Flutter and android and may be missing any of the crucial step. Please help me if you can. Thanks.

like image 640
ajay datla Avatar asked Feb 22 '19 06:02

ajay datla


2 Answers

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

like image 141
Rahul Mahadik Avatar answered Oct 06 '22 13:10

Rahul Mahadik


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; }); 
like image 30
CopsOnRoad Avatar answered Oct 06 '22 14:10

CopsOnRoad