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.
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!)
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