void main() async{
WidgetsFlutterBinding.ensureInitialized();
await Firebase.initializeApp(
options: DefaultFirebaseOptions.currentPlatform
);
final _firebaseMessaing = FirebaseMessaging.instance;
await _firebaseMessaing.requestPermission();
final fCMToken = await _firebaseMessaing.getToken();
print("This is token =========> ${fCMToken}");
runApp(MyApp());
}
Here I want the token but some errors occurs and the Token does not print. Not getting why I am facing this problem?
Unhandled Exception: [firebase_messaging/unknown] java.io.IOException: SERVICE_NOT_AVAILABLE
E/flutter (17731): #0 StandardMethodCodec.decodeEnvelope
(package:flutter/src/services/message_codecs.dart:651:7)
E/flutter (17731): #1 MethodChannel._invokeMethod
(package:flutter/src/services/platform_channel.dart:322:18)
E/flutter (17731): <asynchronous suspension>
E/flutter (17731): #2 MethodChannel.invokeMapMethod
(package:flutter/src/services/platform_channel.dart:522:43)
E/flutter (17731): <asynchronous suspension>
E/flutter (17731): #3 MethodChannelFirebaseMessaging.getToken
2
Answers
Got the answer to this. Mainly shit issue appeared because of my emulator issue. Because google services were being stopped for some issue. Reinstalling emulator and also updating emulator from SDK manager solved the issue. Task done:
you’ve got _firebaseMessaing.RequestPermission(). This line requests notification permissions from the user. Ensure this call is a success before asking for the token. You can add a test for the permission fame using _firebaseMessaing.GetNotificationSettings() and handle cases in which permission is denied.