Flutter FCM Notification Works on Android however Fails on iOS with 400 Error (DioException)

0
18
Flutter FCM Notification Works on Android however Fails on iOS with 400 Error (DioException)


I’m utilizing Flutter to ship push notifications by way of Firebase Cloud Messaging (FCM) utilizing the HTTP v1 API. The notification works effective on Android, however I’m getting a 400 (dangerous request) error when sending to iOS units. Under is the code I’m utilizing:

Future sendNotificationF() async {
strive {
String? accessToken = await CommonMethods.getAccessToken();
ultimate _dio = Dio();

    // Set FCM HTTP v1 headers
    _dio.choices.headers = {
      'Content material-Sort': '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": "Good day",
          "message": "This can be a notification message.",
          "physique": "That is the physique content material.",
          "sound": "notification_chat",
        },
        "notification": {
          "title": "Good day",
          "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
            },
          },
        },
      }
    };
    
    ultimate response = await _dio.put up(
      'https://fcm.googleapis.com/v1/initiatives/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 is just not a sound FCM registration token, standing: INVALID_ARGUMENT, particulars: [{@type: type.googleapis.com/google.firebase.fcm.v1.FcmError, errorCode: INVALID_ARGUMENT}]}

LEAVE A REPLY

Please enter your comment!
Please enter your name here