Ios Firebase推送通知服务器应用程序Java

时间:2017-08-02 16:39:53

标签: java firebase firebase-cloud-messaging

您好,我添加了iOS应用推送通知,并发送了一个 在这个项目中的通知。但我想向所有人发送通知 设备。我怎么能这样做?

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.URL;
import org.json.JSONException;
import org.json.JSONObject;

public class JavaApplication30 {
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{
// 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","Here is your notification.");
infoJson.put("body", message);
JSONObject json = new JSONObject();

我想在不输入任何设备的情况下向所有设备发送通知 本节中的令牌ID

json.put("to",tokenId.trim());
json.put("notification", infoJson);
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("İOS 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){
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(JSONException jsonexception){
//Message format error
System.out.println("Message Format, Error occurred while sending push Notification!.." + jsonexception.getMessage());
}catch (Exception exception) {
//General Error or exception.
System.out.println("Error occurred while sending push Notification!.." + exception.getMessage());
}  
}
    public static void main(String[] args) {

我想在不输入任何设备的情况下向所有设备发送通知 本节中的令牌ID

      String tokenId = "xxxxxxxxxxxx";
      String server_key = "xxxxxxxxxxxx";
      String message = "Hello";
      //Method to send Push Notification
      send_FCM_Notification( tokenId,server_key,message);
    }
}

0 个答案:

没有答案
相关问题