Per push-Benachrichtigung vom server auf das android-Gerät in Java

Bin ich zu kämpfen mit dem neuen FCM... ich verwendet FCM vor, jetzt versuche ich, FCM...

Ich versuche zum senden von push-Benachrichtigungen für meine app-server auf android-Gerät.

I wanna verwenden Sie die standard-Java-Paket ist, versuchen Sie nicht, andere, wie zum Beispiel Vert.x, apache httpClient etc...

hier ist mein code:

public void sendNotification(String messageBody)
{
    try
    {
        URL url = new URL("https://fcm.googleapis.com/fcm/send");
        HttpURLConnection conn = (HttpURLConnection) url.openConnection();
        conn.setDoOutput(true);
        conn.setDoInput(true);
        conn.setRequestMethod("POST");
        conn.setRequestProperty("Content-Type", "application/json");

        String apiKey = "AI...wE";

        String credentials = "key=" + apiKey;
        //String basicAuth = "Basic " + Base64.getEncoder().encodeToString(credentials.getBytes());

        String basicAuth = "Basic " + new String(Base64.encodeBase64(credentials.getBytes()));

        conn.setRequestProperty ("Authorization", basicAuth);

        String notfnStr = "{\"body\": \"this is my body\", \"title\": \"this is my title\"}";
        String dataStr = "{\"key1\": \"value1\", \"key2\": \"value2\"}";

        String bodyStr = "{\"priority\": \"high\", \"to\": \"dFC8GW0N1Q8:APA91bHePPmC7QVV16LGnR6rqxwreHSv1GgawijZ_dZL9T70ZkiXIV8TW_ymAWkvFfXRiWJmtR_UGBXBv2iV2UhS8M-Tndw8sf8ZW6zIqfaiiVJao3G5HFbhqgA18ukNNtW_J7JaWkz8\", " +
                "\"notification\": " + notfnStr + ", \"data\": " + dataStr + "}";

        System.out.println("### input: " + bodyStr);

        OutputStream os = conn.getOutputStream();
        os.write(bodyStr.getBytes());
        os.flush();

        if (conn.getResponseCode() != HttpURLConnection.HTTP_CREATED) {
            throw new RuntimeException("Failed : HTTP error code : " + conn.getResponseCode());
        }

        BufferedReader br = new BufferedReader(new InputStreamReader(
                (conn.getInputStream())));

        String output;
        System.out.println("Output from Server .... \n");
        while ((output = br.readLine()) != null) {
            System.out.println(output);
        }

        conn.disconnect();
    }
    catch (Exception e)
    {
        e.printStackTrace();
    }
}

den Antwort-code, die ich habe ist 401, was bedeutet, dass nicht autorisierte... ich denke, das format des Berechtigungsnachweises falsch ist...

Den json-string ist im gültigen json-format, so dass ich nicht die Mühe zu verwenden JSONObject.

Für die string-Anmeldeinformationen, ich habe versucht, "key:" + apiKey; aber immer noch das gleiche Ergebnis.

Den apiKey String kopiert aus der google-Dienste.json habe ich heruntergeladen von der FB-Konsole in google.

google nicht geben Sie ein gutes Beispiel... gab mir nur dieses: https://firebase.google.com/docs/cloud-messaging/downstream

Wenn einer weiß, wie es geht, bitte Antworten. Danke!!

InformationsquelleAutor fkie4 | 2016-06-29
Schreibe einen Kommentar