8

Androidデバイスは2.3デバイスではGCMメッセージを受信しませんが、4.xデバイスでは受信しません。すべてのデバイス(2.3と4.x)を正常に登録できます。私はそれがthis issueと何か関係があるかもしれないと思ったが、それは私のアンドロイドマニフェストが正しく設定されているようだ。誰でも私のIntentServiceとBroadcastReceiverに目を通して問題が発生したかどうか確認できますか?どんな助けでも大歓迎です。デバッガが接続されている間、onHandeIntent()は通知を送信するときにAndroid 2.3で呼び出されないことに注意してください。私は4.xデバイスをチェックし、onHandleIntent()でデバッガを起動します。 ありがとう!Android 4.xデバイスはGCMメッセージを受信しますが、Android 2.3デバイスは受信しません

アンドロイドManfest:

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

    <uses-sdk android:minSdkVersion="9" android:targetSdkVersion="17" /> 
    <uses-permission android:name="android.permission.INTERNET" /> 
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /> 
    <uses-permission android:name="android.permission.GET_ACCOUNTS" /> 
    <uses-permission android:name="android.permission.WAKE_LOCK" /> 
    <uses-permission android:name="android.permission.VIBRATE" /> 
    <uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" /> 
    <uses-permission android:name="my.package.matchtracker.permission.C2D_MESSAGE" /> 
    <permission android:name="my.package.permission.C2D_MESSAGE" 
     android:protectionLevel="signature" /> 
    <application 
     android:icon="@drawable/ic_launcher" 
     android:label="@string/app_name" > 
     <receiver 
      android:name=".GcmBroadcastReceiver" 
      android:permission="com.google.android.c2dm.permission.SEND" > 
      <intent-filter> 
       <action android:name="com.google.android.c2dm.intent.RECEIVE" /> 
       <action android:name="com.google.android.c2dm.intent.REGISTRATION" /> 
       <category android:name="my.package" /> 
      </intent-filter> 
     </receiver> 
     <service android:name=".NotificationIntentService" android:enabled="true" /> 
     <activity android:name="com.gigya.socialize.android.GSWebViewActivity" /> 
     <activity 
      android:name=".MainActivity" 
      android:label="@string/app_name" 
      android:configChanges="orientation|screenSize" 
      android:theme="@android:style/Theme.NoTitleBar"> 
      <intent-filter> 
       <action android:name="android.intent.action.MAIN" /> 
       <category android:name="android.intent.category.LAUNCHER" /> 
      </intent-filter> 
     </activity> 
    </application> 
</manifest> 

放送受信機:

package my.package; 

import android.app.*; 
import android.content.*; 

public class GcmBroadcastReceiver extends BroadcastReceiver { 
    @Override 
    public void onReceive(Context context, Intent intent) { 
     NotificationIntentService.runIntentInService(context, intent); 
     setResultCode(Activity.RESULT_OK); 
    } 
} 

通知意向サービス

public class NotificationIntentService extends IntentService { 
    private String TAG = "NotificationIntentService"; 
    public NotificationIntentService() { 
     super(AppConstants.GCM_SENDER_ID); 
    } 

    public NotificationIntentService(String name) { 
     super(name); 
     // TODO Auto-generated constructor stub 
    } 

    private static PowerManager.WakeLock sWakeLock; 
    private static final Object LOCK = NotificationIntentService.class; 

    static void runIntentInService(Context context, Intent intent) { 
     synchronized(LOCK) { 
      if (sWakeLock == null) { 
       PowerManager pm = (PowerManager) context.getSystemService(Context.POWER_SERVICE); 
       sWakeLock = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "my_wakelock"); 
      } 
     } 
     sWakeLock.acquire(); 
     intent.setClassName(context, NotificationIntentService.class.getName()); 
     context.startService(intent); 
    } 

    public final void onHandleIntent(Intent intent) { 
     try { 
      String action = intent.getAction(); 
      if (action.equals("com.google.android.c2dm.intent.REGISTRATION")) { 
       //don't care. 
      } else if (action.equals("com.google.android.c2dm.intent.RECEIVE")) { 
       handleMessage(intent); 
      } 
     } finally { 
      synchronized(LOCK) { 
       sWakeLock.release(); 
      } 
     } 
    } 

    private void handleMessage(Intent intent) { 
     Bundle b = intent.getExtras(); 
     String text = b.getString("text"), 
       title = b.getString("title"), 
       largeImageUrl = b.getString("largeImageUrl"); 
     Log.i(TAG, "Message is " + text); 
     NotificationManager nm = (NotificationManager) this.getSystemService(Context.NOTIFICATION_SERVICE); 
     Bitmap bit=null; 
     if (largeImageUrl != null && !largeImageUrl.isEmpty()) { 
      try{bit = BitmapFactory.decodeStream((InputStream)new URL(largeImageUrl).getContent()); 
      } catch (Exception e){} 
     } 
     NotificationCompat.Builder nc = new NotificationCompat.Builder(this) 
              .setContentTitle(title) 
              .setContentText(text) 
              .setSmallIcon(R.drawable.ic_launcher) 
              .setSound(RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION)) 
              .setAutoCancel(true) //notification disappears when clicked 
              .setContentIntent(PendingIntent.getActivity(this, 0, 
                new Intent(this, MainActivity.class), PendingIntent.FLAG_UPDATE_CURRENT)); 
     //bit = Bitmap.createScaledBitmap(bit, android.R.dimen.notification_large_icon_width, android.R.dimen.notification_large_icon_height, true); 
     if (bit != null) nc.setLargeIcon(bit); 
     nm.notify(0, nc.build()); 
    } 
} 

答えて

16

私が見ることができる最初の潜在的な問題は次のとおりです。

permission要素のパッケージ名は、uses-permission要素のパッケージ名と同じではありません。私のアプリ(Android 2.2をターゲットとする)では、それらは同じです。

<uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" /> 
<uses-permission android:name="my.package.matchtracker.permission.C2D_MESSAGE" /> 
<permission android:name="my.package.permission.C2D_MESSAGE" 
    android:protectionLevel="signature" /> 
+0

はい、そうです。私は何とかマッチトラッカーがこの2番目の回答から必要な設定の一部であると思っていました[post](http://stackoverflow.com/questions/5660460/push-notifications-on-emulator-working-but-not-on-real -device#answer-10723301)。今私はよく知っている。これを手伝ってくれてありがとう! – Daniel

+0

よろしくお願いします! – Eran

+1

@EranほとんどのGCM質問にコメントしていますが、それはGCMについて非常によく理解している必要があるという結論に至ります。ですから、私はGCMについての情報を私に提供するようお願いします。私はAndroidHiveチュートリアルをやったが、多くの人が直面しているように、**私はあまりにも**サーバー上の登録に成功したにもかかわらず、デバイス**の通知を受け取ることができません。それ以上の記事を読む私はAndroidHiveチュートリアルと比較してGCMのアップデートがいくつかあると推測していますので、更新情報とそのチュートリアルの変更内容をお知らせください。 –

0

違うAPPLICATIONIDと製品の風味を使用している場合は、私の質問GCMメッセージにanswerは、Android 2.3.6 Vで受信したが、アンドロイド4.X vに正常に動作していない参照

0

GCMデバイスにGoogle Playサービスがインストールされている必要がありますが、バージョン2.3ではデフォルトではインストールされません。

関連する問題