0

I have an activity that call an Otto Event called LoadAuthenticateEvent this event then goes to my ClientManager.java where the following code is:

@Subscribe
public void onLoadAuthenticateEvent(LoadAuthenticateEvent loadAuthenticateEvent) {

    // GCM cannot register on the main thread
    String deviceID = "";
    Thread thread = new Thread(new Runnable() {
        @Override
        public void run() {
            String differentId = GCMRegistrationUtil.registerDevice(mContext);
            Log.d(TAG, "Device Id: " + differentId);
        }
    });
    thread.start();


    String email = loadAuthenticateEvent.getEmail();
    String password = loadAuthenticateEvent.getPassword();

    Callback<User> callback = new Callback<User>() {
        @Override
        public void success(User user, Response response) {
            sClient.setOrganization(user.getRole().getOrganization().getSubdomain());
            mBus.post(new LoadedMeEvent(user));
        }

        @Override
        public void failure(RetrofitError retrofitError) {
            mBus.post(new LoadedErrorEvent(retrofitError));
        }
    };

    sClient.authenticate(email, password, deviceID, PLATFORM, callback);
}

The problem is that the server needs the deviceID, but GCM requires that a call be asynchronous and not on the main thread, how should I go about implementing this where I can properly get the deviceID and then pass it to sClient? Since it is possible that deviceID might be empty.

AndyRoid
  • 5,062
  • 8
  • 38
  • 73

3 Answers3

2

The best way is to contact GCM is through services.

  1. Creates a IntentService to catch the intent released from the activity

    onHandleIntent(Intent intent)

  2. Device sends service request GCM and receives the tokenID.

    InstanceID instanceID = InstanceID.getInstance(this); String token = instanceID.getToken(getString(R.string.gcm_defaultSenderId), GoogleCloudMessaging.INSTANCE_ID_SCOPE, null);

  3. Implement this method to send any registration to your app's servers.

    sendRegistrationToserver(token)

  4. Notify UI that registrationComplete

    Intent registrationComplete = new Intent(GcmUtils.REGISTRATION_COMPLETE); LocalBroadcastManager.getInstance(this).sendBroadcast(registrationComplete);

  5. (Optional) Subscribe to topic channels private void subscribeTopics(String token, ArrayList topics_gcm)throws IOException { for (String topic : topics_gcm) { GcmPubSub pubSub = GcmPubSub.getInstance(this); pubSub.subscribe(token, topic, null); } }

Full IntentService:

public class RegistrationIntentService extends IntentService {
private static final String TAG = "RegIntentService";

public RegistrationIntentService() {
    super(TAG);
}

@Override
protected void onHandleIntent(Intent intent) {

    ArrayList<String> topics_gcm = intent.getStringArrayListExtra("topics_gcm");
    SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this);

    try {
        // In the (unlikely) event that multiple refresh operations occur simultaneously,
        // ensure that they are processed sequentially.
        synchronized (TAG) {
            // Initially this call goes out to the network to retrieve the token, subsequent calls
            // are local.
            // [START get_token]
            InstanceID instanceID = InstanceID.getInstance(this);
            String token = instanceID.getToken(getString(R.string.gcm_defaultSenderId),
                    GoogleCloudMessaging.INSTANCE_ID_SCOPE, null);
            // [END get_token]
            Log.i(TAG, "GCM Registration Token: " + token);

            // TODO: Implement this method to send any registration to your app's servers.
            //sendRegistrationToServer(token);

            // TODO: Subscribe to topic channels
            //subscribeTopics(token, topics_gcm);

            // You should store a boolean that indicates whether the generated token has been
            // sent to your server. If the boolean is false, send the token to your server,
            // otherwise your server should have already received the token.
            sharedPreferences.edit().putBoolean(GcmUtils.SENT_TOKEN_TO_SERVER, true).apply();
            // [END register_for_gcm]
        }
    } catch (Exception e) {
        Log.d(TAG, "Failed to complete token refresh", e);
        // If an exception happens while fetching the new token or updating our registration data
        // on a third-party server, this ensures that we'll attempt the update at a later time.
        sharedPreferences.edit().putBoolean(GcmUtils.SENT_TOKEN_TO_SERVER, false).apply();
    }
    // Notify UI that registration has completed, so the progress indicator can be hidden.
    Intent registrationComplete = new Intent(GcmUtils.REGISTRATION_COMPLETE);
    LocalBroadcastManager.getInstance(this).sendBroadcast(registrationComplete);
}

/**
 * Persist registration to third-party servers.
 *
 * Modify this method to associate the user's GCM registration token with any server-side account
 * maintained by your application.
 *
 * @param token The new token.
 */
private void sendRegistrationToServer(String token) {
    // Add custom implementation, as needed.
}

/**
 * Subscribe to any GCM topics of interest, as defined by the TOPICS constant.
 *
 * @param token GCM token
 * @throws IOException if unable to reach the GCM PubSub service
 */
// [START subscribe_topics]
private void subscribeTopics(String token, ArrayList<String> topics_gcm) throws IOException {
    for (String topic : topics_gcm) {
        GcmPubSub pubSub = GcmPubSub.getInstance(this);
        pubSub.subscribe(token, topic, null);
    }
}
// [END subscribe_topics]

}

  1. Start IntentService from any context: activity, service....
    Intent intent = new Intent(getContext(), egistrationIntentService.class); intent.putCharSequenceArrayListExtra("topics_gcm", topcics_gcm); getContext().startService(intent);
SergioLucas
  • 1,094
  • 8
  • 16
0

If you want to make the sClient call on the UI thread (not sure if that is a good solution for you), use a Handler to call it once your GCM registration ID is returned by GCM.

The accepted answer here has sample code to help you out.

Community
  • 1
  • 1
Koh
  • 1,570
  • 1
  • 8
  • 6
0

I ended up using an AsyncTask for this specific thing like so:

private void registerInBackground() {

    new AsyncTask<Void, Void, String>() {

        @Override
        protected String doInBackground(Void... params) {
            String regId = "";
            try {
                if (gcm == null) {
                    gcm = GoogleCloudMessaging.getInstance(mContext);
                }

                regId = gcm.register(GCMConfig.getSenderId());
                storeRegistrationId(mContext, regId);

            } catch (IOException e) {
                Log.e(TAG, "Error: ", e);
                e.printStackTrace();
            }

            Log.d(TAG, "GCM AsyncTask completed");
            return regId;
        }

        @Override
        protected void onPostExecute(String result) {

            // Get the message
            Log.d(TAG, "Registered with GCM Result: " + result);

        }

    }.execute(null, null, null);

}

This worked really well inside the GCMRegistration class

AndyRoid
  • 5,062
  • 8
  • 38
  • 73