I'm currently trying to setup my Application to register with Google GCM in order to get a Registration ID.
I followed the documentation on Google Developers Website and so far everything is working well.
The context of my application is that I have a Library Application, which is supposed to get the token registration Id, while the host application which is using the library will be handling the notifications.
It's not really well explained in the documentation, but it seems perfectly possible to separe these two actions (Getting the token/Handling the notification) into an Host application and a Library Application (by chosing the right permissions/classes to add for each project).
To get the registration Id, you need to create an Asynctask which will be calling the gcm.register(SENDER_ID) method.
My problem is, that I need to wait for the result of this Asynctask to proceed then with the result in my current Application.
My Asynctask is as following :
public class PushTask extends AsyncTask<Void, Void, String> {
private GoogleCloudMessaging gcm;
private String regId;
public PushTask(Context ctx){
this.gcm = GoogleCloudMessaging.getInstance(ctx);
this.regId = "";
}
@Override
protected String doInBackground(Void... params) {
Log.v("PushTask", "Start getting GCM Token / Sender Id : "+CURRENT_SENDER_ID);
try {
this.regId = this.gcm.register(CURRENT_SENDER_ID);
} catch (IOException ex) {
try {
this.regId = this.gcm.register(CURRENT_SENDER_ID);
} catch (IOException e) {
e.printStackTrace();
}
}
Log.v("PushTask", "Should have GCM Token now : "+this.regId);
return this.regId;
}
}
This is working, but if I call my AsyncTask this way :
PushTask pt = new PushTask(this.ctx);
String token = pt.execute().get();
I'm systematicly getting a SERVICE_NOT_AVAILABLE exception,
Does anyone know why? because I haven't been able to find out more while searching the net, And does anyone know if I hae no other choice, than handling this with the onPostExecute method of my Asynctask, which I'm trying to avoid if possible.
Below the stacktrace of the SERVICE_NOT_AVAILABLE Exception :
05-23 16:59:47.441: W/System.err(13060): java.io.IOException: SERVICE_NOT_AVAILABLE
05-23 16:59:47.441: W/System.err(13060): at com.google.android.gms.gcm.GoogleCloudMessaging.register(Unknown Source)
05-23 16:59:47.441: W/System.err(13060): at *.push.PushTask.doInBackground(PushTask.java:29)
05-23 16:59:47.441: W/System.err(13060): at *.push.PushTask.doInBackground(PushTask.java:1)
05-23 16:59:47.441: W/System.err(13060): at android.os.AsyncTask$2.call(AsyncTask.java:287)
05-23 16:59:47.441: W/System.err(13060): at java.util.concurrent.FutureTask.run(FutureTask.java:234)
05-23 16:59:47.441: W/System.err(13060): at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
05-23 16:59:47.441: W/System.err(13060): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080)
05-23 16:59:47.441: W/System.err(13060): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573)
05-23 16:59:47.441: W/System.err(13060): at java.lang.Thread.run(Thread.java:841)
It seems that I'm getting this exception because I'm trying to call the get() method of an AsyncTask in the main Thread indeed (thx to Emannuel's comments below), because it's forbidden to call the get() method on the main thread,
But Android, instead of throwing a networkOnmainThreadException, is throwing a SERVICE_NOT_AVAILABLE IOException, I don't know why.