Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Google Cloud Messaging register method fails with IOException (MAIN_THREAD)

Followed Google's GCM example extremely carefully, but still came up with frustrating errors. Didn't find the answer here, so thought I'd document it for future reference.

I was fairly sure that I had the manifest properly set up with the correct permissions etc (following the above example to the letter)... but this code always caused an IOException with the explanation "MAIN_THREAD".

String regid = "";
try {
    GoogleCloudMessaging gcm = GoogleCloudMessaging.getInstance(getApplicationContext());
    String sender_id = getResources().getString(R.string.GCM_sender_id);
    regid = gcm.register(sender_id);
    Log.d(TAG, "Device registered, registration id=" + regid);
} 
catch (IOException ex) {
    Log.e(TAG, "Device registration failed", ex);
}

I was trying to find the problem by stepping through, so of course executed this code on the main thread. In retrospect the answer is obvious... but I'll enter it below.

like image 700
mblackwell8 Avatar asked Jun 09 '13 10:06

mblackwell8


1 Answers

Drum roll... you can't call the register method on the main thread. Obvious in retrospect, but perhaps there are others that suck as much as I do (but less each day!)

like image 181
mblackwell8 Avatar answered Nov 15 '22 11:11

mblackwell8