FCM getToken() always returns null on real devices, but is working just fine on emulator, I don't know what causes this. Here's how I use getToken():
FirebaseMessaging _firebaseMessaging = FirebaseMessaging();
String fcmToken = "";
await _firebaseMessaging.getToken().then((value) async {
fcmToken = value;
if (fcmToken != "") {
await _userCollection.doc(user.id).set({
'email': user.email,
'name': user.name,
'noHp': user.noHp,
'alamat': user.alamat,
"email_verification": user.emailVerification,
"phone_verification": user.phoneVerification,
"device_token": fcmToken,
});
}
return;
});
The real device that I use is Android Marshmallow.
Below is the method for et Token :
final FirebaseMessaging _firebaseMessaging = FirebaseMessaging();
_firebaseMessaging.getToken().then((String token) {
assert(token != null);
saveToken(token);
});
I am saving token in pref for further use. you can modify it as per you requirement.
void saveToken(String token) async {
var prefs = await SharedPreferences.getInstance();
await prefs.setString(Constants.DeviceToken, token);
}
Feel free to comment if any issue. I am using in my current application.
For iOS you need to ask for permission.
_firebaseMessaging.requestNotificationPermissions(
const IosNotificationSettings(
sound: true, badge: true, alert: true, provisional: true));
_firebaseMessaging.onIosSettingsRegistered
.listen((IosNotificationSettings settings) {
print("Settings registered: $settings");
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With