2016-08-01 3 views
-1

this을 사용하여 gcm을 구현 중이며 메시지가 표시됩니다. 내 기기에서 푸시 알림이 표시되지 않습니다. 나는 정확한 문제가 어디에 있는지, 왜 어떤 종류의 통지를받지 못하는지 잘 모르겠습니다. 아래는 내 프로젝트에서 구현 한 코드입니다. 그런 문제를 해결하려면 저를 압니다. 내 매니페스트가 여기 있습니다. 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

매니페스트 파일에 아래 권한을 추가하십시오.

안드로이드-권한 사용 : 이름 = "YOUR_PACKAGE_NAME.permission.C2D_MESSAGE"

+0

이미 추가했습니다. 내 명단에 있지만 여전히 내 문제가 해결되지 않았습니다. –

0

을 당신은 당신의 매니페스트에 두 번이 라인을 가지고 하나를 제거 :

uses-permission android:name="com.google.android.c2dm.permission.RECEIVE"/> 
관련 문제