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.
Asked
Active
Viewed 8.7k times
52
-
@KENdi i will wan to know if you were able to get result on this, and if you do, i appreciate if you can assist me – Anana Aristotle Sep 03 '19 at 14:15
-
Use rest API instead. https://arkapp.medium.com/flutter-fcm-rest-api-7719925f2e3e – abdul rehman Apr 15 '21 at 02:45
-
@abdulrehman I have followed your suggested link https://arkapp.medium.com/flutter-fcm-rest-api-7719925f2e3e but notification does not have any sound. Could you please suggest me what is mean of 'default' sound? Can we change it with custom sound line 'blink.wav', 'alert.mp3'? Thanks a lot. – Kamlesh Sep 06 '21 at 13:26
5 Answers
80
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;
});

CopsOnRoad
- 237,138
- 77
- 654
- 440
56
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

Rahul Mahadik
- 11,668
- 6
- 41
- 54
-
It's working except when app is open do you have any suggestion to solve that in flutter – Shashank Pujari Mar 30 '19 at 09:15
-
3In order to show the notification even when the app is open or is running, you can use the flutter_local_notifications dependency. Please check this git repo for more info https://github.com/nitishk72/firebase_messaging_flutter/blob/master/lib/main.dart – nike Jul 02 '19 at 09:07
-
I tried same as flutter doc of FirebaseMessaging. I even cloned the sample project from flutter's github.But i still get null with getToken.This is what I get in log: W/FirebaseInstanceId( 4066):Token retrieval failed:SERVICE_NOT_AVAILABLE. Will retry token retrieval W/FA ( 4066): Failed to retrieve Firebase Instance Id E/FirebaseInstanceId( 4066): Failed to get FIS auth token I/FA ( 4066): Tag Manager is not found and thus will not be used W/FirebaseMessagingPlugin( 4066): getToken, error fetching instanceID: W/FirebaseMessagingPlugin( 4066): java.io.IOException: AUTHENTICATION_FAILED – Prabesh Mar 14 '20 at 10:34
-
-
I recommend using overlay_support it has a fancy look and easy to implement https://pub.dev/packages/overlay_support – Mohammed Fadhl Mar 17 '21 at 13:17
-
@rahul Rahul - could you please update your answer as per latest version of firebase fcm package? which will help me and other users. Thanks a lot. – Kamlesh Sep 01 '21 at 06:44
7
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
});
}

Rafa0809
- 1,733
- 21
- 24

Neha Bhardwaj
- 1,275
- 10
- 13
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

Aman Raj Srivastava
- 294
- 3
- 5
-10
I am not clear your question though. For FCM you have to extend FirebaseMessagingService.
Example:
class PNPFirebaseMessagingService : FirebaseMessagingService() {
override fun onNewToken(token: String?) {
// you can collect token from here
}
}

Farid Haq
- 3,728
- 1
- 21
- 15
-
-
this is kotlin code not related to flutter it should be in dart – EL TEGANI MOHAMED HAMAD GABIR Nov 12 '22 at 19:54