自定义GCM广播接收器不在几个设备上使用,但在其他设备上使用

时间:2013-02-13 13:19:40

标签: java android google-cloud-messaging

我正在创建一个使用GCM接收来自我们服务器的通知的应用,我正在使用Google的gcm.jar,并且有一个customGCMIntentService类来处理它。我的问题是,在调用GCMRegistrar.register()时,只有一部手机实际上正在从GCM获得响应(或更有可能正确地路由响应)。

我所看到的是GCMRegistrar(来自gcm库)正在将我的自定义广播接收器设置为nexus 3手机上的重试接收器,这是唯一有效的,但不能在其他手机上使用。这让我相信我可能会收到GCM的回复,但是没有在其他三部手机上处理(详情如下)。

GCMRegistrar Setting the name of retry receiver class to <My_application_package>.CustomGCMBroadcastReceiver

所有这些都有一个允许与GCM通信的数据连接,所有这些都有一个活跃的gmail帐户(所有这些都有playstore工作)。我也在没有任何问题的情况下运行谷歌的GCM演示。

注册码

 //This is almost identical to how Google's GCM demo does it.
 private void registerOnGCM(){
    checkNotNull(SERVER_URL, "SERVER_URL"); //What it says on the tin
    checkNotNull(SENDER_ID, "SENDER_ID");
    // Make sure the device has the proper dependencies.
    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);
    /*
    registerReceiver(mHandleMessageReceiver,
            new IntentFilter(HANDLE_MESSAGE));
    */
    final String regId = GCMRegistrar.getRegistrationId(getApplicationContext());
    if (regId.equals("")) {
        // Automatically registers application on startup.
        GCMRegistrar.register(getApplicationContext(), SENDER_ID);
    } 
    else {
        // Device is already registered on GCM, check server.
        if (GCMRegistrar.isRegisteredOnServer(getApplicationContext())) {
            // Skips registration.
            Log.i(TAG, "Already registered");
        } 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) {
                boolean registered = ServerUtilities.register(context, regId);

                if (!registered) {
                    GCMRegistrar.unregister(context);
                }
                return null;
            }

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

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

CustomGCMBroadcastReceiver的代码无关紧要,GCMIntentService也没关系,因为它们都没有被调用过。问题不在他们身上。

我正在测试的设备

  • HTC Desire GSM运行CyanogenMod 7
  • 谷歌nexus 3运行Android 4.1.1(应用程序适用于此)
  • 摩托罗拉T910运行Android 4.0.1
  • 三星Galaxy mini运行Android 4.2.6

清单

<uses-sdk  android:minSdkVersion="8" android:targetSdkVersion="16" />
<uses-permission android:name="android.permission.INTERNET" />

<!-- App has permission to read/write files on sd card. Used for RSS document -->
<uses-permission android:name = "android.permission.READ_EXTERNAL_STORAGE"/>
<uses-permission android:name = "android.permission.WRITE_EXTERNAL_STORAGE"/>

<!-- GCM connects to Google 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" />

<permission
    android:name="<My_package_name>.permission.C2D_MESSAGE"
    android:protectionLevel="signature" />
<uses-permission
    android:name="<My_package_name>.permission.C2D_MESSAGE" />

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



<application
    android:allowBackup="true"
    android:theme="@style/AppTheme"
    android:label="@string/app_name">
    <activity
        android:name="<My_package_name>.MasterActivity"
        android:screenOrientation="portrait">
    </activity>

    <activity   android:name="<My_package_name>.WebActivity">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>

    <!--
      BroadcastReceiver that will receive intents from GCM
      services and handle them to the custom IntentService.

      The com.google.android.c2dm.permission.SEND permission is necessary
      so only GCM services can send data messages for the app.
    -->
    <receiver
        android:name="<My_package_name>.CustomGCMBroadcastReceiver"
        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="<My_package_name>" />
        </intent-filter>
    </receiver>

    <!--
      Application-specific subclass of GCMBaseIntentService that will
      handle received messages.

      By default, it must be named .GCMIntentService, unless the
      application uses a custom BroadcastReceiver that redefines its name.
    -->
    <service    android:name="<My_package_name>.GCMIntentService"
                android:enabled="true"/>
 </application>

可能是我忘记添加的东西。让我知道。

欢迎所有想法。

-MrDresden

1 个答案:

答案 0 :(得分:1)

好吧,它似乎只是通过清理,重建然后将其部署到其他设备来修复它。

为什么从所有手机中删除所有迹线,然后在它们上以调试模式运行不起作用,除了在一部手机上我不知道。

相关问题