[英]sending fcm push notifications using retrofit library in android
嘗試使用 android 中的改造庫發送 fcm 通知。 我想向其他 Android 設備顯示帶有標題和消息的通知。 這是用於推送通知的代碼和示例文件。
使用改造調用發送推送通知的功能:
private void sendNotification(String deviceId,String sender,String message)
{
FirebaseApi apiService =
FirebaseClient.getClient().create(FirebaseApi.class);
NotifyData notifydata = new NotifyData(sender,message);
Call<FirebaseMessage> call = apiService.sendMessage(new
FirebaseMessage(deviceId, notifydata));
call.enqueue(new Callback<FirebaseMessage>() {
@Override
public void onResponse(Call<FirebaseMessage> call,
Response<FirebaseMessage> response) {
Log.e("Message Response","Send");
}
@Override
public void onFailure(Call<FirebaseMessage> call, Throwable t) {
Log.e("Message Response","Fail");
}
});
}
FirebaseMessage 類:
public class FirebaseMessage {
String to;
NotifyData notification;
public FirebaseMessage(String to, NotifyData notification) {
this.to = to;
this.notification = notification;
}
}
通知數據類:
public class NotifyData {
String title;
String body;
public NotifyData(String title, String body ) {
this.title = title;
this.body = body;
}
}
FirebaseClient 類:
public class FirebaseClient {
public static Retrofit RETROFIT = null;
public static Retrofit getClient(){
if(RETROFIT==null){
OkHttpClient client = new OkHttpClient.Builder()
.addInterceptor(new LoggingInterceptor())
.build();
RETROFIT = new Retrofit.Builder()
.baseUrl(StaticConfig.FIREBASE_URL)
.client(client)
.addConverterFactory(GsonConverterFactory.create())
.build();
}
return RETROFIT;
}
}
FirebaseApi 類:
public interface FirebaseApi {
@Headers({"Authorization: key=Legacy Service Key",
"Content-Type:application/json"})
@POST("fcm/send")
Call<FirebaseMessage> sendMessage(@Body FirebaseMessage message);
}
在 MyFirebaseMessagingService 類中:
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
super.onMessageReceived(remoteMessage);
String title=remoteMessage.getData().get("title");
String body=remoteMessage.getData().get("body");
sendNotification(title, body);
}
但它每次都顯示空標題和空正文的空白通知。 如何正確地通過 remoteMessage 檢索我的通知數據。
implementation 'com.squareup.retrofit2:retrofit:2.6.0'
implementation 'com.squareup.retrofit2:converter-gson:2.6.0'
public class ApiClient {
private 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;
}
}
public interface ApiInterface {
@Headers({"Authorization: key=" + ConstantKey.SERVER_KEY, "Content-Type:application/json"})
@POST("fcm/send")
Call<ResponseBody> sendNotification(@Body RootModel root);
}
public class RootModel {
@SerializedName("to") // "to" changed to token
private String token;
@SerializedName("notification")
private NotificationModel notification;
@SerializedName("data")
private DataModel data;
public RootModel(String token, NotificationModel notification, DataModel data) {
this.token = token;
this.notification = notification;
this.data = data;
}
public String getToken() {
return token;
}
public void setToken(String token) {
this.token = token;
}
public NotificationModel getNotification() {
return notification;
}
public void setNotification(NotificationModel notification) {
this.notification = notification;
}
public DataModel getData() {
return data;
}
public void setData(DataModel data) {
this.data = data;
}
}
public class NotificationModel {
private String title;
private String body;
public NotificationModel(String title, String body) {
this.title = title;
this.body = body;
}
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;
}
}
public class DataModel {
private String name;
private String age;
public DataModel(String name, String age) {
this.name = name;
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAge() {
return age;
}
public void setAge(String age) {
this.age = age;
}
}
private void sendNotificationToUser(String token) {
RootModel rootModel = new RootModel(token, new NotificationModel("Title", "Body"), new DataModel("Name", "30"));
ApiInterface apiService = ApiClient.getClient().create(ApiInterface.class);
retrofit2.Call<ResponseBody> responseBodyCall = apiService.sendNotification(rootModel);
responseBodyCall.enqueue(new Callback<ResponseBody>() {
@Override
public void onResponse(retrofit2.Call<ResponseBody> call, retrofit2.Response<ResponseBody> response) {
Log.d(TAG,"Successfully notification send by using retrofit.");
}
@Override
public void onFailure(retrofit2.Call<ResponseBody> call, Throwable t) {
}
});
}
哇,當我嘗試@Mustofa Kamal 的回答時。 我遇到了關於模型的問題,因為我得到了錯誤代碼 400,這意味着使用 JSON 格式解析錯誤。 所以,我使用 JsonObject 作為數據解析成這樣的 json 格式
JsonObject jNotif = new JsobObject()
jNotif.addProperly("title","hello world");
jNotif.addProperly("body","hello this world");
JsonObject jFCM = new JsonObject()
jFCM.add("notification",jNotif);
jFCM.addProperly("to",<your_token_from_firebase>);
我在 ApiInterface 接口中使用如下:
@Headers({"Content-Type: application/json","Authentication: <your_server_key>"})
@POST("/fcm/send")
Call<<your_model_response>> createFirebaseCloudM(@Body <your_model_class>);
我將該代碼包含在方法填充改造響應中。 我知道你如何處理改造的響應,它與上述答案相同。 但是,當您發送數據時沒有模型
希望它會有所幫助...
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.