【发布时间】:2019-06-29 20:47:41
【问题描述】:
我正在开发一个应用程序,在该应用程序中我必须向特定设备发送通知,但现在我正在寻找解决方案
我正在使用rest api链接https://fcm.googleapis.com/fcm/send
我已经尝试了很多并寻求更好的解决方案。
//API 类改造
public interface Api {
@Headers({"Content-Type:application/json",
"Authorization:key=SECRET_KEY"
})
@POST("fcm/send")
Call<List<Score>> getValues(@Body Score score);
}
// Gson类
// 分数等级
//
public class Score {
public DataScore getData() {
return data;
}
public void setData(DataScore data) {
this.data = data;
}
public String getTo() {
return to;
}
public void setTo(String to) {
this.to = to;
}
@SerializedName("data")
@Expose
private DataScore data;
@SerializedName("to")
@Expose
private String to;
}
// 数据评分
class DataScore {
@SerializedName("score")
@Expose
private String score;
public String getScore() {
return score;
}
public void setScore(String score) {
this.score = score;
}
public String getTime() {
return time;
}
public void setTime(String time) {
this.time = time;
}
@SerializedName("time")
@Expose
private String time;
}
// 主类
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
FirebaseMessaging.getInstance().setAutoInitEnabled(true);
String token =FirebaseInstanceId.getInstance().getToken();
String key=token;
DataScore dataScore =new DataScore();
dataScore.setScore("aaa");
dataScore.setTime("aa");
Score score=new Score();
score.setData(dataScore);
score.setTo(token);
Retrofit retrofit =new Retrofit.Builder().baseUrl("https://fcm.googleapis.com/").addConverterFactory(GsonConverterFactory.create()).build();
Api api= retrofit.create(Api.class);
api.getValues(score).enqueue(new Callback<List<Score>>() {
@Override
public void onResponse(Call<List<Score>> call, Response<List<Score>> response) {
if (response.isSuccessful()){
Toast.makeText(MainActivity.this, response.body().toString() +"Successfull", Toast.LENGTH_SHORT).show();
}
else
{
Toast.makeText(MainActivity.this, response.message()+"FAiled",Toast.LENGTH_SHORT).show();
}
}
@Override
public void onFailure(Call<List<Score>> call, Throwable t) {
Toast.makeText(MainActivity.this, t.getMessage() +"Failure", Toast.LENGTH_SHORT).show();
}
});
}
【问题讨论】: