问题
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.
回答1:
Add this to your package's pubspec.yaml file:
dependencies:
firebase_messaging: ^4.0.0+1
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
回答2:
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
回答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
});
}
回答4:
How would you convert the onmessage future to a stream so that it can be used in a bloc. Ex:
This doesnt work:
Stream<MessagesState> _yieldMessage(Map<String, dynamic> message) async* {
yield NewMessageState(message: message);
}
Stream<MessagesState> _mapToInitialiseFirebaseEvent() async* {
_firebaseMessaging.configure(
onMessage: (Map<String, dynamic> message) async {
print("onMessage: $message");
_yieldMessage(message);
},
onLaunch: (Map<String, dynamic> message) async {
print("onLaunch: $message");
_yieldMessage(message);
},
onResume: (Map<String, dynamic> message) async {
print("onResume: $message");
_yieldMessage(message);
},
);
_firebaseMessaging.requestNotificationPermissions(const IosNotificationSettings(sound: true, badge: true, alert: true, provisional: true));
_firebaseMessaging.onIosSettingsRegistered.listen((IosNotificationSettings settings) {
print("Settings registered: $settings");
});
_firebaseMessaging.getToken().then((String token) async {
assert(token != null);
print("Push Messaging token: $token");
Settings settings = await userRepository.loadSettingsFromStore();
sosRepository.updateUser(User(username: settings.userName, imeinumber: token));
});
}
回答5:
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
}
}
来源:https://stackoverflow.com/questions/54821182/firebase-fcm-registration-token-in-flutter