我有一个包含GCM服务的程序,GCM服务在我的某些设备上表现完美
,包括:
1)android模拟器(Google API 17 - 4.2.2)
2)LG手机(Android 4.1.2)
3)谷歌手机(Android 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帐户,但我尝试了它并且它仍然无法正常工作。 同时我尝试了这两个问题中提出的解决方案,但没有帮助
I can not get registration ID from Android GCM
Android GCM : GCMRegistrar gives empty registration 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);
}
}
还有Manifest
<?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>
答案 0 :(得分:0)
我不确定它是否与您的问题有关,但是这个:
<uses-permission android:name="com.test.activity.C2D_MESSAGE" />
应改为:
<uses-permission android:name="com.test.activity.permission.C2D_MESSAGE" />