Код: Выделить всё
Future\ sendNotificationF() async {
try {
String? accessToken = await CommonMethods.getAccessToken();
final \_dio = Dio();
// Set FCM HTTP v1 headers
_dio.options.headers = {
'Content-Type': 'application/json',
'Authorization': 'Bearer $accessToken',
};
FirebaseMessaging messaging = FirebaseMessaging.instance;
String? token = Platform.isAndroid
? await messaging.getToken()
: await messaging.getAPNSToken();
if (token == null) {
print("Error: Could not retrieve FCM token.");
Fluttertoast.showToast(
msg: "Error: Could not retrieve FCM token.",
toastLength: Toast.LENGTH_LONG);
return;
}
var data = {
"message": {
"token": token,
"data": {
"title": "Hello",
"message": "This is a notification message.",
"body": "This is the body content.",
"sound": "notification_chat",
},
"notification": {
"title": "Hello",
"body": "This is the notification body.",
},
"android": {
"priority": "high",
"notification": {
"sound": "notification_chat", // Custom sound for Android
"channel_id": "astrosane",
},
},
"apns": {
"payload": {
"aps": {
"sound": "notification_chat", // Custom sound for iOS
},
},
},
}
};
final response = await _dio.post(
'https://fcm.googleapis.com/v1/projects/project-ID/messages:send',
data: jsonEncode(data),
);
print("Notification sent successfully!");
} on DioError catch (e) {
print("Notification error: $e");
Fluttertoast.showToast(msg: e.response?.data.toString() ?? "An error occurred", toastLength: Toast.LENGTH_LONG);
}
}
{ошибка: {код: 400, сообщение: токен регистрации не является действительным токеном регистрации FCM, статус: INVALID_ARGUMENT, подробности: [{@type: type.googleapis.com/google.firebase.fcm.v1.FcmError, errorCode: INVALID_ARGUMENT}]
Подробнее здесь: https://stackoverflow.com/questions/791 ... rror-dioex