为什么永远不会调用FirebaseInstanceIdService.onTokenRefresh()?

时间:2016-09-20 22:50:18

标签: android firebase firebase-cloud-messaging

也许我做错了一步。我有3个活动:

  • 启动器
  • 登录
  • MainActivity

在我的MainActivity的onCreate中,我正在调用该服务:

   void iniciarServicioSendTokenFCM(){

        servicioFCM= new Intent(this, IDService.class);
        startService(servicioFCM);

    }

执行此操作,因为它会进入服务的onCreate,但永远不会执行onTokenRefresh()

我也完成了这些steps。我已经卸载并重新安装了应用程序,但它没有用。

public class IDService extends FirebaseInstanceIdService {

    private ConnectionStatusSync ConnSync;//= new ConnectionStatusSync(this);
    private DispositivoSync Sync;
    private Integer dispositivoId;
    private PreferenceUtil preferenceUtil ;
    private String tokenDispositivo;
    private DispositivoSync.OnFragmentInteractionListener listener;
    public IDService() {

    }

    @Override
    public void onCreate() {
        super.onCreate();
        Listener();
        ConnSync = new ConnectionStatusSync(this);
        Sync = new DispositivoSync(this);
        preferenceUtil= new PreferenceUtil(this);

        dispositivoId=preferenceUtil.getInt(getString(R.string.dispositivoID),0,null);
        dispositivoId=(dispositivoId==0?null:dispositivoId);
        tokenDispositivo= new IDUtil(this).getId();


    }

    private  void Listener(){
        listener = new DispositivoSync.OnFragmentInteractionListener() {
            @Override
            public void onFinished(boolean terminoBien, int dispositivoID) {
                if(terminoBien){
                    preferenceUtil.savePreference(getString(R.string.dispositivoID),dispositivoID,null);

                }
            }
        };
    }

    @Override
    public void onTokenRefresh() {
        // Get updated InstanceID token.
        String refreshedToken = FirebaseInstanceId.getInstance().getToken();
        preferenceUtil.savePreference(getString(R.string.TokenFCM),refreshedToken,null);
        //Log.d(TAG, "Refreshed token: " + refreshedToken);

        // TODO: Implement this method to send any registration to your app's servers.
        if(validaciones())
        sendRegistrationToServer(refreshedToken);
    }

    private  boolean validaciones(){

        return  dispositivoId!=null && MainActivity.mOperador!=null;
    }

    private  void sendRegistrationToServer(final String token){



        final Thread registrar = new Thread(new Runnable() {
            @Override
            public void run() {
Sync.EnviarDispositivo(MainActivity.mOperador.getOperadorIdServidor(),dispositivoId,token,tokenDispositivo,listener );
            }
        });

        Thread hilo  = new Thread(new Runnable() {
            @Override
            public void run() {

                Command commandNull= new Command() {
                    @Override
                    public void execute() {

                    }
                };

               ConnSync.CheckConnection(registrar,commandNull);

            }
        });

        hilo.start();

    }



}

1 个答案:

答案 0 :(得分:0)

在使用Firebase云消息传递的应用中,客户端会在应用启动时立即开始生成令牌。

很可能在您开始收听onTokenRefresh()时已生成初始令牌。因此,您还希望立即在iniciarServicioSendTokenFCM方法中请求和存储令牌:

sendRegistrationToServer(FirebaseInstanceId.getInstance().getToken());

我认为将令牌存储在共享首选项中是非常有用的,因为它可以从FirebaseInstanceId.getInstance().getToken()轻松访问。