更新时间:2022-12-27 11:36:07
我解决了这个问题,可能对某人有帮助,
I solved this problem may it help someone ,
要通过api将Firebase的通知发送到单个设备,如下所示:
In order to send Notification of firebase through api to single device is as follows:
1:为每个设备生成唯一的实例ID
public class MyFirebaseInstanceIDService extends FirebaseInstanceIdService {
private static final String TAG = "MyFirebaseIIDService";
@Override
public void onTokenRefresh() {
//Getting registration token
String refreshedToken = FirebaseInstanceId.getInstance().getToken();
//Displaying token on logcat
Log.d(TAG, "Refreshed token: " + refreshedToken);
sendRegistrationToServer(refreshedToken);
}
private void sendRegistrationToServer(String token) {
//You can implement this method to store the token on your server
//Not required for current project
}
}
注意:您可以从应用程序中的任何位置调用实例ID,如下所示:
Note: you can call Instance Id from any where in your Application Like this:
String tkn = FirebaseInstanceId.getInstance().getToken();
Toast.makeText(Doc_users.this, "Current token ["+tkn+"]",
Toast.LENGTH_LONG).show();
Log.d("App", "Token ["+tkn+"]");
此令牌必须用于将通知发送到单个设备,它是您可以将其存储在Firebase数据库中的设备的ID
this token must be used to send notification to single device it is the id of the device you can store it in your firebase database
reference.child(UserDetails.username).child("token").setValue(tokun);
2:现在,您可以构建逻辑来获取这些ID并构建您的请求
public static String makeRequest(String id) throws JSONException {
HttpURLConnection urlConnection;
JSONObject json = new JSONObject();
JSONObject info = new JSONObject();
info.put("title", "Notification Title"); // Notification title
info.put("body", "Notification body"); // Notification body
info.put("sound", "mySound"); // Notification sound
json.put("notification", info);
json.put("to","INSTANCE ID FETCHED FOR SIGNLE DEVICE HERE");
Log.e("deviceidkey==> ",id+"");
Log.e("jsonn==> ",json.toString());
String data = json.toString();
String result = null;
try {
//Connect
urlConnection = (HttpURLConnection) ((new URL("https://fcm.googleapis.com/fcm/send").openConnection()));
urlConnection.setDoOutput(true);
urlConnection.setRequestProperty("Content-Type", "application/json");
urlConnection.setRequestProperty("Authorization", "key=YOUR FIREBASE SERVER KEY");
urlConnection.setRequestMethod("POST");
urlConnection.connect();
//Write
OutputStream outputStream = urlConnection.getOutputStream();
BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(outputStream, "UTF-8"));
writer.write(data);
writer.close();
outputStream.close();
//Read
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(urlConnection.getInputStream(), "UTF-8"));
String line = null;
StringBuilder sb = new StringBuilder();
while ((line = bufferedReader.readLine()) != null) {
sb.append(line);
}
bufferedReader.close();
result = sb.toString();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return result;
}
通知会在从您的设备发送的那一台设备上生成,您可以调试以放置自己的设备密钥并使用邮递员进行有效的请求:祝您好运
Notification will generate on that single device sent from your device you can debug to put your own device key and use postman for valid request: Good Luck