I am trying to send firebase push notifications via Flutter. Below is the code
Future<Map<String, dynamic>> sendAndRetrieveMessage(String fcmToken, String title, String msg) async {
await _firebaseMessaging.requestNotificationPermissions(
const IosNotificationSettings(
sound: true, badge: true, alert: true, provisional: false),
);
await http.post(
'https://fcm.googleapis.com/fcm/send',
headers: <String, String>{
'Content-Type': 'application/json',
'Authorization': 'key=$serverToken',
},
body: convert.jsonEncode(
<String, dynamic>{
'notification': <String, dynamic>{'body': msg, 'title': title},
'priority': 'high',
'data': <String, dynamic>{
'click_action': 'FLUTTER_NOTIFICATION_CLICK',
'id': '1',
'status': 'done'
},
'to': fcmToken,
},
),
).then((http.Response response){
final int statusCode = response.statusCode;
print("RESPONSE: " + response.body);
print("STATUS CODE: " + statusCode.toString());
if (statusCode < 200 || statusCode > 400 || response.body == null) {
throw new Exception("Error while fetching data");
}
});
}
Below is the error I get
RESPONSE: {"multicast_id":8498089791993785359,"success":0,"failure":1,"canonical_ids":0,"results":[{"error":"NotRegistered"}]}
Surprisingly, I can use the same FCM Token via the Firebase UI console to send messages. It simply works.
Update
for the to field, If I use the code await _firebaseMessaging.getToken() I dont get the error and it get passed.The above is for getting the FCM of the same user, which is a string.
I know my fcm token of other users are not incorrect, because when used in Firebase console, it works.
What is the issue here?