Firebase设备使用Retrofit进行设备消息传递,如何获取消息ID? [英] Firebase device to device messaging using Retrofit, how do i get message id?

查看:145
本文介绍了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 $解析方案

扩展我的消息POJO

  public class Message {
String to;
NotifyData通知;
字符串message_id;

public Message(String to,NotifyData notification,String message_id){
this.to = to;
this.notification =通知;
this.message_id = message_id;


public String getMessage_id(){

return message_id;


$ b $ / code $ / pre
$ b $ Firebase获取message_id后发送FCM信息

 呼叫< Message> call2 = firebaseAPI.sendMessage(new Message(to,notifydata,)); 
call2.enqueue(new Callback Message>(){
@Override
public void onResponse(Call< Message> call,Response< Message> response){

Log.d(Response,onResponse);
//t1.setText(Notification sent);
Message message = response.body();
Log.d (message,message.getMessage_id());


$ b @Override
public void onFailure(Call< Message> call,Throwable t){
Log.d(Response,onFailure);
//t1.setText(通知失败);
}
});


I use Retrofit for sending device to device message without php script and curl command and everything work fine. I need to save sent message ID. How can i get after success sending message ID for sent message. My code. Sending activity

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 intercept(Chain chain) throws IOException {
        Request original = chain.request();

        // Request customization: add request headers
        Request.Builder requestBuilder = original.newBuilder()
                .header("Authorization", "key=legacy server key from FB console"); // <-- this is the important line
        Request request = requestBuilder.build();
        return chain.proceed(request);
    }
});

httpClient.addInterceptor(logging);
OkHttpClient client = httpClient.build();

Retrofit retrofit = new Retrofit.Builder()
        .baseUrl("https://fcm.googleapis.com")//url of FCM message server
        .client(client)
        .addConverterFactory(GsonConverterFactory.create())//use for convert JSON file into object
        .build();

// prepare call in Retrofit 2.0
FirebaseAPI firebaseAPI = retrofit.create(FirebaseAPI.class);

//for messaging server
NotifyData notifydata = new NotifyData("Notification title","Notification body");

Call<Message> call2 = firebaseAPI.sendMessage(new Message("...... device token ....", notifydata));

call2.enqueue(new Callback<Message>() {
    @Override
    public void onResponse(Call<Message> call, Response<Message> response) {

        Log.d("Response ", "onResponse");
        t1.setText("Notification sent");

    }

    @Override
    public void onFailure(Call<Message> call, Throwable t) {
        Log.d("Response ", "onFailure");
        t1.setText("Notification failure");
    }
});
}

POJOs

public class Message {
String to;
NotifyData notification;

public Message(String to, NotifyData notification) {
    this.to = to;
    this.notification = notification;
}

}

and

public class NotifyData {
String title;
String body;

public NotifyData(String title, String body ) {

    this.title = title;
    this.body = body;
}

}

and FirebaseAPI

public interface FirebaseAPI {

@POST("/fcm/send")
Call<Message> sendMessage(@Body Message message);

}

解决方案

I have extended my Message POJO

public class Message {
String to;
NotifyData notification;
String message_id;

public Message(String to, NotifyData notification, String message_id) {
    this.to = to;
    this.notification = notification;
    this.message_id = message_id;
    }

public String getMessage_id() {

    return message_id;
    }

}

Firebase get message_id in Response after sending FCM message

Call<Message> call2 = firebaseAPI.sendMessage(new Message(to, notifydata, ""));
    call2.enqueue(new Callback<Message>() {
        @Override
        public void onResponse(Call<Message> call, Response<Message> response) {

            Log.d("Response ", "onResponse");
            //t1.setText("Notification sent");
            Message message = response.body();
            Log.d("message", message.getMessage_id());

        }

        @Override
        public void onFailure(Call<Message> call, Throwable t) {
            Log.d("Response ", "onFailure");
            //t1.setText("Notification failure");
        }
    });

这篇关于Firebase设备使用Retrofit进行设备消息传递,如何获取消息ID?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆