Firebase(FCM)登记令牌中的flutter



我正在尝试从 java ret rest api发送通知(使用 firebase admin admin sdk Flutter Application ,似乎需要设备令牌发送通知,我找不到如何获取该令牌。我是Flutter和Android的新手,可能会缺少任何关键步骤。如果可以,请你帮助我。谢谢。

使用firebase_messaging: ^10.0.0,您可以直接使用

获得令牌
String? token = await FirebaseMessaging.instance.getToken();

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

将其添加到您的软件包的pubspec.yaml文件:

dependencies:
  firebase_messaging: ^6.0.16

您可以从命令行安装软件包:

flutter:

$ flutter packages get

现在在您的飞镖代码中,您可以使用:

import 'package:firebase_messaging/firebase_messaging.dart';

实现:

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");
  });
}

有关更多详细信息,请参阅此链接

希望这对您有帮助

,您可以使用firebase消息传递插件发送通知。通过此代码,您可以在控制台中打印令牌。

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

我们需要在pubspec.yaml文件

中添加此软件包
firebase_messaging: ^4.0.0+1

执行软件包获取

现在将其导入您的代码

import 'package:firebase_messaging/firebase_messaging.dart';

创建firebasemessaging的实例

  FirebaseMessaging _firebaseMessaging = FirebaseMessaging();

现在,我们只是添加我在下面的链接中的答案中创建的函数

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

我不清楚您的问题。对于FCM,您必须扩展Firebasemessagingservice。

示例:

class PNPFirebaseMessagingService : FirebaseMessagingService() {
  override fun onNewToken(token: String?) {
     // you can collect token from here
  }
}

相关内容

  • 没有找到相关文章

最新更新