Firebase Notifications through server-side code

后端 未结 3 1497
爱一瞬间的悲伤
爱一瞬间的悲伤 2020-12-29 00:22

The new Firebase notifications service allows us to use a Console UI to post notifications to all mobile app users.

But I could not find any REST API for the Fireba

3条回答
  •  梦毁少年i
    2020-12-29 01:01

    @Narendra Naidu, Hi you can try this code snippet for server side push notification. create simple java class in your sever side project code and add this method with parameter you also need some firebase credential to do this. please try following.

    // Method to send Notifications from server to client end.
    public final static String AUTH_KEY_FCM = "ApidhfkIjd_cAdhpa-ZZ065hskiH53Hw3g";
    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();
    }
    

    Please go through this ref document:

    1. https://firebase.google.com/docs/cloud-messaging/http-server-ref
    2. https://firebase.google.com/docs/cloud-messaging/server

    it provides you server end information for sending notification from your server to - firebase server-to client application. Also, Find this below plain java code file (server end class) from which you will get some quick idea on same.

    Please do let me know if I can be of further assistance.

提交回复
热议问题