Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Exception Android Google Cloud messaging register

When I'm trying to register my app device to the server, I'm getting this error: I have imported the gogole-play-services_lib like an application and added this library in my application in the Properties->Android->Add

    12-10 16:00:03.026: E/AndroidRuntime(29362): FATAL EXCEPTION: AsyncTask #1
12-10 16:00:03.026: E/AndroidRuntime(29362): Process: com.appehour.jdm, PID: 29362
12-10 16:00:03.026: E/AndroidRuntime(29362): java.lang.RuntimeException: An error occured while executing doInBackground()
12-10 16:00:03.026: E/AndroidRuntime(29362):    at android.os.AsyncTask$3.done(AsyncTask.java:300)
12-10 16:00:03.026: E/AndroidRuntime(29362):    at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:355)
12-10 16:00:03.026: E/AndroidRuntime(29362):    at java.util.concurrent.FutureTask.setException(FutureTask.java:222)
12-10 16:00:03.026: E/AndroidRuntime(29362):    at java.util.concurrent.FutureTask.run(FutureTask.java:242)
12-10 16:00:03.026: E/AndroidRuntime(29362):    at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
12-10 16:00:03.026: E/AndroidRuntime(29362):    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
12-10 16:00:03.026: E/AndroidRuntime(29362):    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
12-10 16:00:03.026: E/AndroidRuntime(29362):    at java.lang.Thread.run(Thread.java:841)
12-10 16:00:03.026: E/AndroidRuntime(29362): Caused by: java.lang.NullPointerException
12-10 16:00:03.026: E/AndroidRuntime(29362):    at android.app.PendingIntent.getBroadcastAsUser(PendingIntent.java:478)
12-10 16:00:03.026: E/AndroidRuntime(29362):    at android.app.PendingIntent.getBroadcast(PendingIntent.java:467)
12-10 16:00:03.026: E/AndroidRuntime(29362):    at com.google.android.gms.gcm.GoogleCloudMessaging.d(Unknown Source)
12-10 16:00:03.026: E/AndroidRuntime(29362):    at com.google.android.gms.gcm.GoogleCloudMessaging.b(Unknown Source)
12-10 16:00:03.026: E/AndroidRuntime(29362):    at com.google.android.gms.gcm.GoogleCloudMessaging.register(Unknown Source)
12-10 16:00:03.026: E/AndroidRuntime(29362):    at com.appehour.jdm.TestPush$registerInBackground.doInBackground(TestPush.java:172)
12-10 16:00:03.026: E/AndroidRuntime(29362):    at com.appehour.jdm.TestPush$registerInBackground.doInBackground(TestPush.java:1)
12-10 16:00:03.026: E/AndroidRuntime(29362):    at android.os.AsyncTask$2.call(AsyncTask.java:288)
12-10 16:00:03.026: E/AndroidRuntime(29362):    at java.util.concurrent.FutureTask.run(FutureTask.java:237)
12-10 16:00:03.026: E/AndroidRuntime(29362):    ... 4 more

And this is my code in background:

 @Override
        protected String doInBackground(String... params) {


            String msg = "";
            try {
                if (gcm == null) {
                    gcm = GoogleCloudMessaging.getInstance(context);
                }

                regid = gcm.register(SENDER_ID);
                msg = "Device registered, registration ID=" + regid;
                Log.d(TAG_LOGS, "Device registered, registration ID=" + regid);

                // You should send the registration ID to your server over HTTP,
                // so it can use GCM/HTTP or CCS to send messages to your app.
                // The request to your server should be authenticated if your app
                // is using accounts.
                sendRegistrationIdToBackend();

                // For this demo: we don't need to send it because the device
                // will send upstream messages to a server that echo back the
                // message using the 'from' address in the message.

                // Persist the regID - no need to register again.
                storeRegistrationId(context, regid);
            } catch (IOException ex) {
                msg = "Error :" + ex.getMessage();
                Log.e(TAG_LOGS, "Error :" + ex.getMessage());
                // If there is an error, don't just keep trying to register.
                // Require the user to click a button again, or perform
                // exponential back-off.
            }
            return msg;
        }
     }

I don't know how resolve it. Thank's you.

like image 758
Flofloaud1034 Avatar asked Dec 25 '22 16:12

Flofloaud1034


2 Answers

This one is very tricky. Verify that

GoogleCloudMessaging.getInstance(context);

is not running with a null context. It was the case for me and I got exactly the error that you described. After I fixed my context (I forgot mContext=ctx in my helper class constructor) the error disappeared.

like image 117
Fabian Frank Avatar answered Jan 15 '23 01:01

Fabian Frank


Another situation that this error happens is when google play services is not installed on device. For example on genymotion VDs and cyanogenmod (w/o google apps installed) devices.

like image 35
Mohammad Jafar Mashhadi Avatar answered Jan 15 '23 01:01

Mohammad Jafar Mashhadi