我正在使用Firebase通知构建应用程序,我需要将通知发送到多个设备,我需要知道有什么方法可以在每个设备上设置间隔。
答案 0 :(得分:0)
您可以使用此方法将通知发送到多个设备,首先需要在Firebase中创建项目,然后您将获得一个身份验证密钥,并在下面显示的代码中使用它
import java.io.OutputStreamWriter;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.List;
import org.json.JSONObject;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
public void SendToMultipalToken(List allTokens , String message , String type)
throws Exception
{
//Firebase Server Key
String authKey = "**Your firebase Auth key**";
//Firebase URL
String FMCurl = "https://fcm.googleapis.com/fcm/send";
URL url = new URL(FMCurl);
HttpURLConnection conn = (HttpURLConnection)
url.openConnection();//createb connection with firebase
conn.setUseCaches(false);
conn.setDoInput(true);
conn.setDoOutput(true);
//sending parameters of json object
conn.setRequestMethod("POST");
conn.setRequestProperty("Content-Type", "application/json");
conn.setRequestProperty("Authorization", "key=" + authKey);
JSONObject json = new JSONObject();
//Convert List into String Arry
String[] s=new String[allTokens.size()];
for (int i = 0; i < allTokens.size(); i++) {
s[i]=(String) allTokens.get(i);
}
json.put("registration_ids", s);//for multiple device
//json.put("to", "");//for single device
json.put("collapse_key", "type_a");
JSONObject info = new JSONObject();
info.put("title", "Notification"); // Notification title
info.put("body", "**notification body title**"); // Notification body
json.put("notification", info);
JSONObject jdata = new JSONObject();
jdata.put("body", "**notification body title**");
jdata.put("title", "Notification");
jdata.put("key_1", message);
jdata.put("key_2", type);
json.put("data", jdata);
OutputStreamWriter wr = new OutputStreamWriter(conn.getOutputStream());
wr.write(json.toString());
wr.flush();
conn.getInputStream();
}