0

我得到了一个包含 GCM 服务的程序,而 GCM 服务在我的某些设备上运行良好

包含:

1)安卓模拟器(谷歌API 17 - 4.2.2)

2)LG 手机 (Android 4.1.2)

3)谷歌手机(安卓4.2.2)

但是,在以下设备上,GCM 功能似乎停止在注册状态并且 onRegistered()未被调用。

包含:

1)索尼 (Android 4.0.4)

2)HTC (Android 4.0.3)

3)三星平板电脑 (Android 3.0.3)

由于它可以在我的某些设备上运行,我很确定问题不在我的代码或 Android 清单权限设置中,但我不知道我可以添加或修改什么来解决这个问题。

我知道 4.0.4 及更低版本的 GCM 功能需要一个有效的 Google 帐户,但我尝试了它仍然无法正常工作。同时我尝试了这两个问题中建议的解决方案,但没有帮助

我无法从 Android GCM 获取注册 ID

Android GCM:GCMRegistrar 提供空的注册 ID

谁能给我一些建议如何解决这个问题?我可以通过其他方式执行服务器推送通知功能吗?

这是我的代码

    //GCM part
    cd = new ConnectionDetector(getApplicationContext());

    // Check if Internet present
    if (!cd.isConnectingToInternet()) {
        // Internet Connection is not present
        alert.showAlertDialog(this,
                "Internet Connection Error",
                "Please connect to working Internet connection", false);
        // stop executing code by return
        return;
    }

    // Getting name, email from intent
    Intent i = getIntent(); 

    // Make sure the device has the proper dependencies.
    try{
        GCMRegistrar.checkDevice(this);
        // Make sure the manifest was properly set - comment out this line
        // while developing the app, then uncomment it when it's ready.
        GCMRegistrar.checkManifest(this);
    }catch(Exception e){
        Toast.makeText(getApplicationContext(), "Version too old" + e.toString(), Toast.LENGTH_LONG).show();
    }
    registerReceiver(mHandleMessageReceiver, new IntentFilter(
            DISPLAY_MESSAGE_ACTION));

    // Get GCM registration id
    final String regId = GCMRegistrar.getRegistrationId(this);
    // Check if regid already presents
    if (regId.equals("")) {
        // Registration is not present, register now with GCM           
        try{
            GCMRegistrar.register(this, SENDER_ID);
            Toast.makeText(getApplicationContext(), "Trying to register", Toast.LENGTH_LONG).show();
        }
        catch(Exception e){
            Toast.makeText(getApplicationContext(), "Registration failure", Toast.LENGTH_LONG).show();
        }
    } else {
        // Device is already registered on GCM
        if (GCMRegistrar.isRegisteredOnServer(this)) {
            // Skips registration.              
            Toast.makeText(getApplicationContext(), "Already registered with GCM", Toast.LENGTH_LONG).show();
        } else {
            // Try to register again, but not in the UI thread.
            // It's also necessary to cancel the thread onDestroy(),
            // hence the use of AsyncTask instead of a raw thread.
            final Context context = this;
            mRegisterTask = new AsyncTask<Void, Void, Void>() {

                @Override
                protected Void doInBackground(Void... params) {
                    // Register on our server
                    // On server creates a new user
                    ServerUtilities.register(context, userId, "123", regId);
                    return null;
                }

                @Override
                protected void onPostExecute(Void result) {
                    mRegisterTask = null;
                }

            };
            mRegisterTask.execute(null, null, null);
        }
    }   

还有清单

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.test.activity"
android:versionCode="1"
android:versionName="2" >

<uses-sdk
    android:minSdkVersion="10"
    android:targetSdkVersion="17" />

<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

<!-- GCM connects to Internet Services. -->
<uses-permission android:name="android.permission.INTERNET" />

<!-- GCM requires a Google account. -->
<uses-permission android:name="android.permission.GET_ACCOUNTS" />

<!-- Keeps the processor from sleeping when a message is received. -->
<uses-permission android:name="android.permission.WAKE_LOCK" />

<!-- Creates a custom permission so only this app can receive its messages. -->
<permission
    android:name="com.test.activity.permission.C2D_MESSAGE"
    android:protectionLevel="signature" />

<uses-permission android:name="com.test.activity.C2D_MESSAGE" />

<!-- This app has permission to register and receive data message. -->
<uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />

<!-- Network State Permissions to detect Internet status -->
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />

<!-- Permission to vibrate -->
<uses-permission android:name="android.permission.VIBRATE" />

<application
.
.   
.

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

            <!-- Receives the actual messages. -->
            <action android:name="com.google.android.c2dm.intent.RECEIVE" />
            <!-- Receives the registration id. -->
            <action android:name="com.google.android.c2dm.intent.REGISTRATION" />

            <category android:name="com.test.activity" />
        </intent-filter>
    </receiver>

    <service android:name="com.test.activity.GCMIntentService" />       
</application>
</manifest>
4

1 回答 1

0

我不确定它是否与您的问题有关,但是:

<uses-permission android:name="com.test.activity.C2D_MESSAGE" />

应该改成这样:

<uses-permission android:name="com.test.activity.permission.C2D_MESSAGE" />
于 2013-08-09T13:57:47.360 回答