我已閱讀過有關GCM的文章,可能會刷新註冊ID,無需定期循環。我正在嘗試使用推送通知來構建應用程序,但不太確定如何處理此類刷新的註冊ID。每次應用程序啓動時請求Google Cloud Messaging(GCM)註冊ID
我的第一個策略是每次應用程序啓動時請求註冊ID並將其發送到應用程序服務器。它看起來工作,但聽起來有點不對......
可以這樣做嗎?
我已閱讀過有關GCM的文章,可能會刷新註冊ID,無需定期循環。我正在嘗試使用推送通知來構建應用程序,但不太確定如何處理此類刷新的註冊ID。每次應用程序啓動時請求Google Cloud Messaging(GCM)註冊ID
我的第一個策略是每次應用程序啓動時請求註冊ID並將其發送到應用程序服務器。它看起來工作,但聽起來有點不對......
可以這樣做嗎?
基本上,你應該做你的主要活動如下:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.my_layout);
GCMRegistrar.checkDevice(this);
GCMRegistrar.checkManifest(this);
final String regId = GCMRegistrar.getRegistrationId(this);
if (regId.equals("")) {
GCMRegistrar.register(this, GCMIntentService.GCM_SENDER_ID);
} else {
Log.v(TAG, "Already registered");
}
}
之後,你應該隨時應用程序接收到一個com.google.android.c2dm.intent.REGISTRATION
意圖用registration_id
額外的註冊ID發送到你的應用服務器,。當Google定期更新應用的ID時,可能會發生這種情況。
你可以通過自己的實現擴展com.google.android.gcm.GCMBaseIntentService
實現這一目標,例如:
public class GCMIntentService extends GCMBaseIntentService {
// Also known as the "project id".
public static final String GCM_SENDER_ID = "XXXXXXXXXXXXX";
private static final String TAG = "GCMIntentService";
public GCMIntentService() {
super(GCM_SENDER_ID);
}
@Override
protected void onRegistered(Context context, String regId) {
// Send the regId to your server.
}
@Override
protected void onUnregistered(Context context, String regId) {
// Unregister the regId at your server.
}
@Override
protected void onMessage(Context context, Intent msg) {
// Handle the message.
}
@Override
protected void onError(Context context, String errorId) {
// Handle the error.
}
}
有關詳細信息,我將(重新)閱讀writing the client side code和the Advanced Section of the GCM documentation的文檔。
希望有幫助!
註冊刷新不包含在新的GCM庫中。的Costin Manolache
的「定期」刷新
詞從來沒有發生過,而註冊刷新不包括在新的GCM庫。
註冊ID更改的唯一已知原因是應用程序 的舊bug如果在 升級時收到消息而自動取消註冊。在此錯誤修復之前,應用程序在升級後仍需要調用 register(),並且到目前爲止,此情況下注冊ID可能會更改爲 。顯式調用unregister()通常也會更改 註冊ID。
建議/解決方法是生成您自己的隨機標識符 ,例如保存爲共享首選項。在每次應用升級時,您可以上傳 標識符和潛在的新註冊ID。此 也可能有助於跟蹤和調試服務器端的升級和註冊 更改。
[處理Android上的Google雲消息傳遞中的註冊ID更改]的可能重複(http://stackoverflow.com/questions/16838654/handling-registration-id-changes-in-google-cloud-messaging-on -Android) – Eran