I have an app which is basically get the notification from my own server using Firebase Cloud Messaging service whenever the API is called, strangely when i install it through Android studio RUN menu, which is install the app directly to my device the APP is working fine, but when i take the app-debug from my project directory and install it manually, it gives me the error
Can't create handler inside thread that has not called Looper.prepare()
the error points out to my method that sending data to my database using volley library inside onTokenRefresh() Method.
i am putting that methods inisde a serveice that extends FirebaseInstanceIdService.
this is the complete error message
E/AndroidRuntime: FATAL EXCEPTION: pool-3-thread-1
Process: proizzy.com.qrcodeproizzyadm, PID: 5704
java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare()
at android.os.Handler.<init>(Handler.java:200)
at android.os.Handler.<init>(Handler.java:114)
at android.widget.Toast$TN.<init>(Toast.java:645)
at android.widget.Toast.<init>(Toast.java:137)
at android.widget.Toast.makeText(Toast.java:440)
at proizzy.com.qrcodeproizzyadm.Notification.FirebaseInstanceIDService.onTokenRefresh(FirebaseInstanceIDService.java:25)
at com.google.firebase.iid.FirebaseInstanceIdService.zza(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:1112)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
at java.lang.Thread.run(Thread.java:818)
i dont have any clue what went wrong with my code.
this is my complete code of the service class
public class FirebaseInstanceIDService extends FirebaseInstanceIdService {
@Override
public void onTokenRefresh() {
final String token = FirebaseInstanceId.getInstance().getToken();
Log.v("tok", token);
String tokUrl = "someurl.php";
JsonObjectRequest regToken = new JsonObjectRequest(Request.Method.POST, tokUrl, null,
new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
Log.v("success", response.toString());
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Log.v("errorvolley", error.getMessage());
}
}){
@Override
protected Map<String, String> getParams() {
Map<String, String> params = new HashMap<>();
params.put("token", token);
return params;
}
};
RequestQueue regQueue = Volley.newRequestQueue(getApplicationContext());
regQueue.add(regToken);
}
OK so i've solved the problems, for some reason if you install app manually from debug-app you will get the error as i mentioned in my question. i solved it by saving the Token that i get from FCM to a shared preference and later when the user log in, i send the token to my database. this way imo is less efficient as the user has to send the token everytime they log in but the token remain the same (until they cleared the data and or uninstall the app).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With