如何通过java而不是FirebaseConsole发送Firebase推送通知?

时间:2017-12-03 23:38:43

标签: java android firebase-cloud-messaging

我正在进入Firebase云消息传递,我想知道如何使用java代码在推送通知中发送消息,而不是手动将其写入Firebase控制台。

提前致谢。

3 个答案:

答案 0 :(得分:2)

目前唯一的选择是从Java代码调用Firebase Cloud Messaging的REST API。

此处介绍了REST API:https://firebase.google.com/docs/cloud-messaging/send-message。您还想了解authenticating the request,其中包括Java示例。

如果你还没有从那些工作中得到它,也可以查看一些previous questions about the topic

答案 1 :(得分:1)

//Add dependency... 
implementation 'com.android.volley:volley:1.1.0'


private String strServerKey = "Your server key";
private String strAuthorization = "key=" + strServerKey;

//Put JSON data...

JSONObject jsonMain = new JSONObject();
jsonMain.put("to", token);

JSONObject jsonNotification = new JSONObject();
jsonNotification.put("title", "Notification Title");
jsonNotification.put("body", "Notification Body");
jsonMain.put("notification", jsonNotification);

JSONObject jsonData = new JSONObject();
jsonData.put("title", "Data Payload Title");
jsonData.put("body", "Data Payload Body");
jsonMain.put("data", jsonData);

String sendJsonData=jsonMain.toString();


StringRequest stringRequest = new StringRequest(Request.Method.POST, strFcmUrl,
        new Response.Listener<String>() {
            @Override
            public void onResponse(String response) {
                //
            }
        }, new Response.ErrorListener() {
    @Override
    public void onErrorResponse(VolleyError error) {
       //
    }
}) {
    @Override
    public byte[] getBody() {
        return sendJsonData.getBytes();
    }

    @Override
    public Map<String, String> getHeaders() {
        Map<String, String> params = new HashMap<>();
        params.put("Content-Type", "application/json; charset=UTF-8");
        params.put("Authorization", strAuthorization);
        return params;
    }
};
MyApplication.getInstance().addToRequestQueue(stringRequest);




public class MyApplication extends Application 
{
    private static Context context;
    public static Context getContext() {
        return context;
    }

    public static final String TAG = AppController.class.getSimpleName();
    private static AppController mInstance;
    private RequestQueue mRequestQueue;


    @Override
    public void onCreate() {
        super.onCreate();
        context = getApplicationContext();
        mInstance = this;
    }

    public static synchronized AppController getInstance() {
        return mInstance;
    }

    public RequestQueue getRequestQueue() {
        if (mRequestQueue == null) {
            mRequestQueue = Volley.newRequestQueue(getApplicationContext());
        }
        return mRequestQueue;
    }
    public <T> void addToRequestQueue(Request<T> req) {
        req.setTag(TAG);
        getRequestQueue().add(req);
    }
}       

答案 2 :(得分:-1)

我是使用api密钥和移动令牌在python中完成的。 在java中,我相信你可以做这样的事情

public final static String AUTH_KEY_FCM = "xyxyxyxxyxyxyxyxyxyxyxy";
public final static String API_URL_FCM = "https://fcm.googleapis.com/fcm/send";

// userDeviceIdKey is the device id you will query from your database

public static void pushFCMNotification(String userDeviceIdKey) throws Exception{

   String authKey = AUTH_KEY_FCM; // You FCM AUTH key
   String FMCurl = API_URL_FCM; 

   URL url = new URL(FMCurl);
   HttpURLConnection conn = (HttpURLConnection) url.openConnection();

   conn.setUseCaches(false);
   conn.setDoInput(true);
   conn.setDoOutput(true);

   conn.setRequestMethod("POST");
   conn.setRequestProperty("Authorization","key="+authKey);
   conn.setRequestProperty("Content-Type","application/json");

   JSONObject json = new JSONObject();
   json.put("to",userDeviceIdKey.trim());
   JSONObject info = new JSONObject();
   info.put("title", "Notificatoin Title"); // Notification title
   info.put("body", "Hello Test notification"); // Notification body
   json.put("notification", info);

   OutputStreamWriter wr = new OutputStreamWriter(conn.getOutputStream());
   wr.write(json.toString());
   wr.flush();
   conn.getInputStream();
}

See

对于我的python实现,我使用了from pyfcm import FCMNotification库。 See