How To Send Fcm Notification From Android Using Retrofit?
I am want send fcm notification from android device to another device , using retrofit . I try this but, public interface ApiInterface { @Headers('Authorization : key=AAAA4Ubio
Solution 1:
Finally solved problem !
It was mistake in header
, actually it is like this
@Headers({"Authorization: key=AAAA4Ubio1Q:APA91bGWkw84b1XX2nnnOKn8MO25U2giLRXXXTUkXidojFluZk_qKXXXlS27oMZZV5goTQdwRtpdmvI1iAPRZZDNKz6c-mpU6nvHZJ-Jg9f1fQ5NdttftqUpqwAkObLEEX26VFDDbXN8",
"Content-Type:application/json"})
Full code of how to send fcm notification
from android using retrofit
:-
publicinterfaceApiInterface {
@Headers({"Authorization: key=AAAA4Ubio1Q:APA91bGWkw84b1Pw2nnnOKn8MO25U2giLRtv5TUkXidojFluZk_qKOGllS27oMZZV5goTQdwRtpdmvI1iAPRZZDNKz6c-mpU6nvHZJ-Jg9f1fQ5NdttftqUpqwAkObLEED26VFDDbXN8",
"Content-Type:application/json"})
@POST("fcm/send")
Call<ResponseBody> sendChatNotification(@Body RequestNotificaton requestNotificaton);
}
ApiClient class :-
publicstaticfinalStringBASE_URL="https://fcm.googleapis.com/";
privatestaticRetrofitretrofit=null;
publicstatic Retrofit getClient() {
if (retrofit==null) {
retrofit = newRetrofit.Builder()
.baseUrl(BASE_URL)
.addConverterFactory(GsonConverterFactory.create())
.build();
}
return retrofit;
}
Model class :- RequestNotificaton.class
publicclassRequestNotificaton {
@SerializedName("token") // "to" changed to tokenprivateString token;
@SerializedName("notification")
privateSendNotificationModel sendNotificationModel;
publicSendNotificationModelgetSendNotificationModel() {
return sendNotificationModel;
}
publicvoidsetSendNotificationModel(SendNotificationModel sendNotificationModel) {
this.sendNotificationModel = sendNotificationModel;
}
publicStringgetToken() {
return token;
}
publicvoidsetToken(String token) {
this.token = token;
}
}
Model class :-SendNotificationModel.class
publicclassSendNotificationModel {
privateString body,title;
publicSendNotificationModel(String body, String title) {
this.body = body;
this.title = title;
}
publicStringgetBody() {
return body;
}
publicvoidsetBody(String body) {
this.body = body;
}
publicStringgetTitle() {
return title;
}
publicvoidsetTitle(String title) {
this.title = title;
}
}
Main Code:-
privatevoidsendNotificationToPatner() {
SendNotificationModel sendNotificationModel = newSendNotificationModel("check", "i miss you");
RequestNotificaton requestNotificaton = newRequestNotificaton();
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(newCallback<ResponseBody>() {
@OverridepublicvoidonResponse(retrofit2.Call<ResponseBody> call, retrofit2.Response<ResponseBody> response) {
Log.d("kkkk","done");
}
@OverridepublicvoidonFailure(retrofit2.Call<ResponseBody> call, Throwable t) {
}
});
}
I hope its help You!
Note
Dependencies are :-
// 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'
Post a Comment for "How To Send Fcm Notification From Android Using Retrofit?"