Android - Device ID from GCM return empty

送分小仙女□ 提交于 2019-12-14 04:01:26

问题


I have to get only device id from GCM for further use. I try all but result is negative, Device id I get is empty. Please help

Here is code:

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 results :

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

Here is code for 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);
}

}

Here is Mainfest.xml code:

<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>

回答1:


GCMRegister is deprecated use Google Cloud Messaging refer this link https://developers.google.com/cloud-messaging/android/start.




回答2:


try to put all files in main package and use mobile data for internet. Sometime wifi not works.

Hope this helps you.




回答3:


Put your GCM code in application package




回答4:


Put your GCM code in your main package(e.g "com.sample" in this case) of application that is defined in manifest:

package="com.sample"



来源:https://stackoverflow.com/questions/34508599/android-device-id-from-gcm-return-empty

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