2014-04-08 92 views
1

我在應用中正在實施GCM,我正在成功註冊設備ID,但是當我從服務器發送它時,我沒有收到任何通知。 這裏是我的代碼部分: 清單未接收到GCM通知

<receiver 
     android:name=".services.GcmBroadcastReceiver" 
     android:permission="com.google.android.c2dm.permission.SEND"> 
     <intent-filter> 
      <action android:name="com.google.android.c2dm.intent.RECEIVE"/> 
      <category android:name="com.test.gcm"/> 
     </intent-filter> 
    </receiver> 
    <service android:name=".services.GcmIntentService"/> 

和封裝服務裏面的GcmBroadcastReceiver:

public class GcmBroadcastReceiver extends WakefulBroadcastReceiver { 
    @Override 
    public void onReceive(Context context, Intent intent) { 
     ComponentName component = new ComponentName(context.getPackageName(), 
       GcmIntentService.class.getName()); 

     startWakefulService(context, (intent.setComponent(component))); 
     setResultCode(Activity.RESULT_OK); 
    } 
} 

最後gcmIntentService

public class GcmIntentService extends IntentService { 

    public static final int NOTIFICATION_ID = 1; 
    private NotificationManager mNotificationManager; 

    public GcmIntentService() { 
     super("GcmIntentService"); 
    } 

    @Override 
    protected void onHandleIntent(Intent intent) { 
     Bundle extras = intent.getExtras(); 
     GoogleCloudMessaging gcm = GoogleCloudMessaging.getInstance(this); 
     String messageType = gcm.getMessageType(intent); 

     if (!extras.isEmpty()) { 
      sendNotification("Notification " + extras.toString()); 
     } 

     GcmBroadcastReceiver.completeWakefulIntent(intent); 
    } 

    private void sendNotification(String message) { 
     mNotificationManager = (NotificationManager) 
       this.getSystemService(Context.NOTIFICATION_SERVICE); 

     PendingIntent contentIntent = PendingIntent.getActivity(this, 0, 
       new Intent(this, MainActivity.class), 0); 

     NotificationCompat.Builder mBuilder = 
       new NotificationCompat.Builder(this) 
         .setContentTitle("Notification") 
         .setContentText(message) 
         .setStyle(new NotificationCompat.BigTextStyle()) 
         .setAutoCancel(true); 

     mBuilder.setContentIntent(contentIntent); 
     mNotificationManager.notify(NOTIFICATION_ID, mBuilder.build()); 
    } 
} 

我不知道什麼可能是錯誤的,我甚至不知道如何調試它。 感謝您的幫助

+0

的第一步是檢查服務器上的響應,當你發送消息。你需要看到'成功'而不是'失敗' – NickT

回答

0

說到GCM,它非常關注服務和接收者的名稱。 接收者的名稱應該是

<receiver 
     android:name="com.google.android.gcm.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="com.packagename" /> 
     </intent-filter> 
    </receiver> 

和服務應該是

<service android:name=".GCMIntentService" /> 

此外,GCMIntentService應該在同一個包,你的啓動程序類。

0

的問題是要嘗試啓動服務的方式:

public class GcmBroadcastReceiver extends WakefulBroadcastReceiver { 
    @Override 
    public void onReceive(Context context, Intent intent) { 
     ComponentName component = new ComponentName(context.getPackageName(), 
       GcmIntentService.class.getName()); 

     startWakefulService(context, (intent.setComponent(component))); 
     setResultCode(Activity.RESULT_OK); 
    } 
} 

您初始化context.getPackageName()一個ComponentName這是您的應用程序(com.test.gcm)的主包,但你GcmIntentService類位於不同包(com.test.gcm.services)。

您可以指定正確的軟件包名稱:

ComponentName component = new ComponentName(
        GcmIntentService.class.getPackage().getName(), 
        GcmIntentService.class.getName()); 

或者你可以用不同的方式啓動服務:

Intent gcmIntent = new Intent(context, GcmIntentService.class); 
gcmIntent.putExtras(intent.getExtras()); 
startWakefulService(context, gcmIntent);