Android GCM没有收到registrationId

时间:2014-04-04 06:10:57

标签: android android-manifest google-cloud-messaging

我的大多数Android设备都遇到了问题。我可以使用我的Nexus One接收注册ID,但是对于大多数其他Android设备,它会返回一个空字符串。

当我调用

时,它返回空字符串
regid = gcm.register(SENDER_ID);

MainActivity:

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        SENDER_ID = getString(R.string.sender_id);
        this.requestWindowFeature(Window.FEATURE_NO_TITLE);
        gcm = GoogleCloudMessaging.getInstance(this);
        regid = getRegistrationId(getApplicationContext());

        if (regid.isEmpty()) {
                    registerInBackground();
        }

        setContentView(R.layout.activity_getnumber);
}


private String getRegistrationId(Context context) {
        final SharedPreferences prefs = getGCMPreferences(context);
        String registrationId = prefs.getString(PROPERTY_REG_ID, "");
        if (registrationId.isEmpty()) {
            Log.i("tag", "Registration not found.");
            return "";
        }
        // Check if app was updated; if so, it must clear the registration ID
        // since the existing regID is not guaranteed to work with the new
        // app version.
        int registeredVersion = prefs.getInt(PROPERTY_APP_VERSION, Integer.MIN_VALUE);
        int currentVersion = getAppVersion(getApplicationContext());
        if (registeredVersion != currentVersion) {
            Log.i("tag", "App version changed.");
            return "";
        }
        return registrationId;
    }
private SharedPreferences getGCMPreferences(Context context) {
        // This sample app persists the registration ID in shared preferences, but
        // how you store the regID in your app is up to you.
        return getPreferences(Context.MODE_PRIVATE);
    }
    private void registerInBackground() {
        new AsyncTask<Void, Void, String>() {
            @Override
            protected String doInBackground(Void... params) {
                String msg = "";
                try {
                    if (gcm == null) {
                        gcm = GoogleCloudMessaging.getInstance(getApplicationContext());
                    }

                    regid = gcm.register(SENDER_ID); // Here it returns empty string**
                    msg = "Device registered, registration ID=" + regid;

                    sendRegistrationIdToBackend();

                    // Persist the regID - no need to register again.
                    storeRegistrationId(MainActivity.this.getApplicationContext(), regid);
                } catch (IOException ex) {
                    msg = "Error :" + ex.getMessage();
                    // If there is an error, don't just keep trying to register.
                    // Require the user to click a button again, or perform
                    // exponential back-off.
                }
                return msg;
            }




        }.execute(null, null, null);

    }
    private void sendRegistrationIdToBackend() {
        // Your implementation here.
    }
    private void storeRegistrationId(Context context, String regId) {
        final SharedPreferences prefs = getGCMPreferences(context);
        int appVersion = getAppVersion(context);
        Log.i("tag", "Saving regId on app version " + appVersion);
        SharedPreferences.Editor editor = prefs.edit();
        editor.putString(PROPERTY_REG_ID, regId);
        editor.putInt(PROPERTY_APP_VERSION, appVersion);
        editor.commit();
    }

清单:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.eilutes"
    android:versionCode="7"
    android:installLocation="preferExternal"
    android:versionName="0.01d" >

    <uses-sdk
        android:minSdkVersion="9"
        android:targetSdkVersion="18" />
    <uses-permission android:name="android.permission.INTERNET"/>
    <uses-permission android:name="android.permission.READ_PHONE_STATE"/>
    <uses-permission android:name="android.permission.READ_SMS" />
    <uses-permission android:name="android.permission.WRITE_SMS" />
    <uses-permission android:name="android.permission.RECEIVE_SMS" />
    <uses-permission android:name="android.permission.SEND_SMS"/> 
    <uses-permission android:name="android.permission.READ_CONTACTS"/>
    <permission android:name="com.eilutes.permission.C2D_MESSAGE"
        android:protectionLevel="signature" />
    <uses-permission android:name="com.eilutes.permission.C2D_MESSAGE" />
    <uses-permission android:name="android.permission.VIBRATE"/>
    <uses-permission android:name="android.permission.WAKE_LOCK" />
    <uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <meta-data android:name="com.google.android.gms.version"
      android:value="@integer/google_play_services_version" />
        <activity
            android:name="com.eilutes.TermsConditionsActivity"
            android:configChanges="keyboard|keyboardHidden|orientation"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity
            android:name="com.eilutes.MainActivity"
            android:configChanges="keyboard|keyboardHidden|orientation"
            android:label="@string/app_name" >
        </activity>
        <service
            android:name=".SMSService" />
        <activity
            android:name="com.eilutes.MenuActivity"
            />
        <activity
            android:name="com.eilutes.OptionsActivity"
            />
        <activity
            android:name="com.eilutes.CallFriendsActivity"
            />
        <receiver android:name=".SMSReceiver"> 
            <intent-filter android:priority="99999999"> 
                <action android:name=
                    "android.provider.Telephony.SMS_RECEIVED" /> 

            </intent-filter> 

        </receiver>
        <receiver
            android:name="com.eilutes.GcmBroadcastReceiver"
            android:permission="com.google.android.c2dm.permission.SEND" >
            <intent-filter>
                <action android:name="com.google.android.c2dm.intent.RECEIVE" />
                <category android:name="com.eilutes" />
                <action android:name="com.google.android.c2dm.intent.REGISTRATION" />
            </intent-filter>
        </receiver>
        <service android:name=".GCMService" /> 
        <service android:name=".QueueHTTPService" /> 
    </application>

</manifest>

如果你能帮助我真的很好。感谢。

0 个答案:

没有答案