Android - 来自GCM的设备ID返回空

时间:2015-12-29 09:55:54

标签: android google-cloud-messaging

我必须从GCM获取设备ID以供进一步使用。我尝试了所有但结果是否定的,我得到的设备ID是空的。请帮忙

这是代码:

GCMRegistrar.checkDevice(this);
    GCMRegistrar.checkManifest(this);

    setContentView(R.layout.cmain);
    mDisplay = (TextView) findViewById(R.id.display);

    final String regId = GCMRegistrar.getRegistrationId(this);
    Log.i(TAG, "registration id =====  " + regId);

    if (regId.equals("")) {
        GCMRegistrar.register(this, CommonUtilities.SENDER_ID);
    } else {
        Log.v(TAG, "Already registered");
    }

Logcat结果:

I/ pushAndroidActivity (2295): registration id =====  
D/GCMRegistrar(2295): resetting backoff for shane.nuke
V/GCMRegistrar(2295): Registering app shane.nuke of senders 510772122772
V/GCMBroadcastReceiver(2295): onReceive: com.google.android.c2dm.intent.REGISTRATION
V/GCMBroadcastReceiver(2295): GCM IntentService class: shane.nuke.GCMIntentService
V/GCMBaseIntentService(2295): Acquiring wakelock

以下是GCMIntentService_2的代码

public class GCMIntentService_2 extends GCMBaseIntentService {

public GCMIntentService_2() {
    super(CommonUtilities.SENDER_ID);
}
private static final String TAG = "===GCMIntentService===";
@Override
protected void onRegistered(Context arg0, String registrationId) {
    Log.i(TAG, "Device registered: regId = " + registrationId);
    Log.e(TAG, "Device registered: regId = " + registrationId);
}
@Override
protected void onUnregistered(Context arg0, String arg1) {
    Log.i(TAG, "unregistered = " + arg1);
}
@Override
protected void onMessage(Context arg0, Intent arg1) {
    Log.i(TAG, "new message= ");
}
@Override
protected void onError(Context arg0, String errorId) {
    Log.i(TAG, "Received error: " + errorId);
}
@Override
protected boolean onRecoverableError(Context context, String errorId) {
    return super.onRecoverableError(context, errorId);
}

}

这是Mainfest.xml代码:

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="shane.nuke"
android:versionCode="1"
android:versionName="1.0" >

<uses-sdk
    android:minSdkVersion="12"
    android:targetSdkVersion="23" />

<permission
    android:name="shane.nuke.permission.C2D_MESSAGE"
    android:protectionLevel="signature" />

<uses-permission android:name="shane.nuke.permission.C2D_MESSAGE" />
<uses-permission android:name="android.permission.GET_ACCOUNTS" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="android.permission.INTERNET" />
<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" >
    <activity
        android:name="shane.nuke.second.Push"
        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="shane.nuke.next.GCMMessageView" >
    </activity>

    <receiver
        android:name="com.google.android.gcm.GCMBroadcastReceiver"
        android:permission="com.google.android.c2dm.permission.SEND" >
        <intent-filter>
            <action android:name="com.google.android.c2dm.intent.RECEIVE" />
            <action android:name="com.google.android.c2dm.intent.REGISTRATION" />

            <category android:name="shane.nuke" />
        </intent-filter>
    </receiver>


    <service android:name="shane.nuke.second.GCMIntentService_2" />

    <meta-data
        android:name="com.google.android.gms.version"
        android:value="@integer/google_play_services_version" />
</application>

4 个答案:

答案 0 :(得分:1)

GCMRegister已弃用,Google Cloud Messaging请参阅此链接https://developers.google.com/cloud-messaging/android/start

答案 1 :(得分:1)

尝试将所有文​​件放在主包中并使用移动数据进行互联网。有时wifi不起作用。

希望这会对你有所帮助。

答案 2 :(得分:0)

将GCM代码放入应用程序包

答案 3 :(得分:0)

将您的GCM代码放入主要包中(例如,#34; com.sample&#34;在这种情况下)在清单中定义的应用程序:

包=&#34; com.sample&#34;