2016-08-01 158 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艾哈邁德我已經做了你的答案,但我仍然沒有收到我的設備的任何通知。 –

0

只是檢查是你的關鍵是有效還是無效。如果不是,那麼重新生成它並嘗試它。

+0

應該是評論 –

0

在Manifiest文件中添加以下權限。

使用許可權的android:NAME = 「YOUR_PACKAGE_NAME.permission.C2D_MESSAGE」

+0

我已經添加這在我的清單,但仍然是我的問題尚未解決。 –

0

您在您的清單中有這條線的兩倍,刪除一個:

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