这里我有一个非常简单的解决方案,可以发送推送消息给群组和个人。
public class FCM {
final static private String FCM_URL = "https://fcm.googleapis.com/fcm/send";
static void send_FCM_Notification(String tokenId, String server_key, String
message){
try{
URL url = new URL(FCM_URL);
HttpURLConnection conn;
conn = (HttpURLConnection) url.openConnection();
conn.setUseCaches(false);
conn.setDoInput(true);
conn.setDoOutput(true);
conn.setRequestMethod("POST");
conn.setRequestProperty("Authorization","key="+server_key);
conn.setRequestProperty("Content-Type","application/json");
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 ){
BufferedReader reader = new BufferedReader(new
InputStreamReader(conn.getInputStream()));
System.out.println("Android Notification Response : " + reader.readLine());
}else if(status == 401){
System.out.println("Notification Response : TokenId : " + tokenId + " Error occurred :");
}else if(status == 501){
System.out.println("Notification Response : [ errorCode=ServerError ] TokenId : " + tokenId);
}else if( status == 503){
System.out.println("Notification Response : FCM Service is Unavailable
TokenId : " + tokenId);
}
}
}catch(MalformedURLException mlfexception){
System.out.println("Error occurred while sending push Notification!.." + mlfexception.getMessage());
}catch(Exception mlfexception){
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{
URL url = new URL(FCM_URL);
HttpURLConnection conn;
conn = (HttpURLConnection) url.openConnection();
conn.setUseCaches(false);
conn.setDoInput(true);
conn.setDoOutput(true);
conn.setRequestMethod("POST");
conn.setRequestProperty("Authorization","key="+server_key);
conn.setRequestProperty("Content-Type","application/json");
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 ){
BufferedReader reader = new BufferedReader(new
InputStreamReader(conn.getInputStream()));
System.out.println("Android Notification Response : " +
reader.readLine());
}else if(status == 401){
System.out.println("Notification Response : TokenId : " + tokenId + "
Error occurred :");
}else if(status == 501){
System.out.println("Notification Response : [ errorCode=ServerError ]
TokenId : " + tokenId);
}else if( status == 503){
System.out.println("Notification Response : FCM Service is Unavailable
TokenId : " + tokenId);
}
}
}catch(MalformedURLException mlfexception){
System.out.println("Error occurred while sending push Notification!.." +
mlfexception.getMessage());
}catch(IOException mlfexception){
System.out.println("Reading URL, Error occurred while sending push
Notification!.." + mlfexception.getMessage());
}catch (Exception 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) {
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);
FCM.send_FCM_NotificationMulti(putIds,tokenId,server_key,message);
FCM.send_FCM_Notification( tokenId,server_key,message);
}
}