why my response.body is empty and my device token in my backend is null?

171

You are capturing the FCM token correctly, just send it to your backend using a post method, and be prepared on your backend to capture this post. The FCMtoken is a string.

You can also send it along with the user ID for example, store them, then you can use them to send notifications to this user, because you have the userId and their device's FCMtoken.

Share:
171
Yara Abd
Author by

Yara Abd

Updated on November 23, 2022

Comments

  • Yara Abd
    Yara Abd over 1 year

    I am Using Laravel for my App backend and want to send push notification to my flutter app my question is why response body is empty and device token in the backend is null?

    static Future<http.Response> sendDeviceToken(String FCM_token) async{
    
        SharedPreferences sharedPreferences = await SharedPreferences.getInstance();
        int id=sharedPreferences.getInt("id");
        String myUrl = "$serverUrl/FCM";
        var resp = await http.post(Uri.encodeFull(myUrl),
          headers: <String, String>{
            "Accept": "application/json",
          },
          body: jsonEncode(<String, String>{
            'FCM_token': FCM_token,
            'id' : id.toString(),
          }),
        );
        print("res.statusCode: ${resp.statusCode}");
        print("res.body: ${resp.body}");
        return resp;
      }
    

    i print FCM_token inside this function and it print correct.

    • Huthaifa Muayyad
      Huthaifa Muayyad over 2 years
      You are capturing the token correctly, just send it to your backend, it's a string. Send it with the user ID and store them, then you can use them to send notifications to this device.
    • Frank van Puffelen
      Frank van Puffelen over 2 years
      Sounds like an answer @HuthaifaMuayyad ;-)
    • Huthaifa Muayyad
      Huthaifa Muayyad over 2 years
      As always, lots of love @FrankvanPuffelen <3
  • Yara Abd
    Yara Abd over 2 years
    how can i send it by using post method? @Huthaifa
  • Huthaifa Muayyad
    Huthaifa Muayyad over 2 years
    Send it to your server, so your server can store it in your database. Write a simple post request from your flutter app. If you don't know how to do that, that is where I advise you to start looking :) check here.
  • Huthaifa Muayyad
    Huthaifa Muayyad over 2 years
    What happens when you run this code? Where is the problem?
  • Yara Abd
    Yara Abd over 2 years
    resp.body print empty and in the backend the value is null