使用 FCM 和 Flutter 通过设备发送通知
Send notification over device using FCM and Flutter
目前我正在尝试向多个设备发送通知。我已经阅读了有关如何 send notification to device group , they mentioned that I need to use registration_ids: [...]
instead of to: token
. And I also read somewhere that they mentioned about notification_key
which is it will enabled to send the notification to the other device. So, I'm stuck on finding the key. But then, after few days browsing, I found out that here 说明 notification_key
已弃用的文档。所以,我想问问你们有没有人知道如何在不使用控制台的情况下向多个设备发送通知。
这是我发送推送通知的代码段:
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");
}
如果我使用 to
而不是 registration_ids
,它工作正常,但据我所知,如果我只想为一个设备发送通知,则使用 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");
}
目前我正在尝试向多个设备发送通知。我已经阅读了有关如何 send notification to device group , they mentioned that I need to use registration_ids: [...]
instead of to: token
. And I also read somewhere that they mentioned about notification_key
which is it will enabled to send the notification to the other device. So, I'm stuck on finding the key. But then, after few days browsing, I found out that here 说明 notification_key
已弃用的文档。所以,我想问问你们有没有人知道如何在不使用控制台的情况下向多个设备发送通知。
这是我发送推送通知的代码段:
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");
}
如果我使用 to
而不是 registration_ids
,它工作正常,但据我所知,如果我只想为一个设备发送通知,则使用 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");
}