【发布时间】:2022-01-17 23:25:33
【问题描述】:
我正在尝试使用以下代码自动向驱动程序发送通知:
static sendNotificationToDriver(String token, context, String ride_request_id) async
{
var destionation = Provider.of<AppData>(context, listen: false).dropOffLocation;
print("this is the token: ");
print(token);
Map<String, String> headerMap =
{
'Content-Type': 'application/json',
'Authorization': serverToken,
};
print(headerMap);
Map notificationMap =
{
'body': 'DropOff Address, ${destionation!.placeName}',
'title': 'New Ride Request'
};
print(notificationMap);
Map dataMap =
{
'click_action': 'FLUTTER_NOTIFICATION_CLICK',
'id': '1',
'status': 'done',
'ride_request_id': ride_request_id,
};
print(dataMap);
Map sendNotificationMap =
{
"notification": notificationMap,
"data": dataMap,
"priority": "high",
"to": token,
};
print(sendNotificationMap);
var res = await http.post(
Uri.parse('https://fcm.googleapis.com/fcm/send'),
headers: headerMap,
body: jsonEncode(sendNotificationMap),
);
}
但是,推送通知没有通过。
但是当我用这个代码使用邮递员时:
{"notification" : {"body" : "New Ride Request! Tap here to view in the app.",
"title": "New Ride Request"}, "priority": "high", "data": {"click_action": "FLUTTER_NOTIFIATION_CLICK",
"id": "1", "status": "done", "ride_request_id": "-MqrfzH5DKC8hTYQHy35"},
"to" : "dTlAVB_KTe-XZTLEYwZL10:APA91bF-D-Uh5Yb5p2uCwRr8oT90R_BXOw9qCUGMGsWqlp9RNc-WlyyNfJOKSwPArvAa3062F2XxlUJbY6xibWCYZCDPGqvaQG4xvfgByyyJ9SBxvhAgDmaP35Mqr3dfJADmprMQJ-aL"}
它有效。我不知道我做错了什么。
以下是上述打印语句的结果:
I/flutter ( 1288): {Content-Type: application/json, Authorization: AAAAUecji90:APA***********************}
I/flutter ( 1288): {body: DropOff Address, ACE Centerpoint Gen. San. Drive Wing, title: New Ride Request}
I/flutter ( 1288): {click_action: FLUTTER_NOTIFICATION_CLICK, id: 1, status: done, ride_request_id: -MqrfCLsW3Ncmfav7hdk}
I/flutter ( 1288): {notification: {body: DropOff Address, ACE Centerpoint Gen. San. Drive Wing, title: New Ride Request}, data: {click_action: FLUTTER_NOTIFICATION_CLICK, id: 1, status: done, ride_request_id: -MqrfCLsW3Ncmfav7hdk}, priority: high, to: dTlAVB_KTe-XZTLEYwZL10:APA91bF-D-Uh5Yb5p2uCwRr8oT90R_BXOw9qCUGMGsWqlp9RNc-WlyyNfJOKSwPArvAa3062F2XxlUJbY6xibWCYZCDPGqvaQG4xvfgByyyJ9SBxvhAgDmaP35Mqr3dfJADmprMQJ-aL}
【问题讨论】:
-
你试过在请求数据中不使用
jsonEncode吗? -
不,我没有尝试不使用它。是否有其他方法可以在应用内实现自动化?
标签: firebase flutter push-notification firebase-cloud-messaging http-post