0

I want to register a device with my Wordpress plugin. For this the Plguin uses the following code.

Sample request for new registration:
POST /pnfw/register/ HTTP/1.1
Host: yoursite
Content-Length: 26
Content-Type: application/x-www-form-urlencoded

token=new_device_id&os=iOS

Returns:
200 OK: on success.
401 Unauthorized: if OAuth is enabled but the signature is invalid (see below).
404 Not Found: if prevToken is not found on updating registration.
500 Internal Server Error: on missing mandatory parameters, unknown operating system, or general failure.
On errors:
On errors, besides the HTTP status codes defined above, a JSON is returned with this format:

This is the class the device registers with Firebase.

public class MyFirebaseInstanceIdService extends FirebaseInstanceIdService {
    private static final String REG_TOKEN = "REG_TOKEN";
    @Override
    public void onTokenRefresh() {

        String recent_token = FirebaseInstanceId.getInstance().getToken();
        Log.d(REG_TOKEN,recent_token);

        sendRegistrationToServer(recent_token);
    }

    public void sendRegistrationToServer(String recent_token) {


    }


}

I can not get the data from "recent_token" to my server. I tried the whole day.

 public void sendRegistrationToServer(String recent_token) throws IOException {
    String result = null;
    HttpURLConnection urlConnection = null;
    try {
        String postData = "token=" + recent_token + "&os=Android";
        byte[] postDataBytes = postData.getBytes("UTF-8");

        URL url = new URL("http://www.bluestarfish.de/pnfw/register/");
        urlConnection = (HttpURLConnection) url.openConnection();
        urlConnection.setConnectTimeout(30000);
        urlConnection.setReadTimeout(30000);
        urlConnection.setRequestMethod("POST");
        urlConnection.setDoOutput(true);
        urlConnection.setRequestProperty("POST", "/pnfw/register/ HTTP/1.1");
        urlConnection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
        urlConnection.setRequestProperty("charset", "UTF-8");
        urlConnection.setRequestProperty("Host", "www.bluestarfish.de");
        urlConnection.setRequestProperty("Content-Length", Integer.toString(postDataBytes.length));

        OutputStream out = urlConnection.getOutputStream();
        out.write(postDataBytes);
        out.close();
        //result = readStream(urlConnection.getInputStream());
    } finally {
        if (urlConnection != null) {
            urlConnection.disconnect();
        }
    }
    //return result;

    }

Where is the failure Unfortunately, nothing happens. Can someone explain the mistake to me?

  • Hey Karsten You'll have to provide a bit more detail here. What kind of RSS feed are you getting/creating from Firebase? What sort of notification are you looking to send? What did you already try to send such a notification? If you're finding it hard to explain things in English, keep in mind that we all speak `code` here. Explaining your question in code is often a great way to help bridge the language barrier. – Frank van Puffelen Jan 13 '18 at 18:54
  • It is better this way? Unfortunately, I am inexperienced and do the whole as a hobby project –  Jan 14 '18 at 11:56
  • That code definitely helps. You seem to be building a URL to send the token to a server. What part is giving you problems? – Frank van Puffelen Jan 14 '18 at 17:03
  • I can not get the data from "recent_token" to my server. I tried the whole day. –  Jan 14 '18 at 17:49
  • In your previous edit you were building a URL for calling the server. That seems like the right approach. The next step would be to [make an HTTP request from Android](https://stackoverflow.com/questions/3505930/make-an-http-request-with-android) to actually use that URL. – Frank van Puffelen Jan 14 '18 at 18:18
  • I can not get the data from "recent_token" to my server. I tried the whole day. Building a String was wrong for me. –  Jan 14 '18 at 18:36
  • I swapped Get with Post –  Jan 14 '18 at 18:46
  • Hey Karsten. To help with that, we'll need to see how you actually send the data to the server. The approach you got (likely from the Firebase documentation) looks fine, so the problem is bound to be in the code you wrote that actually tries to send the token to the server. – Frank van Puffelen Jan 14 '18 at 20:43
  • I do not have to speak with Firebase myself, but with a plugin. The only thing I have to do is to send the sample request to the address. The whole thing is done with the post method. –  Jan 14 '18 at 21:57
  • I must send this to my Server POST /pnfw/register/ HTTP/1.1 Host: yoursite Content-Length: 26 Content-Type: application/x-www-form-urlencoded token=new_device_id&os=iOS –  Jan 15 '18 at 19:45
  • Did you try reading the response you get back from the server? It'd normally be something like the line that is commented out: `//result = readStream(urlConnection.getInputStream());`. Reading the response will tell you what the server "though" of the request you sent. – Frank van Puffelen Jan 15 '18 at 21:53
  • there is a failure.... he cant resolve readStream –  Jan 17 '18 at 18:13

0 Answers0