GCM works on 4.1 but doesn't work on android 2.3

I have a problem with GCM, it works fine on Nexus 7, but when I run it on any device with Gingerbread onRegistered version, the method is never called.

See my code implementation below:

GMCIntentService



public class GCMIntentService extends GCMBaseIntentService {
private static final String TAG = "GCMIntentService";

private RestHelper restRegisterGCM;
private String userRegisterGCMUrl = "User/SetGcm";

public GCMIntentService() {
    super(AppSettings.SENDER_ID);
}

/**
 * Method called on device registered
 **/
@Override
protected void onRegistered(Context context, String registrationId) {
    Log.i(TAG, "Device registered: regId = " + registrationId);
    // Util.displayMessage(context, "Your device registred with GCM");
    if (!GCMRegistrar.isRegisteredOnServer(this)) {

        restRegisterGCM = new RestHelper(userRegisterGCMUrl, RequestMethod.POST, context);
        restRegisterGCM.setHeader("UserName", AppSettings.getInstance().getUsername(context));
        restRegisterGCM.setHeader("Password", AppSettings.getInstance().getPassword(context));
        restRegisterGCM.setParameter("regId", registrationId);
        restRegisterGCM.execute();

    }
}

/**
 * Method called on device un registred
 * */
@Override
protected void onUnregistered(Context context, String registrationId) {
    Log.i(TAG, "Device unregistered");
}

/**
 * Method called on Receiving a new message
 * */
@Override
protected void onMessage(Context context, Intent intent) {
    Log.i(TAG, "Received message");
    String message = intent.getExtras().getString("Message");
    // notifies user
    generateNotification(context, message);
}

/**
 * Method called on receiving a deleted message
 * */
@Override
protected void onDeletedMessages(Context context, int total) {
    Log.i(TAG, "Received deleted messages notification");
}

/**
 * Method called on Error
 * */
@Override
public void onError(Context context, String errorId) {
    Log.i(TAG, "Received error: " + errorId);
    Toast.makeText(context, getString(R.string.gcm_error, errorId), Toast.LENGTH_SHORT).show();
}

@Override
protected boolean onRecoverableError(Context context, String errorId) {
    // log message
    Log.i(TAG, "Received recoverable error: " + errorId);
    Toast.makeText(context, getString(R.string.gcm_recoverable_error, errorId), Toast.LENGTH_SHORT).show();
    return super.onRecoverableError(context, errorId);
}

      

code>

GMC registration method

private void registerGCM() {

    // Make sure the device has the proper dependencies.
    GCMRegistrar.checkDevice(this);
    Boolean accountExists = false;

    AccountManager am = AccountManager.get(getApplicationContext());
    Account[] accounts = am.getAccounts();

    for (Account account : accounts) {

        if (account.type.equals("com.google")) {
            accountExists = true;
            break;
        }

    }
    if (accountExists) {

        // Get GCM registration id
        String regId = GCMRegistrar.getRegistrationId(this);

        // Check if regid already presents
        if (regId.equals("")) {
            // Registration is not present, register now with GCM
            GCMRegistrar.register(this, AppSettings.SENDER_ID);

        } else {
            // Device is already registered on GCM
            if (!GCMRegistrar.isRegisteredOnServer(this)) {

                restRegisterGCM = new RestHelper(userRegisterGCMUrl, RequestMethod.POST, EvadoFilipActivity.this);
                restRegisterGCM.setHeader("UserName", AppSettings.getInstance().getUsername(EvadoFilipActivity.this));
                restRegisterGCM.setHeader("Password", AppSettings.getInstance().getPassword(EvadoFilipActivity.this));
                restRegisterGCM.setParameter("regId", regId);
                restRegisterGCM.setPostExecuteMethod(2);
                restRegisterGCM.execute();

            }
        }
    } else
        Toast.makeText(this, R.string.gcm_google_account_missing, Toast.LENGTH_SHORT).show();

}

      

code>

UPDATE:

I renamed packages and forgot to change it in my class:

public class GCMBroadcastReceiver extends com.google.android.gcm.GCMBroadcastReceiver{
@Override protected String getGCMIntentServiceClassName(Context context) {


    return "com.mypackage.services.GCMIntentService";
}
}

      

code>

+3


source to share


2 answers


Make sure you set up a user account on the device under test. GCM requires google account to be set up on the device that registers for GCM (also I believe these are requirements for android 4.0 versions)



+1


source


I had the same problem. My code would work on nexus4 (Kitkat) but couldn't get me a notification from appln server (via gcm server). @ Fr0g is correct for versions lower than 4.0.4, you must make sure you have a Google account set up on your device for it to work. I had a google account on my ace of galaxy (2.3.4), but the mistake I made was that my Account and Sync settings in my ace of galaxy were "Off". When I enabled it and ran my code, I got a notification.



+2


source







All Articles