从Java服务器推送通知

时间:2017-08-24 06:57:27

标签: java firebase firebase-cloud-messaging

当我想从服务器(java)向Firebase发送通知而不是向设备发送通知时,有人可以向我解释推送通知的完整过程吗?

在我的下面示例中,结果是Android Notification Response:

{
    "multicast_id":7880177496704383133,
    "success":0,
    "failure":1,
    "canonical_ids":0,
    "results":[{"error":"InvalidRegistration"}]
 }

这是由于错误" TO"价值来自何处?

我已经创建了一个虚拟Firebase项目包含   包com.push.notification;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;

import net.sf.json.JSONException;
import net.sf.json.JSONObject;

public class FCM {

final static private String FCM_URL = "https://fcm.googleapis.com/fcm/send";



/**

 * 

 * Method to send push notification to Android FireBased Cloud messaging 
 Server.

 * 

 * @param tokenId Generated and provided from Android Client Developer

 * @param server_key Key which is Generated in FCM Server 

 * @param message which contains actual information.

 * 

 */

static void send_FCM_Notification(String tokenId, String server_key, String 
message){



    try{

        // Create URL instance.

        URL url = new URL(FCM_URL);



        // create connection.

        HttpURLConnection conn;

        conn = (HttpURLConnection) url.openConnection();



        conn.setUseCaches(false);

        conn.setDoInput(true);

        conn.setDoOutput(true);



        //set method as POST or GET

        conn.setRequestMethod("POST");



        //pass FCM server key

        conn.setRequestProperty("Authorization","key="+server_key);



        //Specify Message Format

        conn.setRequestProperty("Content-Type","application/json");



        //Create JSON Object & pass value

        JSONObject infoJson = new JSONObject();

        infoJson.put("title","Here is your notification.");

        infoJson.put("body", message);



        JSONObject json = new JSONObject();

        json.put("to",tokenId.trim());

        json.put("notification", infoJson);



        OutputStreamWriter wr = new 
         OutputStreamWriter(conn.getOutputStream());

        wr.write(json.toString());

        wr.flush();

        int status = 0;

        if( null != conn ){

            status = conn.getResponseCode();

        }

        if( status != 0){



            if( status == 200 ){

                //SUCCESS message

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

                System.out.println("Android Notification Response : " + 
                 reader.readLine());

            }else if(status == 401){

                //client side error

                System.out.println("Notification Response : TokenId : " + 
                  tokenId + " Error occurred :");

            }else if(status == 501){

                //server side error

                System.out.println("Notification Response : [ 
                errorCode=ServerError ] TokenId : " + tokenId);

            }else if( status == 503){

                //server side error

                System.out.println("Notification Response : FCM Service is 
                    Unavailable  TokenId : " + tokenId);

            }
        }

    }catch(MalformedURLException mlfexception){

        // Prototcal Error

        System.out.println("Error occurred while sending push 
             Notification!.." + mlfexception.getMessage());

    }catch(IOException mlfexception){

        //URL problem

        System.out.println("Reading URL, Error occurred while sending push 
             Notification!.." + mlfexception.getMessage());

    }catch(JSONException jsonexception){

        //Message format error

        System.out.println("Message Format, Error occurred while sending 
              push Notification!.." + jsonexception.getMessage());

    }catch (Exception exception) {

        //General Error or exception.

        System.out.println("Error occurred while sending push 
        Notification!.." + exception.getMessage());

    }

}

}

0 个答案:

没有答案