7

PROBLEM SOLVED UPDATE:

I solved this Problem. The problem is in method registerGCMInBackground(). GoogleCloudMessaging.getInstance() expects the ApplicationContext.

    private void registerGCMInBackground(final String userId) {
    new AsyncTask<Void, Void, String>() {
        @Override
        protected String doInBackground(Void... params) {
            String msg = "";
            Log.d("Register GCM", "started");
            try {
                if (gcm == null) {
                    //PROBLEM SOLVED HERE
                    gcm = GoogleCloudMessaging.getInstance(getApplicationContext());
                    Log.d("GCM", gcm.toString());
                }



                regid = gcm.register(SENDER_ID); //////NULL POINTER EXCEPTION
                msg = "Device registered, registration ID=" + regid;



                // You should send the registration ID to your server over HTTP
                sendRegistrationIdToBackend(userId, regid);

                // 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) {
                return "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 regid;
        }

-------- Solved Problem--------------

I have problems with registering my Android device at GCM (Google Cloud Messaging). I'm using the example from Google Developers. With the use of the Google Developer Example I ensured the following things:

  • adding latest Play Services Library
  • adding latest Play Services JAR
  • correct Manifest file
  • having Play Services APK installed on device
  • using Project Number as SENDER_ID

It seems to be the same issue like this Question. Unfortunately still not answered. I'm getting a Null Pointer Exception by the following line:

regid = gcm.register(SENDER_ID);

Here is my Problem Code: Sample Activity

     static final String TAG = "GCM";
    public static final String EXTRA_MESSAGE = "message";
    public static final String PROPERTY_REG_ID = "registration_id";
    private static final String PROPERTY_APP_VERSION = "appVersion";
    private static final int PLAY_SERVICES_RESOLUTION_REQUEST = 9000;
    String SENDER_ID = "xxxxxxxxxxxxxxx";
    GoogleCloudMessaging gcm;
    AtomicInteger msgId = new AtomicInteger();
    Context context;

    @Override
    protected void onCreate(Bundle savedInstanceState) {

            super.onCreate(savedInstanceState);
            setContentView(R.layout.registration);

            final Button register = (Button) findViewById(R.id.btn_register);

            register.setOnClickListener(new View.OnClickListener() {

                @Override
                public void onClick(View v) {

                     EditText mobilePhoneNumber = (EditText) findViewById(R.id.et_phonenumber);

                    if(!mobilePhoneNumber.getText().toString().isEmpty()){
                    User user = new User();
                    user.setMobilePhoneNumber(mobilePhoneNumber.getText().toString());

                     EditText firstName = (EditText) findViewById(R.id.et_firstName);
                    user.setFirstName(firstName.getText().toString());

                     EditText lastName = (EditText) findViewById(R.id.et_lastName);
                    user.setLastName(lastName.getText().toString());

                     EditText email = (EditText) findViewById(R.id.et_email);
                    user.setEmail(email.getText().toString());
                    Log.d("email: ", user.getEmail().toString());

                    if (android.os.Build.VERSION.SDK_INT>14) {
                        StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder()
                                .permitAll().build();
                        StrictMode.setThreadPolicy(policy);
                    }

                    UserService userService = new UserService();
                    String path = userService.createNewUser(user);




                    Log.d("Location: ", path);
                    Toast.makeText(getApplicationContext(), 
                            path, Toast.LENGTH_LONG).show();

                    String userID = path.replace("/user/", "");
                    userID = userID.replace("/users/", "");
                    Log.d("UserID:::", userID);
                    Log.d("User Service", "register in Background started");
                    if(checkPlayServices()){
                    registerGCMInBackground(userID);
                    }

                    }
                    else{
                        Toast.makeText(getApplicationContext(), 
                                "Please enter your Mobile Phone Number", Toast.LENGTH_LONG).show();
                    }           
                }
            });

    private void registerGCMInBackground(final String userId) {
        new AsyncTask<Void, Void, String>() {
            @Override
            protected String doInBackground(Void... params) {
                String msg = "";
                Log.d("Register GCM", "started");
                try {
                    if (gcm == null) {
                        gcm = GoogleCloudMessaging.getInstance(context);
                        Log.d("GCM", gcm.toString());
                    }



                    regid = gcm.register(SENDER_ID); //////NULL POINTER EXCEPTION
                    msg = "Device registered, registration ID=" + regid;



                    // You should send the registration ID to your server over HTTP
                    sendRegistrationIdToBackend(userId, regid);

                    // 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) {
                    return "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 regid;
            }

            @Override
            protected void onPostExecute(String msg) {

            }
        }.execute(null, null, null);
    }

Here is my Console Output: Console Output

I'm dealing since two full days with this issue. :( Thank you very much!

7
  • same problem... is this resolved?
    – thedjaney
    Feb 15, 2014 at 6:55
  • Problem is solved. See Update. Feb 16, 2014 at 11:20
  • 6
    @FullPatrickJoin If you found the answer to your question you are encouraged to post it as an answer, and not add it to the question. I see that you did that and deleted the answer after some comment from someone. That comment was wrong. People answer their own questions in SO all the time and it's perfectly fine. It's even fine to mark your own answer as accepted (since it solved your problem).
    – Eran
    Feb 16, 2014 at 22:33
  • I see.. I also solved the problem separately with same solution.
    – thedjaney
    Feb 17, 2014 at 9:02
  • @Eran Thank you for your enlightenment. Feb 17, 2014 at 12:39

2 Answers 2

2

Apparently as of May 28, 2015. Obtaining Registration Tokens with gcm.register() was Deprecated. New app development should use the Instance ID API to handle the creation, rotation, and updating of registration tokens. For more information, see Registering Client Apps and Set up a GCM Client App on Android. From Google https://developers.google.com/cloud-messaging/android/legacy-regid

1

I solved this Problem. The problem is in method registerGCMInBackground(). GoogleCloudMessaging.getInstance() expects the ApplicationContext.

    private void registerGCMInBackground(final String userId) {
    new AsyncTask<Void, Void, String>() {
        @Override
        protected String doInBackground(Void... params) {
            String msg = "";
            Log.d("Register GCM", "started");
            try {
                if (gcm == null) {
                    //PROBLEM SOLVED HERE
                    gcm = GoogleCloudMessaging.getInstance(getApplicationContext());
                    Log.d("GCM", gcm.toString());
                }



                regid = gcm.register(SENDER_ID); //////NULL POINTER EXCEPTION
                msg = "Device registered, registration ID=" + regid;



                // You should send the registration ID to your server over HTTP
                sendRegistrationIdToBackend(userId, regid);

                // 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) {
                return "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 regid;
        }
1
  • This is strange. I have the same problem and changing it to getApplicationContext() doesn't solve the problem. The application runs well on Lollipop.
    – bashan
    Aug 21, 2015 at 18:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.