ListaAlunosActivity
public class ListaAlunosActivity extends AppCompatActivity {
private ListView listaAlunos;
private SwipeRefreshLayout swipe;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_lista_alunos);
EventBus eventBus = EventBus.getDefault();
eventBus.register(this);
listaAlunos = (ListView) findViewById(R.id.lista_alunos);
swipe = (SwipeRefreshLayout) findViewById(R.id.swipe_lista_aluno);
swipe.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
@Override
public void onRefresh() {
buscaAlunos();
}
});
AgendaInstanceIDService
public class AgendaInstanceIDService extends FirebaseInstanceIdService {
@Override
public void onTokenRefresh() {
// Get updated InstanceID token.
String refreshedToken = FirebaseInstanceId.getInstance().getToken();
Log.d("token firebase", "Refreshed token: " + refreshedToken);
// TODO: Implement this method to send any registration to your app's servers.
enviaTokenParaServidor(refreshedToken);
}
private void enviaTokenParaServidor(final String token) {
Call<Void> call = new RetrofitInicializador().getDispositivoService().enviaToken(token);
call.enqueue(new Callback<Void>() {
@Override
public void onResponse(Call<Void> call, Response<Void> response) {
Log.i("token enviado", token);
}
@Override
public void onFailure(Call<Void> call, Throwable t) {
Log.e("token falhou", t.getMessage());
}
});
}
}
AgendaMessagingService
public class AgendaMessagingService extends FirebaseMessagingService {
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
super.onMessageReceived(remoteMessage);
Map<String, String> mensagem = remoteMessage.getData();
Log.i("mensagem recebida", String.valueOf(mensagem));
converteParaAluno(mensagem);
}
private void converteParaAluno(Map<String, String> mensagem) {
String chaveDeAcesso = "alunoSync";
if(mensagem.containsKey(chaveDeAcesso)){
String json = mensagem.get(chaveDeAcesso);
ObjectMapper mapper = new ObjectMapper();
try {
AlunoSync alunoSync = mapper.readValue(json, AlunoSync.class);
AlunoDAO alunoDAO = new AlunoDAO(this);
alunoDAO.sincroniza(alunoSync.getAlunos());
alunoDAO.close();
EventBus eventBus = EventBus.getDefault();
eventBus.post(new AtualizaListaAlunoEvent());
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
DispositivoService
public interface DispositivoService {
@POST("firebase/dispositivo")
Call<Void> enviaToken(@Header("token") String token);
}