2016-02-15 47 views
0

gcmquickstart教程在每次應用啓動時啓動RegistrationServiceIntent,是否正確地做到這一點,並在每次運行時向InstanceID請求令牌(即使它相同)?如果是的話:有人能解釋爲什麼嗎?我不明白爲什麼這是必要的。每次應用啓動時GCM RegistrationIntentService getToken?

(最初的問題:GCM token refresh and when to send the token to server

+1

註冊接收每次都會調用它。如果你不想要這個,你可以從清單中排除它並建立你自己的。 – Amt87

回答

-1

你有你的GCM連接狀態存儲在例如共享偏好。任何時候你的應用程序午餐你檢查狀態,並採取相應的行動。例如:

boolean sentToken = sp.getBoolean(Extras.SENT_TOKEN_TO_SERVER, false); 
       if (!sentToken) { 
        Intent intent = new Intent(getActivity(), RegistrationIntentService.class); 
        getActivity().startService(intent); 
       } 

上面你會看到我正在檢查應用程序是否已經啓動了服務一次。

而且您會將sentToken狀態存儲在您的服務中。

0

我有相同的問題,我開了自己的GitHub的倉庫中的問題找到一個比較官方的回答:https://github.com/googlesamples/google-services/issues/256

在此期間,我存儲我的令牌SharedPreferences和纔剛剛開始,如果這個服務令牌不在那裏。一切都按預期工作。

public class MainActivity extends BaseActivity 
    @Override 
    protected void onCreate(Bundle savedInstanceState) { 

     if (sharedPreferences.getString(Constants.GCM_TOKEN, "").equals("")) { 
      // Start IntentService to register this application with GCM. 
      Intent intent = new Intent(this, RegistrationIntentService.class); 
      startService(intent); 
     } 
} 

記住令牌存儲在清單RegistrationIntentService

sharedPreferences.edit().putString(Constants.GCM_TOKEN, token).apply(); 
相關問題