Send push notifications from server with FCM

后端 未结 6 941
滥情空心
滥情空心 2020-11-28 06:46

Recently I asked a question on sending push notifications using GCM: Send push notifications to Android. Now that there is FCM, I am wondering how different it would be from

6条回答
  •  隐瞒了意图╮
    2020-11-28 07:27

    public class SendPushNotification extends AsyncTask {
    
        private final String FIREBASE_URL = "https://fcm.googleapis.com/fcm/send";
        private final String SERVER_KEY = "REPLACE_YOUR_SERVER_KEY";
        private Context context;
        private String token;
    
        public SendPushNotification(Context context, String token) {
            this.context = context;
            this.token = token;
        }
    
        @Override
        protected Void doInBackground(Void... voids) {
    
            /*{
                "to": "DEVICE_TOKEN",
                "data": {
                "type": "type",
                    "title": "Android",
                    "message": "Push Notification",
                    "data": {
                        "key": "Extra data"
                    }
                }
            }*/
    
            try {
                URL url = new URL(FIREBASE_URL);
                HttpURLConnection connection = (HttpURLConnection) url.openConnection();
    
                connection.setUseCaches(false);
                connection.setDoInput(true);
                connection.setDoOutput(true);
    
                connection.setRequestMethod("POST");
                connection.setRequestProperty("Content-Type", "application/json");
                connection.setRequestProperty("Accept", "application/json");
                connection.setRequestProperty("Authorization", "key=" + SERVER_KEY);
    
                JSONObject root = new JSONObject();
                root.put("to", token);
    
                JSONObject data = new JSONObject();
                data.put("type", "type");
                data.put("title", "Android");
                data.put("message", "Push Notification");
    
                JSONObject innerData = new JSONObject();
                innerData.put("key", "Extra data");
    
                data.put("data", innerData);
                root.put("data", data);
                Log.e("PushNotification", "Data Format: " + root.toString());
    
                try {
                    OutputStreamWriter writer = new OutputStreamWriter(connection.getOutputStream());
                    writer.write(root.toString());
                    writer.flush();
                    writer.close();
    
                    int responseCode = connection.getResponseCode();
                    Log.e("PushNotification", "Request Code: " + responseCode);
    
                    BufferedReader bufferedReader = new BufferedReader(new InputStreamReader((connection.getInputStream())));
                    String output;
                    StringBuilder builder = new StringBuilder();
                    while ((output = bufferedReader.readLine()) != null) {
                        builder.append(output);
                    }
                    bufferedReader.close();
                    String result = builder.toString();
                    Log.e("PushNotification", "Result JSON: " + result);
                } catch (Exception e) {
                    e.printStackTrace();
                    Log.e("PushNotification", "Error: " + e.getMessage());
                }
    
            } catch (Exception e) {
                e.printStackTrace();
                Log.e("PushNotification", "Error: " + e.getMessage());
            }
    
            return null;
        }
    }
    

    Use

    SendPushNotification sendPushNotification = new SendPushNotification(context, "token");
    sendPushNotification.execute();
    

提交回复
热议问题