GCM サービスを含むプログラムを入手しましたが、GCM サービスは一部のデバイスで完全に動作します
含む:
1)Android エミュレーター (Google API 17 - 4.2.2)
2)LG Phone (Android 4.1.2)
3) Google フォン (Android 4.2.2)
ただし、以下のデバイスでは、GCM関数が登録状態で停止し、
onRegistered()
呼び出されていないようです。
含む:
1) ソニー (Android 4.0.4)
2)HTC (アンドロイド 4.0.3)
3)SAMSUNG タブレット (Android 3.0.3)
一部のデバイスで実行できるため、コードや Android マニフェストのアクセス許可設定に問題があるわけではないと確信していますが、これを解決するために何を追加または変更すればよいかわかりません。
バージョン 4.0.4 以前の GCM 機能にはアクティブな Google アカウントが必要であることは知っていましたが、試してみましたが、まだ機能していません。その間、これら2つの質問で提案された解決策を試しましたが、役に立ちませんでした
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>