2015-02-24 39 views
-1

我們已經實現GCM在android應用程序中顯示通知,但只有一些消息沒有收到服務器發送到谷歌的設備?服務器正在發送沒有collapse_key的消息,因此每條消息都應該到達設備而不會摺疊。請建議我繼續下一步。服務器向谷歌發送的某些設備中沒有收到GCM消息?

感謝

接收器代碼如下:

public class GcmBroadcastReceiver extends WakefulBroadcastReceiver { 

    @Override 
    public void onReceive(Context context, Intent intent) { 
     // Explicitly specify that GcmIntentService will handle the intent. 
     ComponentName comp = new ComponentName(context.getPackageName(), 
       GcmIntentService.class.getName()); 
     // Start the service, keeping the device awake while it is launching. 
     startWakefulService(context, (intent.setComponent(comp))); 
     setResultCode(Activity.RESULT_OK); 
    } 
} 

服務代碼如下:

public class GcmIntentService extends IntentService { 

    NotificationCompat.Builder builder; 

    public GcmIntentService() { 
     super("GcmIntentService"); 
    } 
    public static final String TAG = "GCM Demo"; 

    @Override 
    protected void onHandleIntent(Intent intent) { 
     Bundle extras = intent.getExtras(); 
     GoogleCloudMessaging gcm = GoogleCloudMessaging.getInstance(this); 
     // The getMessageType() intent parameter must be the intent you received 
     // in your BroadcastReceiver. 
     String messageType = gcm.getMessageType(intent); 

     if (!extras.isEmpty()) { // has effect of unparcelling Bundle 
      /* 
      * Filter messages based on message type. Since it is likely that GCM will be 
      * extended in the future with new message types, just ignore any message types you're 
      * not interested in, or that you don't recognize. 
      */ 
      if (GoogleCloudMessaging.MESSAGE_TYPE_SEND_ERROR.equals(messageType)) { 
       //sendNotification("Send error: " + extras.toString()); 
      } else if (GoogleCloudMessaging.MESSAGE_TYPE_DELETED.equals(messageType)) { 
       //sendNotification("Deleted messages on server: " + extras.toString()); 
      // If it's a regular GCM message, do some work. 
      } else if (GoogleCloudMessaging.MESSAGE_TYPE_MESSAGE.equals(messageType)) { 
       // This loop represents the service doing some work. 
       for (int i = 0; i < 5; i++) { 
        Log.i(TAG, "Working... " + (i + 1) 
          + "/5 @ " + SystemClock.elapsedRealtime()); 
        try { 
         Thread.sleep(5000); 
        } catch (InterruptedException e) { 
        } 
       } 
       generateNotification(this, name, id, message); 
      } 
     } 
     // Release the wake lock provided by the WakefulBroadcastReceiver. 
     GcmBroadcastReceiver.completeWakefulIntent(intent); 
    } 
} 

清單文件低於

<permission 
    android:name="MY_PACKAGE_NAME.permission.C2D_MESSAGE" 
    android:protectionLevel="signature" /> 

<uses-permission android:name="MY_PACKAGE_NAME.permission.C2D_MESSAGE" /> 
<uses-permission android:name="android.permission.GET_ACCOUNTS" /> 
<uses-permission android:name="android.permission.WAKE_LOCK" /> 
<uses-permission android:name="com.google.android.c2dm.permission.RECEIVE"/> 
<receiver android:name=".GcmBroadcastReceiver" 
     android:permission="com.google.android.c2dm.permission.SEND" > 
    <intent-filter> 
     <action android:name="com.google.android.c2dm.intent.RECEIVE" /> 
     <category android:name="MY_PACKAGE_NAME" /> 
    </intent-filter> 
</receiver> 

<service android:name=".GcmIntentService" /> 
+0

提供更多信息。這是模糊的。 – drulabs 2015-02-24 13:48:28

+0

我已經提供了我上面的代碼的更多信息。 – user3588330 2015-02-24 14:37:26

+0

由於您收到了一些消息,我相信您已完成註冊,獲取設備的gcm註冊ID,並且您正從服務器或本地主機發送gcm。只有一件事還包括您的服務器的http響應 – drulabs 2015-02-24 17:41:42

回答

0

你需要創建一個谷歌在這個帳戶設備(設置 - >帳戶 - > Google)。所以請檢查它是否與谷歌登錄或沒有:)

+0

是的,gmail帳戶在所有設備中都處於活動狀態,並且還能夠訪問Play商店。 – user3588330 2015-02-24 13:34:43

相關問題