Java Flutter 中的 Firebase (FCM) 注册令牌
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/54821182/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Firebase (FCM) registration token in Flutter
提问by ajay datla
I am trying to send notification from JavaRest Api (using Firebase Admin sdk) to my Flutter applicationand 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.
我正在尝试从JavaRest Api(使用Firebase Admin sdk)向我的Flutter 应用程序发送通知,它似乎需要设备令牌才能发送通知,但我找不到如何获取该令牌。我是 Flutter 和 android 的新手,可能缺少任何关键步骤。如果可以,请你帮助我。谢谢。
回答by Farid Haq
I am not clear your question though. For FCM you have to extend FirebaseMessagingService.
我不清楚你的问题。对于 FCM,您必须扩展 FirebaseMessagingService。
Example:
例子:
class PNPFirebaseMessagingService : FirebaseMessagingService() {
override fun onNewToken(token: String?) {
// you can collect token from here
}
}
回答by Neha Bhardwaj
As you can the use the firebase Messaging Plugin to send the Notification. Through this code you can print the Token in Console.
您可以使用 firebase 消息插件来发送通知。通过此代码,您可以在控制台中打印 Token。
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
});
}
回答by Rahul Mahadik
Add this to your package's pubspec.yaml file:
将此添加到您的包的 pubspec.yaml 文件中:
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:
现在在您的 Dart 代码中,您可以使用:
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 thislink
有关更多详细信息,请参阅此链接
Hope this helps you
希望这对你有帮助
回答by Theuno de Bruin
How would you convert the onmessage future to a stream so that it can be used in a bloc. Ex:
您将如何将 onmessage 未来转换为流,以便它可以在块中使用。前任:
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));
});
}
回答by Aman Raj Srivastava
We need to add this package in pubspec.yaml file
我们需要在 pubspec.yaml 文件中添加这个包
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 = FirebaseMessaging();
Now we just to add the function which I have created in the answer in the link below
现在我们只是添加我在下面链接的答案中创建的函数