Damir
Damir

Reputation: 1112

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

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

See my code implementation belowe:

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);
}

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();

}

UPDATE:

I have renamed packages and forget 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";
}
}

Upvotes: 3

Views: 892

Answers (2)

Basher51
Basher51

Reputation: 1329

I had the very same problem.My code would work on nexus4(Kitkat) but would fail to get me a notification from the appln server(via gcm server).@Fr0g is correct for versions less that 4.0.4 you should make sure that you have your google account setup on your device for gcm to work. I had google account on my galaxy ace(2.3.4) but the mistake I made was that my Account and Sync settings in my galaxy ace was 'Off'.When I turned it ON and ran my code, i received the notification.

Upvotes: 2

Fr0g
Fr0g

Reputation: 252

Ensure that you have set up a user account on the device that you are testing on. GCM requires that a google account must be setup on the device that is registering for GCM, (also I think that this requirements is for android versions < 4.0)

Upvotes: 1

Related Questions