Does Google Play Services handle offline device for registering GCM token?

282 views Asked by At

I'm using the code below to register app token :

    public class RegistrationIntentService extends IntentService {

    private static final String TAG = "RegIntentService";
    private static final String[] TOPICS = {"global"};
    public static final String TOKEN_ID = "registration_id";
    private final static int MAX_ATTEMPTS = 5;
    private final static int BACKOFF_MILLI_SECONDS = 2000;
    private String token = null;
    public RegistrationIntentService() {
        super(TAG);
    }

    @Override
    protected void onHandleIntent(Intent intent) {
        SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this);
        try {
            synchronized (TAG) {
                Random random = new Random();

                InstanceID instanceID = InstanceID.getInstance(this);
                long backoff = BACKOFF_MILLI_SECONDS + random.nextInt(1000);
                for (int i = 1; i <= MAX_ATTEMPTS; i++) {
                    try {
                        token = instanceID.getToken(getString(R.string.gcm_defaultSenderId), GoogleCloudMessaging.INSTANCE_ID_SCOPE, null);

                        sharedPreferences.edit().putBoolean(QuickstartPreferences.SENT_TOKEN_TO_SERVER, true).apply();
                    } catch (IOException e) {
                        if (i == MAX_ATTEMPTS) {
                            break;
                        }
                        try {
                            Thread.sleep(backoff);
                        } catch (InterruptedException e1) {
                            break;
                        }
                    }
                    // increase backoff exponentially
                    backoff *= 2;
                }
                // further processing for token goes here
            }

            sendRegistrationToServer(token);
            subscribeTopics(token);
} catch (Exception e) {
            Log.d(TAG, "Failed to complete token refresh", e);
           // sharedPreferences.edit().putBoolean(QuickstartPreferences.SENT_TOKEN_TO_SERVER, false).apply();
        }
Intent registrationComplete = new Intent(QuickstartPreferences.REGISTRATION_COMPLETE);
        LocalBroadcastManager.getInstance(this).sendBroadcast(registrationComplete);
}
}

But if the device were offline, it wouldn't register the token to the app server. And when the device connects, it has no registered token in the app server DB and I can't send push notifications to it.

How can I manage the app to send it's token to the app server when it connects?

I've tried to add something like :

    try {
            token = instanceID.getToken(getString(R.string.gcm_defaultSenderId), GoogleCloudMessaging.INSTANCE_ID_SCOPE, null);
RegisterTOServer(token);
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }

to onResume() method in the main activity, but it makes the app to much slow.

0

There are 0 answers