如何从云端代码上的Appengine服务器发送FCM消息工作本地服务器

时间:2017-05-31 05:13:41

标签: google-app-engine firebase firebase-cloud-messaging

如何在没有结算的云AppEngine中执行此操作。 我可以从Javascript获取令牌,但发送消息到服务器进行通知。为下游数据/通知创建JSON对象。在服务器上说套接字的计费。

 private void localappengine(HttpServletResponse response, String ENDPOINT_URL) throws IOException {

    HttpClient client = new DefaultHttpClient();

    Log.info("after client");
    HttpPost post = new HttpPost(ENDPOINT_URL);
 String deviceToken="cthG-hesotM:APA91bGg_tLg7TqvpY4aAvzHpyBK2mTTOT2KgO94tDFcLGPakcS9vmXkYEIe4Vh0Mo5ka1COfaXarUEJGWyqDdmVi_kujUfKDtE4C30eZwkPQATXnFrDPJxHxd8iouwsWuRcAk-ZYe_4";
    PrintWriter out=response.getWriter();
    response.getWriter().println("Hello from myservlet " );
    // Create JSON object for downstream data/notification
    JSONObject mainNotificationJsonObj = new JSONObject();
    JSONObject outerBaseJsonObj = new JSONObject();
    try {

        // Notification payload has 'title' and 'body' key
        mainNotificationJsonObj.put("title", "testing message");
        mainNotificationJsonObj.put("body", "Hello I sent it");

        // This will be used in case of both 'notification' or 'data' payload
        outerBaseJsonObj.put("to", deviceToken);


        // Set priority of notification. For instant chat setting
        // high will
        // wake device from idle state - HIGH BATTERY DRAIN
        //outerBaseJsonObj.put(PRIORITY_KEY, PRIORITY_HIGH);

        // Specify required payload key here either 'data' or
        // 'notification'. We can even use both payloads in single
        // message
        outerBaseJsonObj.put("notification", mainNotificationJsonObj);
    } catch (JSONException e) {

        e.printStackTrace();
    }
    Log.info("before entity");
    // Setup http entity with json data and 'Content-Type' header
    StringEntity requestEntity = new StringEntity(outerBaseJsonObj.toString());
    //(outerBaseJsonObj.toString(), APPLICATION_JSON);
    String FIREBASE_SERVER_KEY= "key=AAAA6nN2BxI:APA91bHYotXML0siwL0Pm0LK5iXQ9Ik1kQtdB1ALbJrm5kseUk2zS5gJs6AMHVsX86exEE-JFsIF962YNY1yRyl3yFxGCyMBAH4OKwTn8Ff6vcd6vJMVXutNlP99X8AtOsW8_JIBkyEl";
    // Setup required Authorization header
    post.setHeader("Authorization", FIREBASE_SERVER_KEY);
    post.setHeader("Content-Type", String.valueOf(APPLICATION_JSON));
    Log.info("after header");
    // Pass setup entity to post request here
    post.setEntity(requestEntity);

    // Execute apache http client post response
    HttpResponse fcmResponse = client.execute(post);

    Log.info("after client execute");
    // Get status code from FCM server to debug error and success
    System.out.println("RESPONSE_CODE" + fcmResponse.getStatusLine().getStatusCode());

    // Get response entity from FCM server and read throw lines
    BufferedReader rd = new BufferedReader(new InputStreamReader(fcmResponse.getEntity().getContent()));

    StringBuffer result = new StringBuffer();
    String line = "";
    while ((line = rd.readLine()) != null) {
        result.append(line);
    }

    if (response != null) {

        // Print out the response to webpage
        PrintWriter out1;
        out1 = response.getWriter();
        out1.println(result);
        System.out.println("This is Result - " + result);
    }
}

0 个答案:

没有答案