首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Firebase device to device messaging using Retrofit,如何获取消息id?

Firebase device to device messaging using Retrofit,如何获取消息id?
EN

Stack Overflow用户
提问于 2017-02-25 18:43:13
回答 1查看 2.1K关注 0票数 0

我使用Retrofit在没有php脚本和curl命令的情况下发送设备到设备消息,一切工作正常。需要保存已发送消息ID,已发送消息发送成功后如何获取?我的代码。发送活动

代码语言:javascript
复制
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");
    }
});
}

POJO

代码语言:javascript
复制
public class Message {
String to;
NotifyData notification;

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

}

代码语言:javascript
复制
public class NotifyData {
String title;
String body;

public NotifyData(String title, String body ) {

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

}

和FirebaseAPI

代码语言:javascript
复制
public interface FirebaseAPI {

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

}
EN

回答 1

Stack Overflow用户

发布于 2017-08-21 14:52:53

我已经扩展了我的消息POJO

代码语言:javascript
复制
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在发送FCM消息后响应get message_id

代码语言:javascript
复制
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");
        }
    });
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/42454986

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档