3

我在使用 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);
}

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

}

更新:

我已重命名包并忘记在我的班级中更改它:

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

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

4

2 回答 2

2

我遇到了同样的问题。我的代码可以在 nexus4(Kitkat) 上运行,但无法从 appln 服务器(通过 gcm 服务器)收到通知。@Fr0g 对于低于 4.0.4 的版本是正确的,你应该确保你在你的设备上设置了你的谷歌帐户,以便 gcm 工作。我的galaxy ace(2.3.4)上有谷歌帐户,但我犯的错误是我的galaxy ace中的帐户和同步设置为“关闭”。当我打开它并运行我的代码时,我收到了通知。

于 2013-11-25T01:18:47.900 回答
1

确保您已在要测试的设备上设置用户帐户。GCM 要求必须在注册 GCM 的设备上设置一个谷歌帐户,(我也认为这个要求适用于 android 版本 < 4.0)

于 2013-01-28T13:37:05.657 回答