2015-05-27 68 views
55

我得到我的推送服務捕獲一個奇怪的推送消息:奇怪的推送消息開始

Bundle[{CMD=RST_FULL, from=google.com/iid, android.support.content.wakelockid=1}] 

剛開始昨天發生,我真的不能發現它的代碼改變是罪魁禍首這個。有沒有人看過此消息,也許知道它來自哪裏,爲什麼?

+0

能否請您詳細說明......這是您收到的日誌消息嗎?你在使用一些第三方API或庫嗎? – AniV

+0

上面的Bundle是一個來自調試器的複製/粘貼 - 它是我用接收器捕捉的意圖中的一個對象。我用'intent.getExtras()'得到它,期望類型和消息,而不是上面的地圖。我正在使用一些第三方庫,如畢加索和Okhttp,但沒有什麼太晦澀或在這方面。除谷歌之外不使用任何第三方API(分析,存儲,gcm) – vkislicins

+0

另外,似乎我只在應用程序的第一次運行時收到此消息。強制停止應用程序並清除數據不會使該消息重新出現 – vkislicins

回答

20

您的應用得到這個消息,因爲它已經有數據r從備份存儲。由於備份可能包含註冊令牌,因此會發送此廣播消息,告訴您的應用獲取新令牌,因爲備份的令牌不起作用。

這是針對新的GCM APIs,它會導致您的InstanceIdListenerService實現的onTokenRefresh()方法被調用,其中您的應用程序應該再次獲取其所有的令牌。

不幸的是,如果你正在編寫你自己的BroadcastReceiver,這些消息將是意想不到的,並可能導致你的應用崩潰。正確的做法是過濾「from」字段,如果看到其中一條消息,請再次註冊GCM,因爲您的令牌可能無效。

如果您在全新的安裝環境之外獲取這些消息,您的應用程序的數據正在恢復,請發送到android-gcm郵件列表。

+0

你的答案的備份部分不清楚。爲什麼會出現這樣的通知,即使應用程序明確表示通過將allowBackup設置爲false來禁止備份? –

+0

@GiulioPiancastelli根據[GCM示例代碼]中的評論(https://github.com/googlesamples/google-services/blob/master/android/gcm/app/src/main/java/gcm/play/android/ samples/com/gcmquickstart/MyInstanceIDListenerService.java),如果InstanceID令牌更新,則調用此令牌刷新通知。如果上一個令牌的安全性受到了損害,則可能會發生這種情況。' – mpkuth

+0

@mpkuth對不起,我沒有看到受到危害的安全性可能與恢復的備份數據的使用情況有任何關係討論。 –

7

我今天意識到同樣的問題。首先,這條消息必須來自google本身(來自= google.com/iid),否則from屬性將是您的項目在谷歌開發者控制檯(即475832179747)中的ID。但是可以肯定的是,我關閉了我們的應用程序服務器,並且仍然收到了消息。

當我在Google Cloud Messaging服務器上新註冊時,我總是收到它。這不是一個大問題,因爲你可以通過intent-action來過濾消息,但我真的很想知道它的目的。

0

同樣的事情發生在我身上至少華碩平板

可能在更多的設備,但我還沒有機會去看看

我正在尋找在意向某些特定字符串.getExtras()所以修復很簡單,如果它們不存在,則忽略整個事情。

Google的某個人有什麼機會出現並解釋發生了什麼?

+1

來自Google的某人有什麼機會出現並解釋發生了什麼? - 這將是相當不錯的... – vkislicins

2

對於延長WakefulBroadcastReceiver現有的應用程序,谷歌 建議遷移到GCMReceiver和GcmListenerService。要 遷移:

  • 在應用清單,以「com.google.android.gms.gcm.GcmReceiver」取代你GcmBroadcastReceiver,並更換擴展IntentService新 GcmListenerService當前 服務聲明
  • 從客戶端代碼中刪除BroadcastReceiver實施
  • 重構當前IntentService服務實現以使用GcmListenerService有關詳細信息,請參閱示例清單。

似乎谷歌拆分其延伸IntentService處理GCMS到兩個服務的GCMIntentService,一個延伸GcmListenerService將處理接收到的消息和其他該濾波器iid.InstanceID分別濾除用於第一安裝接收到的通知, 這是新的GCM Android指南

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

https://developers.google.com/cloud-messaging/android/client

13

請參閱更新的GCM API Docs,正如@morepork所示。

對於擴展WakefulBroadcastReceiver的現有應用,Google 建議遷移到GCMReceiver和GcmListenerService。要 遷移:

在應用清單,以「com.google.android.gms.gcm.GcmReceiver」取代你GcmBroadcastReceiver,並更換擴展IntentService新GcmListenerService當前服務聲明

刪除從您的客戶端代碼

重構當前IntentService服務實現廣播接收器實現在這個頁面使用GcmListenerService

有關詳細信息,請參閱該示例清單和代碼示例。

從他們的sample code,它很容易遵循。

的AndroidManifest.xml

<receiver 
    android:exported="true" 
    android:name="com.google.android.gms.gcm.GcmReceiver" 
    android:permission="com.google.android.c2dm.permission.SEND"> 
    <intent-filter> 
     <action android:name="com.google.android.c2dm.intent.RECEIVE"/> 
     <category android:name="com.example.client"/> 
    </intent-filter> 
</receiver> 

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

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

<service 
    android:name=".MyGcmRegistrationService" 
    android:exported="false"> 
</service> 

MyGcmListenerService.java

public class MyGcmListenerService extends GcmListenerService { 
    @Override 
    public void onMessageReceived(String from, Bundle data) { 
     final String message = data.getString("message"); 
     makeNotification(message); 
    } 
} 

MyGcmRegistrationService.java

public class MyGcmRegistrationService extends IntentService { 
    private static final String TAG = "MyRegistrationService"; 
    private static final String GCM_SENDER_ID = "XXXXXXXXXXXX"; 
    private static final String[] TOPICS = {"global"}; 

    public MyGcmRegistrationService() { 
     super(TAG); 
    } 

    @Override 
    protected void onHandleIntent(Intent intent) { 
     try { 
      synchronized (TAG) { 
       InstanceID instanceID = InstanceID.getInstance(this); 
       String token = instanceID.getToken(GCM_SENDER_ID, 
         GoogleCloudMessaging.INSTANCE_ID_SCOPE, null); 
       sendTokenToServer(token); 
       subscribeTopics(token); 
      } 
     } catch (IOException e) { 
      e.printStackTrace(); 
     } 
    } 

    private void subscribeTopics(String token) throws IOException { 
     for (String topic : TOPICS) { 
      GcmPubSub pubSub = GcmPubSub.getInstance(this); 
      pubSub.subscribe(token, "/topics/" + topic, null); 
     } 
    } 
} 

MyInstanceIdListenerService.java

public class MyInstanceIdListenerService extends InstanceIDListenerService { 
    public void onTokenRefresh() { 
     Intent intent = new Intent(this, MyGcmRegistrationService.class); 
     startService(intent); 
    } 
} 

然後,你可以只用

Intent intent = new Intent(this, MyGcmRegistrationService.class); 
startService(intent); 
+0

謝謝你!正在尋找這個! – goonerDroid

+0

好答案! –

+0

我得到'INSTANCE_ID_SCOPE無法解析或不是一個字段'和'方法sendTokenToServer(字符串)未定義...'。 –

0

取代舊的註冊碼我遷移GCM->與只接受wakelockid元素FCM期間有過這個問題:

  • 火力控制檯
  • 轉載請求通過郵遞員請求是這樣的:

{ "to": "<your token from FirebaseInstanceId.getInstance().getToken()>", "notification": { "body": "Hello", "title": "This is test message." } }

另外我也複製了谷歌的所有代碼quickstart firebase messaging。 一切都應該沒問題。然而,在所有測試之後,我決定仔細檢查我的gradle庫版本。所以我增加了他們到最新的數字。從此我開始正確接收消息。

我建議從GitHub下載項目的最快解決方案,並嘗試這是否適合您。下一步是將此代碼複製到您的項目。如果在一個項目中一切正常,您至少有一個站立/工作點可以開始。

有關於android studio的傳言正在引發這個問題 - 但事實並非如此。我檢查了它。

可以這樣說,你可以使用舊的令牌(來自gcm)並且不接收消息,但是如果你有和我一樣的情況,即migrating,那麼令牌應該刷新爲新消息,你應該處理它..