Send push notifications to Android

前端 未结 5 696
长情又很酷
长情又很酷 2021-01-13 00:17

I am currently developing Java web services that run on WebLogic on a server. These web services are called by a mobile application, which is developed by another team. Ther

5条回答
  •  半阙折子戏
    2021-01-13 00:34

    Send push notifications to Android

     private void pushNotification(String handlename) {
            StringEntity entity = null;
            JSONObject jsonObject = new JSONObject();
            String referesedtoken = FirebaseInstanceId.getInstance().getToken();
            try {
                jsonObject.put("to", clientToken);
                jsonObject.put("priority", "high");
                JSONArray jsonArray = new JSONArray();
                jsonArray.put(referesedtoken);
                JSONObject jsonObject1 = new JSONObject();
                jsonObject1.put("body", handlename);
                jsonObject1.put("commonContact", commonContact);
                jsonObject1.put("profileType", profileType);
                jsonObject1.put("notification-type", "chat");
                jsonObject1.put("target", handlename);
                jsonObject1.put("email", authPreferences.getEmailAddress());
                jsonObject1.put("UniqueId", authPreferences.getUserUid());
                jsonObject1.put("profilePicUrl", authPreferences.getMyProfilePicImageUrl());
                jsonObject1.put("title", profileBeenClasss.getFirstname() + " " + profileBeenClasss.getLastname() + " has sent message to you");
                jsonObject.put("registrationIDs", jsonArray);
                jsonObject.put("notification", jsonObject1);
                entity = new StringEntity(jsonObject.toString());
            } catch (JSONException e) {
                e.printStackTrace();
            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
            }
            AsyncHttpClient client = new AsyncHttpClient(true, 80, 443);
            client.setTimeout(80000);
            client.addHeader("Authorization", "key=AIzaSyCxKiM-LlNHfUOZ3QiiZfSGTo3vTAZdAAI");
            client.post(this, "https://fcm.googleapis.com/fcm/send", entity, "application/json", new TextHttpResponseHandler() {
                        @Override
                        public void onSuccess(int statusCode, Header[] headers, String res) {
                            System.out.println("Send Notification sucessfully" + res.toString());
                       }
    
                        @Override
                        public void onFailure(int statusCode, Header[] headers, String res, Throwable t) {
                            System.out.println("notification fail", res.toString());
                        }
    
                    }
            );
    
        }
    

提交回复
热议问题