SOLVING UPDATE PROBLEM :
I solved this problem. The problem is the registerGCMInBackground() method. GoogleCloudMessaging.getInstance() expects 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) {
-------- Solved problem --------------
I'm having problems registering my Android device with GCM (Google Cloud Messaging). I am using an example from Google Developers . Using the Google Developer example, I did the following:
- adding the latest service library
- adding the latest Play Services JAR
- valid manifest file
- on which the APK Play Services device installed on the device is installed
- using project number as SENDER_ID
It seems to be the same problem as this Question . Unfortunately, still have not answered. I get a Null Pointer exception on the following line:
regid = gcm.register(SENDER_ID);
Here is my problem code: Activity example
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);
Here is my console output: Console Out
I have been dealing with two full days with this problem. :( Thank you so much!
android google-play-services google-cloud-messaging
FullPatrickJoin
source share