2017-02-25 63 views
0

我使用Retrofit發送設備到設備消息沒有PHP腳本和捲曲命令,一切工作正常。我需要保存已發送的消息ID。我怎樣才能成功發送消息ID發送消息。我的代碼。 發送活動使用Retrofit進行Firebase設備到設備消息傳遞,我如何獲取消息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 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

public class Message { 
String to; 
NotifyData notification; 

public Message(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; 
} 

} 

和FirebaseAPI

public interface FirebaseAPI { 

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

} 

回答

0

我已經延長我的留言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; 
    } 

} 

發送FCM消息

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"); 
     } 
    }); 
後火力地堡得到響應MESSAGE_ID
相關問題