Flutter 使用FCM和颤振通过设备发送通知

Flutter 使用FCM和颤振通过设备发送通知,flutter,dart,push-notification,firebase-cloud-messaging,Flutter,Dart,Push Notification,Firebase Cloud Messaging,目前我正在尝试向多个设备发送通知。我已经阅读了关于如何注册的文档,他们提到我需要使用registration\u id:[…]而不是to:token。我还读到他们提到的notification\u key,它可以将通知发送到其他设备。所以,我一直在寻找钥匙。但是,在浏览了几天之后,我发现上面说,notification\u键已经被弃用了。所以,我想问你们中是否有人知道如何在不使用控制台的情况下向多个设备发送通知 这是我要发送推送通知的代码段: try { await http.post(

目前我正在尝试向多个设备发送通知。我已经阅读了关于如何注册的文档,他们提到我需要使用
registration\u id:[…]
而不是
to:token
。我还读到他们提到的
notification\u key
,它可以将通知发送到其他设备。所以,我一直在寻找钥匙。但是,在浏览了几天之后,我发现上面说,
notification\u键
已经被弃用了。所以,我想问你们中是否有人知道如何在不使用控制台的情况下向多个设备发送通知

这是我要发送推送通知的代码段:

try {
  await http.post(
    Uri.parse('https://fcm.googleapis.com/fcm/send'),
    // Uri.parse('https://fcm.googleapis.com/fcm/notification'),

    headers: <String, String>{
      'Content-Type': 'application/json; charset=UTF-8',
      'Authorization': 'key=$serverKey',
      'project_id':'....',
    },
    body: jsonEncode(
      <String, dynamic>{
        'notification': <String, dynamic>{
          'body': 'this is a body2',
          'title': 'this is a title'
        },
        'priority': 'high',
        'data': <String, dynamic>{
          'click_action':
          'FLUTTER_NOTIFICATION_CLICK',
          'id': '1',
          'status': 'done'
        },
        'registration_ids': ['fbN9aYoORhihksrTo7j5D2:APA91b......', 'fArqpgKrTJ0fL8SUKddy2F:APA91bFWf1cnVMS8.......'],
        // 'to': _token,
      },
    ),
  );
} catch (e) {
  print("error push notification");
}
试试看{
等待http.post(
parse('https://fcm.googleapis.com/fcm/send'),
//parse('https://fcm.googleapis.com/fcm/notification'),
标题:{
“内容类型”:“应用程序/json;字符集=UTF-8”,
“授权”:“key=$serverKey”,
“项目编号”:“项目编号”,
},
正文:JSONECODE(
{
“通知”:{
“body”:这是一个body2,
“标题”:“这是一个标题”
},
“优先级”:“高”,
“数据”:{
“单击操作”:
“颤振\u通知\u点击”,
“id”:“1”,
“状态”:“完成”
},
“注册ID:[“FBN9ayooorHihksrto7J5D2:APA91b……”、“fArqpgKrTJ0fL8SUKddy2F:APA91bFWf1cnVMS8……”,
//“收件人”:,
},
),
);
}捕获(e){
打印(“错误推送通知”);
}
如果我使用
to
而不是
registration\u id
,它可以正常工作,但据我所知,如果我只想为一个设备发送通知,则使用to


这个问题我已经坚持了三天,仍然没有找到任何解决办法。他们中的大多数人使用的是控制台。你的帮助真的会让我高兴的。提前谢谢你

我找到了一个解决方案和它的工作

 var serverKey =
    'AAAAUb...';
QuerySnapshot ref =
    await FirebaseFirestore.instance.collection('users').get();


try {
  ref.docs.forEach((snapshot) async {
    http.Response response = await http.post(
      Uri.parse('https://fcm.googleapis.com/fcm/send'),
      headers: <String, String>{
        'Content-Type': 'application/json',
        'Authorization': 'key=$serverKey',
      },
      body: jsonEncode(
        <String, dynamic>{
          'notification': <String, dynamic>{
            'body': 'this is a body',
            'title': 'this is a title'
          },
          'priority': 'high',
          'data': <String, dynamic>{
            'click_action': 'FLUTTER_NOTIFICATION_CLICK',
            'id': '1',
            'status': 'done'
          },
          'to': snapshot.data() ['tokenID'],
        },
      ),
    );
  });
} catch (e) {
  print("error push notification");
}
var-serverKey=
“AAAAUb…”;
QuerySnapshot参考=
等待FirebaseFirestore.instance.collection('users').get();
试一试{
ref.docs.forEach((快照)异步{
http.Response-Response=等待http.post(
parse('https://fcm.googleapis.com/fcm/send'),
标题:{
“内容类型”:“应用程序/json”,
“授权”:“key=$serverKey”,
},
正文:JSONECODE(
{
“通知”:{
“body”:这是一个body,
“标题”:“这是一个标题”
},
“优先级”:“高”,
“数据”:{
“点击动作”:“颤振通知点击”,
“id”:“1”,
“状态”:“完成”
},
“to”:snapshot.data()['tokenID'],
},
),
);
});
}捕获(e){
打印(“错误推送通知”);
}