2017-01-03 72 views
1

我也跟着在互聯網上的例子和審查這裏的許多問題,並在其他網站上FirebaseInstanceIdService不會被調用

我建我的應用程序,以便處理FCM通知,但我仍然無法找出原因FirebaseInstanceIdService不會被調用。

我宣佈它在AndroidManifest.xml

<service android:name=".InstanceService"> 
    <intent-filter> 
     <action android:name="com.google.firebase.INSTANCE_ID_EVENT"></action> 
    </intent-filter> 
</service> 

<service android:name=".MessageService"> 
    <intent-filter> 
     <action android:name="com.google.firebase.MESSAGING_EVENT"></action> 
    </intent-filter> 
</service> 

我的服務是

public class InstanceService extends FirebaseInstanceIdService { 
    private static final String REG_Token="REG_Token"; 

    @Override 
    public void onTokenRefresh() { 

     String recent_token= FirebaseInstanceId.getInstance().getToken(); // get token from the server 

     Log.d(REG_Token,recent_token); // show device token in Logcat View 

    } 

我看logcat的但是沒有REG_Token那裏。

我試圖調試代碼並停止它在Log.d(REG_Token,recent_token);,但它並沒有停在那裏。

我認爲服務沒有被調用,我找不到原因。

+0

我遇到了一些問題,以及與註冊時,我發現,你第一次需要2次,我不知道爲什麼,(這是幾個月前),嘗試調整它2次 –

+0

您是否在您的清單中添加Internet權限? –

+0

嘗試添加super.onTokenRefresh(); –

回答

7

this documentation

當登記令牌可以改變:

  • 該應用程序將刪除實例ID
  • 該應用新的設備
  • 用戶卸載上還原/重新安裝應用程序
  • 用戶清除應用程序數據。

這意味着onTokenRefresh()方法不叫每一個應用程序被打開,但只有當這些4個事件之一發生時間。

最簡單的方法是卸載應用程序,然後再次安裝應用程序。安裝完成後,打開應用程序,然後查看logcat(根據您的代碼)。

希望這有助於:)

+0

我卸載後再運行應用程序。但是沒有REG_Token – asmgx

+0

是否確定logcat篩選器是Debug或Verbose? @asmgx – Wilik

+0

我使用Logcat,我也使用調試來停止Log中的代碼。d(REG_Token,recent_token);但從未到達 – asmgx