Skip to content Skip to sidebar Skip to footer

Corresponding Gcmregistrar.isregistered() In New Gcm

I am trying to implement GCM using the new GoogleCloudMessaging API. I want to check whether the device is registered on the cloud, which was previously checked using GCMRegistrar.

Solution 1:

The deprecated GCMRegistrar was just a helper client class that stored the RegistrationID locally on the device. GCMRegistrar.isRegistered() never called the GCM server to find if the device is registered (since there is no such API). It just checked if a previously received RegistrationID is locally stored on the device for a specific app (and invalidated the stored RegistrationId on some occassions, such as when the app version changes).

In fact, you can see the code of GCMRegistrar here:

/**
 * Gets the current registration id for application on GCM service.
 * <p>
 * If result is empty, the registration has failed.
 *
 * @return registration id, or empty string if the registration is not
 *         complete.
 */publicstaticString getRegistrationId(Context context) {
    final SharedPreferences prefs = getGCMPreferences(context);
    String registrationId = prefs.getString(PROPERTY_REG_ID, "");
    // check if app was updated; if so, it must clear registration id to// avoid a race condition if GCM sends a messageint oldVersion = prefs.getInt(PROPERTY_APP_VERSION, Integer.MIN_VALUE);
    int newVersion = getAppVersion(context);
    if (oldVersion != Integer.MIN_VALUE && oldVersion != newVersion) {
        Log.v(TAG, "App version changed from " + oldVersion + " to " +
                newVersion + "; resetting registration id");
        clearRegistrationId(context);
        registrationId = "";
    }
    return registrationId;
}

/**
 * Checks whether the application was successfully registered on GCM
 * service.
 */publicstaticboolean isRegistered(Context context) {
    return getRegistrationId(context).length() > 0;
}

Therefore, if you store the registration ID in your app, you would achieve the exact same functionality you had when you used GCMRegistrar. The only way to know for sure in the client app that the device is registered or not registered to GCM is to call GoogleCloudMessaging.register or GoogleCloudMessaging.unregister.

Solution 2:

You need to do this

StringSENDER_ID="13312313";//product id will be different for you
GoogleCloudMessaging gcm;
Contextcontext=getApplicationContext();
String regid;

Stringmsg="";
                try {
                    if (gcm == null) {
                        gcm = GoogleCloudMessaging.getInstance(context);
                    }
                    regid =   gcm.register(SENDER_ID);
                    storeRegistrationId(context, regid);
                    msg = regid;
                } catch (IOException ex) {
                    msg=null;
                }

save to sharedpreferences

privatevoidstoreRegistrationId(Context context, String regId) {
        finalSharedPreferencesprefs= getGCMPreferences(context);

        Log.i(TAG, "Saving regId on app version " + appVersion);
        SharedPreferences.Editoreditor= prefs.edit();
        editor.putString(PROPERTY_REG_ID, regId);

        editor.commit();
    }

Now check for exixtence

finalSharedPreferencesprefs= getGCMPreferences(context);
        StringregistrationId= prefs.getString(PROPERTY_REG_ID, "");
        if (TextUtils.isEmpty(registrationId)) {
            Log.i(TAG, "Registration not found.");
            returnnull;
        }

private SharedPreferences getGCMPreferences(Context context) {
        // This sample app persists the registration ID in shared preferences, but// how you store the regID in your app is up to you.return getSharedPreferences(this.getClass().getSimpleName(),
                Context.MODE_PRIVATE);
    }   

Solution 3:

For your concern U can check registration status and much more like this

publicclassGCMIntentServiceextendsGCMBaseIntentService {

    privatestaticfinalStringTAG="GCMIntentService";

    publicGCMIntentService() {
        super(SENDER_ID);
    }

    /**
     * Method called on device registered
     **/@OverrideprotectedvoidonRegistered(Context context, String registrationId) {
        Log.i(TAG, "Device registered: regId = " + registrationId);
        // Toast.makeText(getApplicationContext(), "Your registration ID : " +// registrationId, Toast.LENGTH_LONG).show();
        displayMessage(context, "Your device registred with GCM"
                + registrationId);
        //Log.d("NAME", LoginActivity.email);
        ServerUtilities.register(context, LoginActivity.uid,
                LoginActivity.phone, registrationId);
    }

    /**
     * Method called on device un registred
     * */@OverrideprotectedvoidonUnregistered(Context context, String registrationId) {
        Log.i(TAG, "Device unregistered");
        displayMessage(context, getString(R.string.gcm_unregistered));
        ServerUtilities.unregister(context, registrationId);
    }

    /**
     * Method called on Receiving a new message
     * */@OverrideprotectedvoidonMessage(Context context, Intent intent) {
        Log.i(TAG, "Received message");
        Stringmessage= intent.getExtras().getString("price");

        displayMessage(context, message);
        // notifies user
        generateNotification(context, message);
    }

    /**
     * Method called on receiving a deleted message
     * */@OverrideprotectedvoidonDeletedMessages(Context context, int total) {
        Log.i(TAG, "Received deleted messages notification");
        Stringmessage= getString(R.string.gcm_deleted, total);
        displayMessage(context, message);
        // notifies user
        generateNotification(context, message);
    }

    /**
     * Method called on Error
     * */@OverridepublicvoidonError(Context context, String errorId) {
        Log.i(TAG, "Received error: " + errorId);
        displayMessage(context, getString(R.string.gcm_error, errorId));
    }

    @OverrideprotectedbooleanonRecoverableError(Context context, String errorId) {
        // log message
        Log.i(TAG, "Received recoverable error: " + errorId);
        displayMessage(context,
                getString(R.string.gcm_recoverable_error, errorId));
        returnsuper.onRecoverableError(context, errorId);
    }

    /**
     * Issues a notification to inform the user that server has sent a message.
     * This message wil be notified at status bar!!!
     */@SuppressWarnings("deprecation")privatestaticvoidgenerateNotification(Context context, String message) {
        inticon= R.drawable.ic_launcher;
        longwhen= System.currentTimeMillis();
        NotificationManagernotificationManager= (NotificationManager) context
                .getSystemService(Context.NOTIFICATION_SERVICE);

        Notificationnotification=newNotification(icon, message, when);

        Stringtitle= context.getString(R.string.app_name);

        IntentnotificationIntent=newIntent(context, MainActivity.class);
        // set intent so it does not start a new activity
        notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP
                | Intent.FLAG_ACTIVITY_SINGLE_TOP);
        PendingIntentintent= PendingIntent.getActivity(context, 0,
                notificationIntent, 0);
        notification.setLatestEventInfo(context, title, message, intent);
        notification.flags |= Notification.FLAG_AUTO_CANCEL;

        // Play default notification sound
        notification.defaults |= Notification.DEFAULT_SOUND;

        // notification.sound = Uri.parse("android.resource://" +// context.getPackageName() + "your_sound_file_name.mp3");// Vibrate if vibrate is enabled
        notification.defaults |= Notification.DEFAULT_VIBRATE;
        notificationManager.notify(0, notification);

    }

}

Post a Comment for "Corresponding Gcmregistrar.isregistered() In New Gcm"