本文介绍了Firebase设备使用Retrofit进行设备消息传递,如何获取消息ID?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我使用Retrofit发送设备到设备消息没有PHP脚本和卷曲命令,一切工作正常。我需要保存发送的消息ID。我怎么能成功后发送消息ID发送消息。我的代码。发送活动
public void onClick(View view){
HttpLoggingInterceptor logging = new HttpLoggingInterceptor();
logging.setLevel(HttpLoggingInterceptor.Level.BODY);
OkHttpClient.Builder httpClient = new OkHttpClient.Builder();
httpClient.addInterceptor(new Interceptor(){
@Override
public okhttp3.Response拦截(链式链接)抛出IOException {
Request original = chain.request();
//请求定制:添加请求头
Request.Builder requestBuilder = original.newBuilder()
.header(Authorization,key =来自FB控制台的遗留服务器密钥) ; //< - 这是重要的行
Request request = requestBuilder.build();
return chain.proceed(request);
}
});
httpClient.addInterceptor(logging);
OkHttpClient客户端= httpClient.build();
Retrofit retrofit = new Retrofit.Builder()
.baseUrl(https://fcm.googleapis.com)// FCM消息服务器的URL
.client客户端)
.addConverterFactory(GsonConverterFactory.create())//用于将JSON文件转换为对象
.build();
//在Retrofit 2.0中准备调用
FirebaseAPI firebaseAPI = retrofit.create(FirebaseAPI.class);
//用于消息传递服务器
NotifyData notifydata = new NotifyData(Notification title,Notification body);
呼叫<消息> call2 = firebaseAPI.sendMessage(new Message(...... device token ....,notifydata));
$ b call2.enqueue(new Callback Message> {
@Override $ b $ public void onResponse(Call< Message> call,Response< Message> response){
Log.d(Response,onResponse);
t1.setText(Notification sent);
}
@覆盖
public void onFailure(调用< Message> call,Throwable t){
Log.d(Response,onFailure);
t1.setText(Notification failure
}
});
$ / code>
POJO
public class Message {
String to;
NotifyData通知;
public Message(String to,NotifyData notification){
this.to = to;
this.notification =通知;
$ b $ / code $ / pre
$ b $ p和$ / b
public class NotifyData {
String title;
字符串正文;
$ b $ public NotifyData(String title,String body){
this.title = title;
this.body = body;
}
}
和FirebaseAPI
public interface FirebaseAPI {
@POST(/ fcm / send)
Call< Message> sendMessage(@Body Message message);
$ div $解析方案