I’m utilizing Flutter to ship push notifications through Firebase Cloud Messaging (FCM) utilizing the HTTP v1 API. The notification works nice on Android, however I’m getting a 400 (dangerous request) error when sending to iOS units. Beneath is the code I’m utilizing:
Future sendNotificationF() async {
strive {
String? accessToken = await CommonMethods.getAccessToken();
remaining _dio = Dio();
// Set FCM HTTP v1 headers
_dio.choices.headers = {
'Content material-Kind': 'utility/json',
'Authorization': 'Bearer $accessToken',
};
FirebaseMessaging messaging = FirebaseMessaging.occasion;
String? token = Platform.isAndroid
? await messaging.getToken()
: await messaging.getAPNSToken();
if (token == null) {
print("Error: Couldn't retrieve FCM token.");
Fluttertoast.showToast(
msg: "Error: Couldn't retrieve FCM token.",
toastLength: Toast.LENGTH_LONG);
return;
}
var information = {
"message": {
"token": token,
"information": {
"title": "Hiya",
"message": "This can be a notification message.",
"physique": "That is the physique content material.",
"sound": "notification_chat",
},
"notification": {
"title": "Hiya",
"physique": "That is the notification physique.",
},
"android": {
"precedence": "excessive",
"notification": {
"sound": "notification_chat", // Customized sound for Android
"channel_id": "astrosane",
},
},
"apns": {
"payload": {
"aps": {
"sound": "notification_chat", // Customized sound for iOS
},
},
},
}
};
remaining response = await _dio.publish(
'https://fcm.googleapis.com/v1/tasks/project-ID/messages:ship',
information: jsonEncode(information),
);
print("Notification despatched efficiently!");
} on DioError catch (e) {
print("Notification error: $e");
Fluttertoast.showToast(msg: e.response?.information.toString() ?? "An error occurred", toastLength: Toast.LENGTH_LONG);
}
}
Error
{error: {code: 400, message: The registration token isn’t a legitimate FCM registration token, standing: INVALID_ARGUMENT, particulars: [{@type: type.googleapis.com/google.firebase.fcm.v1.FcmError, errorCode: INVALID_ARGUMENT}]}