我创建了一个应用程序,它使用GCM从我们的服务器接收通知,使用Google的gcm.jar,并且有一个customGCMIntentService类来处理它。我的问题是,拨打GCMRegistrar.register()
时,只有一部手机实际上从GCM获得响应(或更有可能正确路由响应)。自定义GCM广播接收器没有在几个设备上使用,但在其他设备上使用
我所看到的是GCMRegistrar(来自gcm库)正确地将我的自定义广播接收器设置为nexus 3手机上的重试接收器,唯一可用的,但不是其他的。这让我相信我可能会从GCM获得回应,但是它并未在其他三部手机上处理(详情如下)。
GCMRegistrar Setting the name of retry receiver class to <My_application_package>.CustomGCMBroadcastReceiver
所有这些具有数据连接的允许与GCM通信,都对它们的活性的Gmail帐户(它们都具有Play商店中工作)。我也运行过Google的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
大胆猜测。 – Hrafn 2013-02-13 16:39:19
@Qkyrie'<','>'是包名中的无效字符。 – wtsang02 2013-02-13 23:15:54