如何将fcm通知推送设备发送到特定设备?

时间:2017-12-08 12:27:34

标签: java android push-notification firebase-cloud-messaging

我正在尝试在两台设备之间发送推送通知。通过一些研究,这里是我的文件。这是我的FCMNotificationService类的详细信息。有人可以帮我做。我在那儿厨房很长一段时间。 谢谢!

import android.os.AsyncTask;

import com.google.firebase.FirebaseApp;
import com.proper_international.properinternationnal.miscs.Utilities;

import org.json.JSONException;
import org.json.JSONObject;

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.ProtocolException;
import java.net.URL;



public class FCMNotificationService extends AsyncTask<String, Void, Boolean> {

    // Method to send Notifications from server to client end.
    public final static String AUTH_KEY_FCM = "-----";

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



    @Override
    protected Boolean doInBackground(String... strings) {

        // public static void pushFCMNotification(String deviceIdKey, String titre, String contenu, String userRole, String latLng, String idReserve) throws Exception {
        String authKey = AUTH_KEY_FCM; // You FCM AUTH key
        String FMCurl = API_URL_FCM;
        boolean r = false;
        URL url = null;
        try {
            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 data = new JSONObject();
            data.put("to", strings[0]);
            JSONObject info = new JSONObject();
            info.put("title", strings[1]); // Notification title
            info.put("body", strings[2]); // Notification body
            info.put("latLng", strings[3]);
            info.put("idReserve", strings[4]);
            data.put("data", info);
            data.put("notification", info);

            OutputStreamWriter wr = new OutputStreamWriter(conn.getOutputStream());
            wr.write(data.toString());
            wr.flush();
            wr.close();

            int responseCode = conn.getResponseCode();
            System.out.println("Response Code : " + responseCode);

            BufferedReader in = new BufferedReader(new InputStreamReader(conn.getInputStream()));
            String inputLine;
            StringBuffer response = new StringBuffer();

            while ((inputLine = in.readLine()) != null) {
                response.append(inputLine);
            }
            in.close();
            r = true;

        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return r;
    }


}

但问题是如何找到我想要发送通知的设备的令牌。

0 个答案:

没有答案