GCM适用于4.1但不适用于2.3 android版本

时间:2013-01-28 13:30:05

标签: google-cloud-messaging

我遇到了GCM的问题,它在Nexus 7上运行得很好,但是当我在任何使用Gingerbread版本的onRegistered方法的设备上运行它时,它永远不会被调用。

请参阅下面的代码实现:

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

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注册方法

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

}

更新:

我已经重命名了包,忘记在课堂上更改它:

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

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

2 个答案:

答案 0 :(得分:2)

我遇到了同样的问题。我的代码可以在nexus4(Kitkat)上运行,但是无法从appln服务器(通过gcm服务器)向我发送通知。@ Fr0g对于4.0.4以下的版本是正确的确保您在设备上设置了Google帐户,以便gcm正常运行。 我在我的Galaxy ace(2.3.4)上有谷歌帐户,但我犯的错误是我的银河王牌中的帐户和同步设置是'关闭'。当我打开它并运行我的代码时,我收到了通知。 / p>

答案 1 :(得分:1)

确保您在要测试的设备上设置了用户帐户。 GCM要求必须在注册GCM的设备上设置Google帐户(我也认为此要求适用于Android版本< 4.0)