stavros.3p
stavros.3p

Reputation: 2324

Unable to pass data to a POST request using Volley library - Android

I am trying to send a firebase push notification from my server. I trigger the .php file by sending a POST request using Volley like this:

public static void dataPacket(Activity activity, Map<String, String> bundle)
    {
        new AsyncTask<String, Boolean, Boolean>()
        {
            protected Boolean doInBackground(String... params)
            {
                String url  = CloudData.TEST_DATA_PACKET;
                RequestQueue queue = Volley.newRequestQueue(activity);

                StringRequest postRequest = new StringRequest(Request.Method.POST, url,
                        response -> {
                            // response
                            Log.d("Response", response);
                        },
                        error -> {
                            error.printStackTrace();
                            Log.d("Error.Response", error.toString());
                        }
                ) {
                    @Override
                    protected Map<String, String> getParams()
                    {
                        return bundle;
                    }

                    @Override
                    public Map<String, String> getHeaders()
                    {
                        Map<String, String>  headerParams = new HashMap<>();
                        headerParams.put("Content-Type", "application/json; charset=utf-8");

                        return headerParams;
                    }//*/
                };

                postRequest.setRetryPolicy(new DefaultRetryPolicy(
                        10000,
                        0,
                        DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));//*/

                queue.add(postRequest);
                return true;
            }
        }.execute();
    }

This is my .php file in my server:

<?php
    require 'FCMPushNotification.php'; 

    $FCMPushNotification = new \BD\FCMPushNotification('MY_API_KEY');

    $sDeviceToken = $_POST['token'];

    $aPayload = array(
        'data' => array("test"=>123),
        'notification' => array(
            'title' => 'Example app',
            'body'=> 'This is an example message',
            'sound'=> 'default'
        )
    );

    $aOptions = array(
        'time_to_live' => 0 //means messages that can't be delivered immediately are discarded. 
    );

    $aResult = $FCMPushNotification->sendToDevice(
        $sDeviceToken,      
        $aPayload,
        $aOptions // optional
    );

    var_dump($aResult);  

 ?>

When I change this line

$sDeviceToken = $_POST['token'];

to this

$sDeviceToken = 'my_firebase_token_for_a_device';

everything works fine. But when I try to get the variable 'token' from POST request android gives me this error:

BasicNetwork.performRequest: Unexpected response code 500 for https://mywebsite.com/sendnotification.php W/System.err: com.android.volley.ServerError W/System.err: at com.android.volley.toolbox.BasicNetwork.performRequest(BasicNetwork.java:179) W/System.err: at com.android.volley.NetworkDispatcher.run(NetworkDispatcher.java:114) D/Error.Response: com.android.volley.ServerError

What am I doing wrong?

Update

This is how I call dataPacket:

PostTaskListener<String> postTaskListener = result ->
        {
            bundle.put("token",result); 
            SendNotification.dataPacket(activity,bundle);
        };
        FirebaseCloud.getFirebaseToken(db.getActiveProfile().cID,postTaskListener,activity);

Upvotes: 0

Views: 171

Answers (1)

Daniel Protopopov
Daniel Protopopov

Reputation: 7236

You specified body format as application/json, and put through as an array(form-data), that may be affecting the result. The format should be multipart/form-data (or application/x-www-form-urlencoded, as per wiki).

Upvotes: 2

Related Questions