2016-08-01 11 views
-1

gcmを実装しています。thisを使用してメッセージが表示されますが、デバイスにプッシュ通知が表示されません。私は正確な問題がどこにあるのか、何故通知を受けないのかわからない。以下は私のプロジェクトで実装したコードですので、このような問題を整理してください。 ここに私の目録があります。ここでAndroid GCMプッシュ通知が受信されない

<uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" /> 
<uses-permission android:name="android.permission.WAKE_LOCK" /> 

<uses-permission android:name="com.google.android.c2dm.permission.RECEIVE"/> 
<permission 
    android:name="com.example.android.pushnotificationdemo2.permission.C2D_MESSAGE" 
    android:protectionLevel="signature"/> 


<application 
    android:allowBackup="true" 
    android:icon="@mipmap/ic_launcher" 
    android:label="@string/app_name" 
    android:theme="@style/AppTheme" > 

    <activity 
     android:name="com.example.android.pushnotificationdemo2.MainActivity" 
     android:label="@string/app_name" > 
     <intent-filter> 
      <action android:name="android.intent.action.MAIN" /> 

      <category android:name="android.intent.category.LAUNCHER" /> 
     </intent-filter> 
    </activity> 

    <receiver 
     android:name="com.google.android.gms.gcm.GcmReceiver" 
     android:exported="true" 
     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" /> 
      <action android:name="com.google.android.c2dm.intent.GCM_RECEIVED_ACTION" /> 

      <category android:name="gcm.play.android.samples.com.gcmquickstart" /> 
     </intent-filter> 
    </receiver> 

    <service 
     android:name="com.example.android.pushnotificationdemo2.MyGcmListenerService" 
     android:exported="false" > 
     <intent-filter> 
      <action android:name="com.google.android.c2dm.intent.RECEIVE" /> 
      <action android:name="com.google.android.c2dm.intent.GCM_RECEIVED_ACTION"/> 
     </intent-filter> 
    </service> 

    <service 
     android:name="com.example.android.pushnotificationdemo2.MyInstanceIDListenerService" 
     android:exported="false"> 
     <intent-filter> 
      <action android:name="com.google.android.gms.iid.InstanceID"/> 
     </intent-filter> 
    </service> 

    <service 
     android:name="com.example.android.pushnotificationdemo2.RegistrationIntentService" 
     android:exported="false"> 
    </service> 

    <meta-data 
     android:name="com.google.android.gms.version" 
     android:value="@integer/google_play_services_version" /> 

</application> 

私の活動です。

public class MainActivity extends Activity { 

    private static final int PLAY_SERVICES_RESOLUTION_REQUEST = 9000; 
    private static final String TAG = "MainActivity"; 

    private BroadcastReceiver mRegistrationBroadcastReceiver; 
    private ProgressBar mRegistrationProgressBar; 
    private TextView mInformationTextView; 
    private boolean isReceiverRegistered; 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 
     mRegistrationProgressBar = (ProgressBar) findViewById(R.id.registrationProgressBar); 

     mRegistrationBroadcastReceiver = new BroadcastReceiver() { 
      @Override 
      public void onReceive(Context context, Intent intent) { 

       Log.e("On Received ","?????? "); 

       mRegistrationProgressBar.setVisibility(ProgressBar.GONE); 

       SharedPreferences sharedPreferences =PreferenceManager.getDefaultSharedPreferences(context); 
       boolean sentToken = sharedPreferences.getBoolean(QuickstartPreferences.SENT_TOKEN_TO_SERVER, false); 

       if (sentToken) { 
        mInformationTextView.setText(getString(R.string.gcm_send_message)); 
       } 
       else { 
        mInformationTextView.setText(getString(R.string.token_error_message)); 
       } 
      } 
     }; 
     mInformationTextView = (TextView) findViewById(R.id.informationTextView); 

     // Registering BroadcastReceiver 
     registerReceiver(); 

     if (checkPlayServices()) { 
      Intent intent = new Intent(this, RegistrationIntentService.class); 
      startService(intent); 
     } 

    } 

    @Override 
    protected void onResume() { 
     super.onResume(); 
     registerReceiver(); 
    } 

    @Override 
    protected void onPause() { 
     LocalBroadcastManager.getInstance(this).unregisterReceiver(mRegistrationBroadcastReceiver); 
     isReceiverRegistered = false; 
     super.onPause(); 
    } 

    private void registerReceiver() { 
     if (!isReceiverRegistered) { 
      LocalBroadcastManager.getInstance(this).registerReceiver(mRegistrationBroadcastReceiver, 
        new IntentFilter(QuickstartPreferences.REGISTRATION_COMPLETE)); 
      isReceiverRegistered = true; 
     } 
    } 

    private boolean checkPlayServices() { 
     GoogleApiAvailability apiAvailability = GoogleApiAvailability.getInstance(); 
     int resultCode = apiAvailability.isGooglePlayServicesAvailable(this); 
     if (resultCode != ConnectionResult.SUCCESS) { 
      if (apiAvailability.isUserResolvableError(resultCode)) { 
       apiAvailability.getErrorDialog(this, resultCode, PLAY_SERVICES_RESOLUTION_REQUEST).show(); 
      } 
      else { 
       Log.i("  ", "This device is not supported."); 
       finish(); 
      } 
      return false; 
     } 
     return true; 
    } 
} 

、ここMyGcmListenerService

public class MyGcmListenerService extends GcmListenerService { 

    private static final String TAG = "MyGcmListenerService"; 

    @Override 
    public void onMessageReceived(String from, Bundle data) { 

     Log.e("onMessageReceived ","@@@@@@@@@@@@"); 

     String message = data.getString("message"); 
     Log.d(TAG, "From: " + from); 
     Log.d(TAG, "message: " + message); 

     if (from.startsWith("/topics/")) { 
      // message received from some topic. 
     } else { 
      // normal downstream message. 
     } 

     sendNotification(message); 

    } 

    private void sendNotification(String message) { 

     Intent intent = new Intent(MyGcmListenerService.this, MainActivity.class); 

     intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); 
     PendingIntent pendingIntent = PendingIntent.getActivity(this, 0 /* Request code */, intent, 
       PendingIntent.FLAG_ONE_SHOT); 

     Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION); 

     NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this) 
       .setSmallIcon(R.mipmap.ic_launcher) 
       .setContentTitle("GCM Message") 
       .setContentText(message) 
       .setAutoCancel(true) 
       .setSound(defaultSoundUri) 
       .setContentIntent(pendingIntent); 

     NotificationManager notificationManager =(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); 

     notificationManager.notify(0 /* ID of notification */, notificationBuilder.build()); 

    } 
} 
+0

は、お使いのデバイスは、GCMの上に登録されなくてもよいです、デバイスIDをデバッグして確認してください。そうでなければ、別のデバイスをチェックしてください。 –

+0

[gcmメッセージを受信できません]の複製があります(http://stackoverflow.com/questions/33890426/can-not-receive-gcm-message) –

答えて

0

である私は、インターネットのアクセス権がちょうどチェック@Sakibサイード

+0

はコメントです。 –

+1

コメントするには十分な評判がありません –

+0

@Mujammil Ahmedあなたが答えても、私のデバイスには何の通知も受けていません。 –

0

が欠けていると思うあなたのキーが有効か無効です。そうでなければそれを再生成して試してみてください。

+0

はコメント –

0

マニフェストファイルに以下の権限を追加します。

は、Androidの許可使用しています:名=「YOUR_PACKAGE_NAME.permission.C2D_MESSAGE」

+0

に追加済みですこれは私の目に見えますが、まだ私の問題は解決されていません。 –

0

あなたがマニフェストに二回この行を持って、1を削除します。

uses-permission android:name="com.google.android.c2dm.permission.RECEIVE"/> 
関連する問題