web-dev-qa-db-fra.com

Firebase Java Server pour envoyer une notification Push à tous les périphériques

J'essaie d'envoyer une notification Push à mon appareil Android avec le nouveau service Firebase . J'ai enregistré et configuré une application. J'ai également mis tout le code nécessaire pour recevoir une notification dans l'application Android . Via la console Firebase. Je peux envoyer une notification à mon application et celle-ci est reçue et affichée . Maintenant, je souhaite écrire un serveur autonome Java, pour envoyer une notification à TOUS les appareils. Ceci est mon code actuel:

final String apiKey = "I added my key here";
URL url = new URL("https://fcm.googleapis.com/fcm/send");
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setDoOutput(true);
conn.setRequestMethod("POST");
conn.setRequestProperty("Content-Type", "application/json");
conn.setRequestProperty("Authorization", "key=" + apiKey);

conn.setDoOutput(true);

String input = "{\"notification\" : {\"title\" : \"Test\"}, \"to\":\"test\"}";

OutputStream os = conn.getOutputStream();
os.write(input.getBytes());
os.flush();
os.close();

int responseCode = conn.getResponseCode();
System.out.println("\nSending 'POST' request to URL : " + url);
System.out.println("Post parameters : " + input);
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();

// print result
System.out.println(response.toString());

Et voici le résultat que je reviens de leurs serveurs:

{"multicast_id":6602141464107786356,"success":0,"failure":1,"canonical_ids":0,"results":[{"error":"InvalidRegistration"}]}

Malheureusement, le simple fait de supprimer la balise "to" ne fonctionne pas, un code 400 est renvoyé puis . J'ai lu que je devais enregistrer le périphérique, envoyer l'ID de périphérique au serveur, le sauvegarder à cet emplacement puis en boucle. pour envoyer le message à tous les périphériques enregistrés sur le serveur . N'existe-t-il pas un moyen plus simple d'envoyer simplement un message à tous les périphériques, comme dans la console?

Votre aide est vraiment appréciée, car j'essaie de faire en sorte que cela fonctionne toute la journée = (

Cordialement, Dustin

15
user3907491

Je ne crois pas que ce soit possible. Au lieu de cela, je suggérerais d'enregistrer tous les périphériques sur le même sujet et vous pourrez ensuite envoyer un message à tous les périphériques en même temps. Voici la documentation d'aide à ce sujet:

Envoyer des messages de sujet à partir du serveur

https://firebase.google.com/docs/cloud-messaging/topic-messaging

11
Alexander N.

Cette solution envoie une notification Push à Firebase à l'aide d'Apache HttpClient:

HttpClient client = HttpClientBuilder.create().build();
HttpPost post = new HttpPost("https://fcm.googleapis.com/fcm/send");
post.setHeader("Content-type", "application/json");
post.setHeader("Authorization", "key=AIzaSyBSxxxxsXevRq0trDbA9mhnY_2jqMoeChA");

JSONObject message = new JSONObject();
message.put("to", "dBbB2BFT-VY:APA91bHrvgfXbZa-K5eg9vVdUkIsHbMxxxxxc8dBAvoH_3ZtaahVVeMXP7Bm0iera5s37ChHmAVh29P8aAVa8HF0I0goZKPYdGT6lNl4MXN0na7xbmvF25c4ZLl0JkCDm_saXb51Vrte");
message.put("priority", "high");

JSONObject notification = new JSONObject();
notification.put("title", "Java");
notification.put("body", "Notificação do Java");

message.put("notification", notification);

post.setEntity(new StringEntity(message.toString(), "UTF-8"));
HttpResponse response = client.execute(post);
System.out.println(response);
System.out.println(message);
9
Geucimar

Utilisez le code ci-dessous pour envoyer des notifications Push à plusieurs appareils:

import Java.io.IOException;
import Java.util.ArrayList;
import Java.util.List;
import com.google.Android.gcm.server.Message;
import com.google.Android.gcm.server.MulticastResult;
import com.google.Android.gcm.server.Result;
import com.google.Android.gcm.server.Sender;

public class PushNotifactionHelper {
public final static String AUTH_KEY_FCM = "your key ";
public final static String API_URL_FCM = "https://fcm.googleapis.com/fcm/send";

public static void sendPushNotification(List<String> deviceTokenList) {
    Sender sender = new Sender(AUTH_KEY_FCM);
    Message msg = new Message.Builder().addData("message", "Message body")
            .build();
    try {
        MulticastResult result = sender.send(msg, deviceTokenList, 5);
        for (Result r : result.getResults()) {
            if (r.getMessageId() != null) 
                System.out.println("Push Notification Sent Successfully");
             else 
                System.out.println("ErrorCode " + r.getErrorCodeName());
        }
    } catch (IOException e) {
        System.out.println("Error " + e.getLocalizedMessage());
    }
   }
  }   

Remarque: utilisez gcm.server.jar 

9
Sandip S.

Votre JSON sera comme ça:

      {
      "registration_ids": [
                        "fcm token 1",
                        "fcm token 2",
                        "fcm token 3"
                    ],
      "data": {
                "message": "msg"
              },
      "notification": {
                "title": "App name",
                "text": " your msg"
            }
    }
public void sendGroupPush(Context context, ArrayList tokenlist, String message) {
            String msg = message;
            String title = context.getString(R.string.app_name);
            JSONArray regId = null;
            JSONObject objData = null;
            JSONObject data = null;
            JSONObject notif = null;
            try {
                regId = new JSONArray();
                for (int i = 0; i < tokenlist.size(); i++) {
                    regId.put(tokenlist.get(i));
                }
                data = new JSONObject();
                data.put("message", message);
                notif = new JSONObject();
                notif.put("title", title);
                notif.put("text", msg);

                objData = new JSONObject();
                objData.put("registration_ids", regId);
                objData.put("data", data);
                objData.put("notification", notif);
                Log.e("!_@rj@_group_PASS:>", objData.toString());
            } catch (JSONException e) {
                e.printStackTrace();
            }

            JsonObjectRequest jsObjRequest = new JsonObjectRequest(Request.Method.POST, Constants.FCM_Push_GROUP_URL, objData,
                    new Response.Listener<JSONObject>() {
                        @Override
                        public void onResponse(JSONObject response) {
                            Log.e("!_@rj@_@@_SUCESS", response + "");
                        }
                    },
                    new Response.ErrorListener() {
                        @Override
                        public void onErrorResponse(VolleyError error) {
                            Log.e("!_@rj@_@@_Errors--", error + "");
                        }
                    }) {
                @Override
                public Map<String, String> getHeaders() throws AuthFailureError {
                    Map<String, String> params = new HashMap<String, String>();
                    params.put("Authorization", "key=" + Constants.FCM_API_KEY);
                    params.put("Content-Type", "application/json");
                    Log.e("!_@rj@_@@Push_headrers", "::> " + params);
                    return params;
                }
            };
            RequestQueue requestQueue = Volley.newRequestQueue(context);
            int socketTimeout = 1000 * 60;// 60 seconds
            RetryPolicy policy = new DefaultRetryPolicy(socketTimeout, DefaultRetryPolicy.DEFAULT_MAX_RETRIES, DefaultRetryPolicy.DEFAULT_BACKOFF_MULT);
            jsObjRequest.setRetryPolicy(policy);
            requestQueue.add(jsObjRequest);
        }

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

6
Rjz Satvara

Ici, j'ai aussi une solution très simple pour envoyer un groupe de messages Push individuel 

public class FCM {
final static private String FCM_URL = "https://fcm.googleapis.com/fcm/send";

/**
* 
* Method to send Push notification to Android FireBased Cloud messaging 
Server.
* @param tokenId Generated and provided from Android Client Developer
* @param server_key Key which is Generated in FCM Server 
   @param message which contains actual information.
* 
*/

static void send_FCM_Notification(String tokenId, String server_key, String 

message){


    try{
// Create URL instance.
URL url = new URL(FCM_URL);
// create connection.
HttpURLConnection conn;
conn = (HttpURLConnection) url.openConnection();
conn.setUseCaches(false);
conn.setDoInput(true);
conn.setDoOutput(true);
//set method as POST or GET
conn.setRequestMethod("POST");
//pass FCM server key
conn.setRequestProperty("Authorization","key="+server_key);
//Specify Message Format
conn.setRequestProperty("Content-Type","application/json");
//Create JSON Object & pass value
 JSONObject infoJson = new JSONObject();

 infoJson.put("title","Alankit");
infoJson.put("body", message);

JSONObject json = new JSONObject();
json.put("to",tokenId.trim());
json.put("notification", infoJson);

System.out.println("json :" +json.toString());
System.out.println("infoJson :" +infoJson.toString());
OutputStreamWriter wr = new OutputStreamWriter(conn.getOutputStream());
wr.write(json.toString());
wr.flush();
int status = 0;
if( null != conn ){
status = conn.getResponseCode();
}
if( status != 0){

if( status == 200 ){
//SUCCESS message
BufferedReader reader = new BufferedReader(new 
InputStreamReader(conn.getInputStream()));
System.out.println("Android Notification Response : " + reader.readLine());
}else if(status == 401){
//client side error
System.out.println("Notification Response : TokenId : " + tokenId + " Error occurred :");
}else if(status == 501){
//server side error
System.out.println("Notification Response : [ errorCode=ServerError ] TokenId : " + tokenId);
}else if( status == 503){
//server side error
System.out.println("Notification Response : FCM Service is Unavailable  
TokenId : " + tokenId);
}
}
}catch(MalformedURLException mlfexception){
// Prototcal Error
System.out.println("Error occurred while sending Push Notification!.." + mlfexception.getMessage());
}catch(Exception mlfexception){
//URL problem
System.out.println("Reading URL, Error occurred while sending Push 
Notification!.." + mlfexception.getMessage());
}

}

 static void send_FCM_NotificationMulti(List<String> putIds2, String 
tokenId, 
 String server_key, String message){
    try{
    // Create URL instance.
    URL url = new URL(FCM_URL);
    // create connection.
    HttpURLConnection conn;
    conn = (HttpURLConnection) url.openConnection();
    conn.setUseCaches(false);
    conn.setDoInput(true);
    conn.setDoOutput(true);
    //set method as POST or GET
    conn.setRequestMethod("POST");
    //pass FCM server key
    conn.setRequestProperty("Authorization","key="+server_key);
    //Specify Message Format
    conn.setRequestProperty("Content-Type","application/json");
    //Create JSON Object & pass value

        JSONArray regId = null;
        JSONObject objData = null;
        JSONObject data = null;
        JSONObject notif = null;

            regId = new JSONArray();
            for (int i = 0; i < putIds2.size(); i++) {
                regId.put(putIds2.get(i));
            }
            data = new JSONObject();
            data.put("message", message);
            notif = new JSONObject();
            notif.put("title", "Alankit Universe");
            notif.put("text", message);

            objData = new JSONObject();
            objData.put("registration_ids", regId);
            objData.put("data", data);
            objData.put("notification", notif);
            System.out.println("!_@rj@_group_PASS:>"+ objData.toString());


    System.out.println("json :" +objData.toString());
    OutputStreamWriter wr = new OutputStreamWriter(conn.getOutputStream());

    wr.write(objData.toString());
    wr.flush();
    int status = 0;
    if( null != conn ){
    status = conn.getResponseCode();
    }
    if( status != 0){

        if( status == 200 ){
    //SUCCESS message
    BufferedReader reader = new BufferedReader(new 
    InputStreamReader(conn.getInputStream()));
    System.out.println("Android Notification Response : " + 
    reader.readLine());
    }else if(status == 401){
    //client side error
    System.out.println("Notification Response : TokenId : " + tokenId + " 
      Error occurred :");
    }else if(status == 501){
    //server side error
    System.out.println("Notification Response : [ errorCode=ServerError ] 
    TokenId : " + tokenId);
    }else if( status == 503){
    //server side error
    System.out.println("Notification Response : FCM Service is Unavailable  
   TokenId : " + tokenId);
    }
    }
    }catch(MalformedURLException mlfexception){
    // Prototcal Error
    System.out.println("Error occurred while sending Push Notification!.." + 
   mlfexception.getMessage());
    }catch(IOException mlfexception){
    //URL problem
    System.out.println("Reading URL, Error occurred while sending Push 
  Notification!.." + mlfexception.getMessage());
    }catch (Exception exception) {
    //General Error or exception.
    System.out.println("Error occurred while sending Push Notification!.." + 
 exception.getMessage());
    }

    }

}

     Calling should be like that :



  public class TestFCM {
        static List<String> putIds;
        public static void main(String[] args) {

    //Just I am passed dummy information
    // DeviceID's

    String tokenId1 = "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX";
    String tokenId  = "yyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy";
    String server_key ="<Server Key>" ;
    String message = "Welcome alankit Push Service.";

    putIds= new ArrayList<>();
    putIds.add(tokenId1);
    putIds.add(tokenId);

   /* for Group*/
    FCM.send_FCM_NotificationMulti(putIds,tokenId,server_key,message);  

    /*for indevidual*/
    FCM.send_FCM_Notification( tokenId,server_key,message);
    }


}
4
Arun-Khanka

votre code est parfait il suffit de revérifier l'ID d'enregistrement de l'utilisateur à qui vous envoyez la notification. À la place de to, utilisez le périphérique de l'utilisateur regid . Cela a parfaitement fonctionné pour moi.

3
Durgesh Kumar
  @Autowired
AndroidPushNotificationsService androidPushNotificationsService;

@RequestMapping(value = "/send", method = RequestMethod.GET, produces = "application/json")
public ResponseEntity<String> send() {


    JSONObject body = new JSONObject();
    // JsonArray registration_ids = new JsonArray();
    // body.put("registration_ids", registration_ids);
    body.put("to", "xxxxxxxxxxxxxxxxxxxjPwZpLgLpji_");
    body.put("priority", "high");
    // body.put("dry_run", true);

    JSONObject notification = new JSONObject();
    notification.put("body", "body string here");
    notification.put("title", "title string here");
    // notification.put("icon", "myicon");

    JSONObject data = new JSONObject();
    data.put("key1", "value1");
    data.put("key2", "value2");

    body.put("notification", notification);
    body.put("data", data);

    HttpEntity<String> request = new HttpEntity<>(body.toString());

    CompletableFuture<FirebaseResponse> pushNotification = androidPushNotificationsService.send(request);
    CompletableFuture.allOf(pushNotification).join();

    try {
        FirebaseResponse firebaseResponse = pushNotification.get();
        if (firebaseResponse.getSuccess() == 1) {
            log.info("Push notification sent ok!");
        } else {
            log.error("error sending Push notifications: " + firebaseResponse.toString());
        }
        return new ResponseEntity<>(firebaseResponse.toString(), HttpStatus.OK);

    } catch (InterruptedException e) {
        e.printStackTrace();
    } catch (ExecutionException e) {
        e.printStackTrace();
    }
    return new ResponseEntity<>("the Push notification cannot be send.", HttpStatus.BAD_REQUEST);
}

vous pouvez obtenir les cours à partir d’ici: https://github.com/AndreiD/testingpushnotifications

2
OWADVL

Le code côté client Android devrait être:

//subscribe_topic with name "ALL"
FirebaseMessaging.getInstance().subscribeToTopic("ALL");    

Du côté serveur:

string json = "{\"to\": \"/topics/ALL\",\"data\": {\"message\": \"This is a Cloud Messaging Topic Message\",}}";
1
Aditya varale

Utilisez le code ci-dessous pour envoyer le Push à plusieurs périphériques à l'aide de FCM.

public void sendAndroidPush(String input) {
        final String apiKey = ServerKey or legacyServerKey;
        StringBuffer response = new StringBuffer();
        try {
            URL url = new URL("https://fcm.googleapis.com/fcm/send");
            HttpURLConnection conn = (HttpURLConnection) url.openConnection();
            conn.setDoOutput(true);
            conn.setRequestMethod("POST");
            conn.setRequestProperty("Content-Type", "application/json");
            conn.setRequestProperty("Authorization", "key=" + apiKey);

            conn.setDoOutput(true);

            OutputStream os = conn.getOutputStream();
            os.write(input.getBytes());
            os.flush();
            os.close();

            int responseCode = conn.getResponseCode();
            System.out.println("\nSending 'POST' request to URL : " + url);
            System.out.println("Post parameters : " + input);
            System.out.println("Response Code : " + responseCode);

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


            while ((inputLine = in.readLine()) != null) {
                response.append(inputLine);
            }
            in.close();
        } catch(Exception e) {
            e.printStackTrace();
        }
        // print result
        System.out.println(response.toString());
    }


JSONArray devices = new JSONArray();
        devices.put("Your Actual Device Token");

        JSONObject payloadObj = new JSONObject();
        try {
            JSONObject notifyObj = new JSONObject();
            notifyObj.put("title","Your Title");
            notifyObj.put("text", "Your Text");

            payloadObj.put("registration_ids",devices.toString());
            payloadObj.put("notification",notifyObj.toString());

        } catch (Exception e) {
            // TODO: handle exception
            e.printStackTrace();
        }
        sendAndroidPush(payloadObj.toString());
0
Mandeep Singh