0

I have an FCM service class where I picked up the token and send to my server using AsyncTask. This error only happens on devices with Android 2.3 to start my app I try to send the token to the server, does not even run my AsyncTask. Stracktrace

Fatal Exception: java.lang.ExceptionInInitializerError
   at br.lgfelicio.gcm.MyInstanceIDListenerService.sendRegistrationToServer(MyInstanceIDListenerService.java:39)
   at br.lgfelicio.gcm.MyInstanceIDListenerService.onTokenRefresh(MyInstanceIDListenerService.java:30)
   at com.google.firebase.iid.FirebaseInstanceIdService.zzag(Unknown Source)
   at com.google.firebase.iid.FirebaseInstanceIdService.zzm(Unknown Source)
   at com.google.firebase.iid.zzb$2.run(Unknown Source)
   at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1088)
   at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:581)
   at java.lang.Thread.run(Thread.java:1019)
Caused by java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare()

I do not understand why the android 2.3 this error happens, how can I send the token to my server from that version?

Class FCM Service

public class MyInstanceIDListenerService extends FirebaseInstanceIdService {
private static final String SAVE_ABERTURA = "1";
private Armazenamento armazenamento = new Armazenamento(this);

@Override
public void onTokenRefresh() {
    // Get updated InstanceID token.
    String refreshedToken = FirebaseInstanceId.getInstance().getToken();

    String tokenFcm = armazenamento.LoadPreferences("tokenFcm");

    if (tokenFcm != null && !tokenFcm.equals("") && !refreshedToken.equals(tokenFcm)) {
        removeTokenFcm(tokenFcm);
    }

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

private void removeTokenFcm(String tokenFcm) {
    new RemoveTokenTask(getApplicationContext(), "remove").execute(tokenFcm);
}

private void sendRegistrationToServer(String refreshedToken) {

    new RegistroTokenTask(getApplicationContext(), refreshedToken, "", SAVE_ABERTURA).execute();
}
}

AsyncTask

public class RegistroTokenTask extends AsyncTask<Void, Void, String> {

private Context context;
private String tokenFcm;
private String key;

// save = 1 veio do abertura e save = 2 veio do login
private String save;

public RegistroTokenTask(Context context, String tokenFcm, String key, String save) {
    this.tokenFcm = tokenFcm;
    this.key = key;
    this.save = save;
    this.context = context;
}

@Override
protected void onPreExecute() {
    Log.i("checkin", "teste a: ");
}

@Override
protected String doInBackground(Void... params) {
    String msg = "";

    try {

        Armazenamento arm = new Armazenamento(context);

        if (key.trim().equals("")) {
            key = arm.LoadPreferences("key");
        }

        // url...

        DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
        Document doc = builder.parse(link);
        msg = doc.getElementsByTagName("status").item(0).getTextContent();

    } catch (Exception e) {
        Log.i("checkin", "e: " +e.getMessage());
    }

    return msg;
}

@Override
protected void onPostExecute(String result) {

    // se armazenou o token no banco de dados entao salva no storage
    if (result.equals("Success") && save.equals("1")) {

        Armazenamento tokenFcmStorage = new Armazenamento(context);
        tokenFcmStorage.SavePreferences("tokenFcm", tokenFcm);
    }
}
}

can someone please help me

ruitercomp
  • 162
  • 2
  • 16
  • Possible duplicate of [Can't create handler inside thread that has not called Looper.prepare() inside AsyncTask for ProgressDialog](http://stackoverflow.com/questions/3614663/cant-create-handler-inside-thread-that-has-not-called-looper-prepare-inside-a) – Onik Oct 25 '16 at 17:06
  • hello, thanks for comment. I tried using this solution that you said but is not performing my task. – ruitercomp Oct 25 '16 at 17:57

1 Answers1

3

I managed to find a solution, but I do not know if it's the best. From what I googled got this code in my class service FCM:

Handler handler = new Handler(Looper.getMainLooper());
    handler.postDelayed(new Runnable() {
        @Override
        public void run() {
            new RegistroTokenTask(getApplicationContext(), refreshedToken, "", SAVE_ABERTURA).execute();
        }
    }, 2000);
ruitercomp
  • 162
  • 2
  • 16