终于解决问题了!
这是错误的header
,其实是这样的
@Headers({"Authorization: key=AAAA4Ubio1Q:APA91bGWkw84b1XX2nnnOKn8MO25U2giLRXXXTUkXidojFluZk_qKXXXlS27oMZZV5goTQdwRtpdmvI1iAPRZZDNKz6c-mpU6nvHZJ-Jg9f1fQ5NdttftqUpqwAkObLEEX26VFDDbXN8",
"Content-Type:application/json"})
如何发送的完整代码fcm notification
从 android 使用retrofit
:-
public interface ApiInterface {
@Headers({"Authorization: key=AAAA4Ubio1Q:APA91bGWkw84b1Pw2nnnOKn8MO25U2giLRtv5TUkXidojFluZk_qKOGllS27oMZZV5goTQdwRtpdmvI1iAPRZZDNKz6c-mpU6nvHZJ-Jg9f1fQ5NdttftqUpqwAkObLEED26VFDDbXN8",
"Content-Type:application/json"})
@POST("fcm/send")
Call<ResponseBody> sendChatNotification(@Body RequestNotificaton requestNotificaton);
}
ApiClient 类:-
public static final String BASE_URL = "https://fcm.googleapis.com/";
private static Retrofit retrofit = null;
public static Retrofit getClient() {
if (retrofit==null) {
retrofit = new Retrofit.Builder()
.baseUrl(BASE_URL)
.addConverterFactory(GsonConverterFactory.create())
.build();
}
return retrofit;
}
模型类:-RequestNotificaton.class
public class RequestNotificaton {
@SerializedName("token") // "to" changed to token
private String token;
@SerializedName("notification")
private SendNotificationModel sendNotificationModel;
public SendNotificationModel getSendNotificationModel() {
return sendNotificationModel;
}
public void setSendNotificationModel(SendNotificationModel sendNotificationModel) {
this.sendNotificationModel = sendNotificationModel;
}
public String getToken() {
return token;
}
public void setToken(String token) {
this.token = token;
}
}
模型类:-SendNotificationModel.class
public class SendNotificationModel {
private String body,title;
public SendNotificationModel(String body, String title) {
this.body = body;
this.title = title;
}
public String getBody() {
return body;
}
public void setBody(String body) {
this.body = body;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
}
主要代码:-
private void sendNotificationToPatner() {
SendNotificationModel sendNotificationModel = new SendNotificationModel("check", "i miss you");
RequestNotificaton requestNotificaton = new RequestNotificaton();
requestNotificaton.setSendNotificationModel(sendNotificationModel);
//token is id , whom you want to send notification ,
requestNotificaton.setToken(token);
apiService = ApiClient.getClient().create(ApiInterface.class);
retrofit2.Call<ResponseBody> responseBodyCall = apiService.sendChatNotification(requestNotificaton);
responseBodyCall.enqueue(new Callback<ResponseBody>() {
@Override
public void onResponse(retrofit2.Call<ResponseBody> call, retrofit2.Response<ResponseBody> response) {
Log.d("kkkk","done");
}
@Override
public void onFailure(retrofit2.Call<ResponseBody> call, Throwable t) {
}
});
}
我希望它能帮助你!
Note
依赖关系是:-
// retrofit, gson
implementation 'com.google.code.gson:gson:2.8.0'
implementation 'com.squareup.retrofit2:retrofit:2.4.0'
implementation 'com.squareup.retrofit2:converter-gson:2.3.0'