简体   繁体   English

Flutter 中的 Firebase (FCM) 注册令牌

[英]Firebase (FCM) registration token in Flutter

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.我正在尝试从Java Rest Api(使用Firebase Admin sdk )向我的Flutter 应用程序发送通知,它似乎需要设备令牌才能发送通知,但我找不到如何获取该令牌。 I am new to Flutter and android and may be missing any of the crucial step.我是 Flutter 和 android 的新手,可能缺少任何关键步骤。 Please help me if you can.如果可以,请你帮助我。 Thanks.谢谢。

Add this to your package's pubspec.yaml file:将此添加到您的包的 pubspec.yaml 文件中:

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:现在在您的 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 this link有关更多详细信息,请参阅链接

Hope this helps you希望这对你有帮助

As you can the use the firebase Messaging Plugin to send the Notification.你可以使用 firebase 消息插件来发送通知。 Through this code you can print the Token in Console.通过此代码,您可以在控制台中打印 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
  });
}

With firebase_messaging: ^10.0.0 , you can directly get the token using使用firebase_messaging: ^10.0.0 ,您可以使用直接获取令牌

String? token = await FirebaseMessaging.instance.getToken();

or或者

FirebaseMessaging.instance.getToken().then((value) {
  String? token = value;
});

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现在我们只是添加我在下面链接的答案中创建的函数

https://stackoverflow.com/a/60523014/11887774 https://stackoverflow.com/a/60523014/11887774

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
  }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM