2014-06-16 125 views
2

我正在處理移動應用程序,並且我正在推送通知。推送通知,令牌過期?

我可以檢索從電話公司(Apple或Android),一個令牌發送推,但我有一個問題:

此令牌是永遠不變?如果獲得一次令牌,我需要檢查令牌是否更改?

回答

2

從蘋果文檔,

令牌信任的這一階段的形式確保只有APN的生成 令牌其將稍後榮譽,它可以保證本身,一個 令牌通過傳遞給它設備與之前爲該特定設備配置的 相同,並且僅用於該設備。

如果用戶將備份數據恢復到新設備或重新安裝操作系統,則設備令牌會發生變化。

因此,它總是很好地用來自APN的令牌更新服務器。作爲優化的一部分,如果您收到相同的令牌,則不需要更新服務器。

+1

注意,[文檔](https://developer.apple.com/library/ios/documentation/NetworkingInternet/Conceptual/RemoteNotificationsPG/Chapters/ApplePushService.html)已經改變:您報價單中的最後一句已被刪除。 – macserv

1

對於Android的:

這取決於具體的實現,但什麼是從谷歌建議是,註冊ID,可以在應用程序更新後更改...

每次的註冊ID被更改,客戶端應該用新值更新服務器。

檢查:http://developer.android.com/google/gcm/client.html#sample-register

if (checkPlayServices()) { 
     gcm = GoogleCloudMessaging.getInstance(this); 
     regid = getRegistrationId(context); 

     if (regid.isEmpty()) { 
      registerInBackground(); 
     } 
    } else { 
     Log.i(TAG, "No valid Google Play Services APK found."); 
    } 

private void registerInBackground() { 
new AsyncTask() { 
    @Override 
    protected String doInBackground(Void... params) { 
     String msg = ""; 
     try { 
      if (gcm == null) { 
       gcm = GoogleCloudMessaging.getInstance(context); 
      } 
      regid = gcm.register(SENDER_ID); 
      msg = "Device registered, registration ID=" + regid; 

      // You should send the registration ID to your server over HTTP, 
      // so it can use GCM/HTTP or CCS to send messages to your app. 
      // The request to your server should be authenticated if your app 
      // is using accounts. 
      sendRegistrationIdToBackend(); 

      // For this demo: we don't need to send it because the device 
      // will send upstream messages to a server that echo back the 
      // message using the 'from' address in the message. 

      // Persist the regID - no need to register again. 
      storeRegistrationId(context, regid); 
     } catch (IOException ex) { 
      msg = "Error :" + ex.getMessage(); 
      // If there is an error, don't just keep trying to register. 
      // Require the user to click a button again, or perform 
      // exponential back-off. 
     } 
     return msg; 
    } 

    @Override 
    protected void onPostExecute(String msg) { 
     mDisplay.append(msg + "\n"); 
    } 
}.execute(null, null, null); 
... 
}