I'm trying to send a notification automatically to driver with the following code:
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),
);
}
However, the push notification doesn't go through.
But when I use postman with this code:
{"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"}
It works. I don't know which part I did wrong.
Here are the results of the print statements above:
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}
I finally managed to get it to work. I used the following codes:
static sendNotificationToDriver(String token, context, String rideRequestId) async {
if (token == null) {
print('Unable to send FCM message, no token exists.');
return;
}
try {
await http.post(
Uri.parse('https://fcm.googleapis.com/fcm/send'),
headers: <String, String>{
'Content-Type': 'application/json; charset=UTF-8',
'Authorization': serverToken,
},
body: constructFCMPayload(token, rideRequestId),
);
print('FCM request for device sent!');
} catch (e) {
print(e);
}
}
static String constructFCMPayload(String token, String rideRequestId) {
var res = jsonEncode({
'token': token,
'notification': {
"body" : "You have a 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": rideRequestId,
},
'to': token,
});
print(res.toString());
return res;
}
However, the code wasn't the problem itself, but the serverToken
variable. No wonder why it works using Postman and not in app because I forgot to put "key="
at the beginning of the serverToken
value. Now, the problem is solved.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.