google cloud messaging in android lower than 4.1

吃可爱长大的小学妹 提交于 2020-01-03 06:21:08

问题


I'm using push notifications (GCM) in my application and it works correctly when run on android 4.2.2 (Galaxy Nexus and HTC One)

Also I have a tablet (asus transformer T101 running android 4.03) and Nexus One (running Android 2.3.6)

Running the application on the tablet and the Nexus one, methods of push notifications do not work.

As indicated in the documentation, I have included android.permission.GET_ACCOUNTS the permission for versions below 4.0.3, but without success.

The method that calls the library of GCM runs:

final String regId = GCMRegistrar.getRegistrationId(Hello.this);
        if (regId.equals("")) {
            GCMRegistrar.register(Hola.this, "MY_ID_SENDER"); //Sender ID           
        } else {
            Log.i("masterhv", "already registered");
        }

But the method that handles the response is not executed (onRegistered) ... I understand that there is no response from Google

As I said, it works perfectly on devices with android 4.2.2 and 4.1, permissions are correct, but does not work on android 4.0.3 and lower.

I appreciate any help.

Thanks and regards


回答1:


With the old version of GCM (deprecated 1-2 month ago aprox.) you had to ask to the user to add a google account if he was running a version lower than 4.0.4

Looking at the API docs it is still a requirement

Mobile Device The device that is running an Android application that uses GCM. This must be a 2.2 Android device that has Google Play Store installed, and it must have at least one logged in Google account if the device is running a version lower than Android 4.0.4. Alternatively, for testing you can use an emulator running Android 2.2 with Google APIs.

GCM API

I have not used the last version. If you decide to use the old one you have to generate an acces to the account settings to add an account:

private String[] getAccountNames() {        
        AccountManager accMan = AccountManager.get(this);
        Account[] accArray = accMan.getAccountsByType("com.google");
        String[] names = new String[accArray.length];
        for (int i = 0; i < names.length; i++) {
            names[i] = accArray[i].name;
        }
    return names;
}

So if the array is 0 you can pop-up a dialog asking to move to the account settings.

Without an account if you try to register you should see on your logcat a message error ACCOUNT_MISSING.



来源:https://stackoverflow.com/questions/17708128/google-cloud-messaging-in-android-lower-than-4-1

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!