2013-06-27 42 views
5

Khi câu hỏi cho biết Cách tìm hiểu khi nào thì registration ID đã trở thành không hợp lệ trong API GoogleCloudMessaging? Tôi đã đọc câu trả lời cho một số câu hỏi về chủ đề tương tự: Do GCM registration id's expire?Google Coud Mesaging (GCM) and registration_id expiry, how will I know?. Vấn đề với câu hỏi đó là câu trả lời có cho C2DM hoặc API GCM cũ sử dụng GCMRegistrar thay vì API GoogleCloudMessaging. Hai phương thức trước đã bị khấu hao.Trong API GoogleCloudMessaging, cách xử lý gia hạn hoặc hết hạn ID đăng ký?

tôi sẽ cố gắng phá vỡ sự nhầm lẫn của tôi/câu hỏi từng bước:

1) Bên dưới tiêu đề Enable GCM, trong điểm thứ hai nó nói:

Google may periodically refresh the registration ID, so you should design your Android application with the understanding that the com.google.android.c2dm.intent.REGISTRATION intent may be called multiple times. Your Android application needs to be able to respond accordingly.

The registration ID lasts until the Android application explicitly unregisters itself, or until Google refreshes the registration ID for your Android application. Whenever the application receives a com.google.android.c2dm.intent.REGISTRATION intent with a registration_id extra, it should save the ID for future use, pass it to the 3rd-party server to complete the registration, and keep track of whether the server completed the registration. If the server fails to complete the registration, it should try again or unregister from GCM.

2) Bây giờ, nếu đó là trường hợp thứ vi Tôi nên xử lý ý định trong BroadcastReceiver và gửi lại yêu cầu register() để nhận ID đăng ký mới. Nhưng vấn đề là trên cùng một trang dưới tiêu đề ERROR_MAIN_THREAD, nó nói rằng: GCM methods are blocking. You should not run them in the main thread or in broadcast receivers.

3) Tôi cũng hiểu rằng có hai kịch bản khác khi thay đổi ID đăng ký (như đã đề cập dưới Chủ đề Advanced thuộc nhóm Keeping the Registration State in Sync): cập nhật Ứng dụng và sao lưu & khôi phục. Tôi đã xử lý chúng khi mở ứng dụng.

4) Trong GCMRegistrar API, bên GCMBaseIntentService, có từng là một phương pháp gọi lại onRegistered(), mà đã gọi là khi thiết bị đã đăng ký. Ở đây tôi đã sử dụng để duy trì ID đăng ký và gửi tới máy chủ của bên thứ ba.

Nhưng, bây giờ Tôi nên xử lý việc cập nhật hoặc gia hạn ID đăng ký, lưu giữ và gửi nó đến máy chủ của bên thứ ba như thế nào?

Có thể là tôi đang bối rối khi đọc tất cả hoặc tôi đang thiếu thứ gì đó. Tôi sẽ rất biết ơn sự giúp đỡ của bạn.

Cập nhật

Ngay cả trên Handling registration ID changes in Google Cloud Messaging on Android chủ đề, không có đề cập về cách xử lý làm mới định kỳ các ID của Google?

+0

Có thể trùng lặp điều này - http://stackoverflow.com/questions/16838654/handling-registration-id-changes-in-google-cloud-messaging-on-android/16839326#16839326 – Eran

Trả lời

4

Tôi đưa ra một cách như Những gì tôi thực hiện trong ứng dụng của tôi

@Override 
protected void onRegistered(Context context, String registrationId) { 
    Log.i(TAG, "Device registered: regId = " + registrationId); 
    //displayMessage(context, getString(R.string.gcm_registered)); 
    //ServerUtilities.register(context, registrationId); 
    //1. Store this id to application Prefs on each request of device registration 
    //2. Clear this id from app prefs on each request of device un-registration 
    //3. Now add an if check for new registartion id to server, you can write a method on server side to check if this reg-id matching for this device or not (and you need an unique identification of device to be stored on server) 
    //4. That method will clear that if id is matching it meanse this is existing reg-id, and if not matching this is updated reg-id. 
    //5. If this is updated reg-id, update on server and update into application prefs. 
} 

Bạn có thể làm như thế này cũng

if reg_id exists_into prefrences then 
    if stored_id equals_to new_reg_id then 
     do nothing 
    else 
     say server to reg_id updated 
     update prefrences with new id 
    end if 
else 
    update this id to application prefs 
    say server that your device is registered 
end if 

Nhưng vấn đề nảy sinh khi, xóa người dùng dữ liệu ứng dụng và bạn sẽ mất reg-id hiện tại.


Cập nhật cho API mới dụ Tín đi vào Eran và trả lời của ông Handling registration ID changes in Google Cloud Messaging on Android

Google thay đổi Demo App họ để sử dụng giao diện mới. Chúng làm mới ID đăng ký bằng cách đặt ngày hết hạn trên giá trị được ứng dụng duy trì cục bộ. Khi ứng dụng bắt đầu, chúng tải id đăng ký được lưu trữ cục bộ của chúng. Nếu nó "hết hạn" (trong bản demo có nghĩa là nó đã nhận được từ GCM hơn 7 ngày trước), họ gọi lại là gcm.register(senderID).

Điều này không xử lý tình huống giả định trong đó ID đăng ký được làm mới bởi Google cho một ứng dụng chưa được khởi chạy trong một thời gian dài. Trong trường hợp đó, ứng dụng sẽ không nhận thức được sự thay đổi và máy chủ của bên thứ ba cũng sẽ không.

public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 

    setContentView(R.layout.main); 
    mDisplay = (TextView) findViewById(R.id.display); 

    context = getApplicationContext(); 
    regid = getRegistrationId(context); 

    if (regid.length() == 0) { 
     registerBackground(); 
    } 
    gcm = GoogleCloudMessaging.getInstance(this); 
} 

/** 
* Gets the current registration id for application on GCM service. 
* <p> 
* If result is empty, the registration has failed. 
* 
* @return registration id, or empty string if the registration is not 
*   complete. 
*/ 
private String getRegistrationId(Context context) { 
    final SharedPreferences prefs = getGCMPreferences(context); 
    String registrationId = prefs.getString(PROPERTY_REG_ID, ""); 
    if (registrationId.length() == 0) { 
     Log.v(TAG, "Registration not found."); 
     return ""; 
    } 
    // check if app was updated; if so, it must clear registration id to 
    // avoid a race condition if GCM sends a message 
    int registeredVersion = prefs.getInt(PROPERTY_APP_VERSION, Integer.MIN_VALUE); 
    int currentVersion = getAppVersion(context); 
    if (registeredVersion != currentVersion || isRegistrationExpired()) { 
     Log.v(TAG, "App version changed or registration expired."); 
     return ""; 
    } 
    return registrationId; 
} 

/** 
* Checks if the registration has expired. 
* 
* <p>To avoid the scenario where the device sends the registration to the 
* server but the server loses it, the app developer may choose to re-register 
* after REGISTRATION_EXPIRY_TIME_MS. 
* 
* @return true if the registration has expired. 
*/ 
private boolean isRegistrationExpired() { 
    final SharedPreferences prefs = getGCMPreferences(context); 
    // checks if the information is not stale 
    long expirationTime = 
      prefs.getLong(PROPERTY_ON_SERVER_EXPIRATION_TIME, -1); 
    return System.currentTimeMillis() > expirationTime; 
} 
+0

Cảm ơn câu trả lời Pankaj. Bạn đã làm điều này bên trong dịch vụ [GCMBaseIntentService] (http://developer.android.com/reference/com/google/android/gcm/GCMBaseIntentService.html)? Khi tôi đang sử dụng [GCMRegistrar] (http://developer.android.com/reference/com/google/android/gcm/GCMRegistrar.html), tôi đã sử dụng dịch vụ ý định này như bạn đã làm. –

+0

không không. Đây là phương thức 'GCMIntentService'. –

+0

Oh Yaa .. xin lỗi tôi có nghĩa là 'GCMIntentService' mở rộng' GCMBaseIntentService'? Điều đó đã được khấu hao phải không? (Nhưng vẫn hoạt động) –

0

Chỉ cần để thêm vào câu trả lời của Pankaj:

  • This(the example on getting started documents by Google) doesn't handle the hypothetical scenario in which a registration ID is refreshed by Google for an app that hasn't been launched for a long time. In that case, the app won't be aware of the change, and neither will the 3rd party server.

    đúng của nó mà ví dụ trên Getting started tài liệu không xử lý trường hợp đó. Vì vậy, nhà phát triển cần phải tự xử lý.

  • Ngoài ra câu trả lời nói rằng They refresh the registration ID by setting an expiration date on the value persisted locally by the app. When the app starts, they load their locally stored registration id. If it is "expired" they call gcm.register(senderID) again.

    Vấn đề là bảy ngày hết hạn cục bộ của registration ID trong mẫu là để tránh những kịch bản mà các thiết bị sẽ gửi đăng ký đến máy chủ bên thứ 3 nhưng máy chủ mất nó. Nó không xử lý việc làm mới ID từ máy chủ của Google.

  • Điểm thứ hai dưới tiêu đề Enable GCM trên Architectural Overview trang, nó nói:

    Note that Google may periodically refresh the registration ID, so you should design your Android application with the understanding that the com.google.android.c2dm.intent.REGISTRATION intent may be called multiple times. Your Android application needs to be able to respond accordingly.

    Vì vậy, để xử lý mà bạn nên có một Listener Broadcast mà có thể xử lý com.google.android.c2dm.intent.REGISTRATION ý định, mà Google gửi cho ứng dụng khi ứng dụng phải làm mới đăng ký ID.

  • Có một phần của câu hỏi trong đó nêu về the problem is that inside the Broadcast Listener I cannot call register the for Push ID again. This is because the documentation nói: GCM methods are blocking. You should not run them in the main thread or in broadcast receiver.

    Tôi nghĩ vấn đề đó hoàn toàn khác với tuyên bố. Khi bạn đăng ký bộ thu phát sóng, nó sẽ có một số Intent sẽ chứa registration ID mới từ Google. Tôi KHÔNG cần gọi lại phương thức gcm.register() trong Trình nghe phát sóng.

Hy vọng điều này sẽ giúp ai đó hiểu cách xử lý gia hạn ID đăng ký.

+0

Một yêu cầu khiêm tốn của nó rằng nếu bạn đang xuống bỏ phiếu cho câu trả lời, vui lòng bình luận để giải thích thêm. Điều này sẽ giúp tôi cải thiện câu trả lời và sẽ làm cho tôi hiểu quan điểm của bạn về nội dung câu trả lời. Mặc dù câu trả lời trước đó đã giúp nhưng không thực sự trả lời/giải quyết sự nhầm lẫn, vì vậy tôi trả lời nó một lần nữa. Nếu bạn nghĩ rằng có bất kỳ lời chỉ trích nào trong câu trả lời, xin hãy lên tiếng. Nó sẽ xóa nhiều khái niệm hơn. Cảm ơn một lần nữa! –

+1

Tôi không chắc tại sao điều này lại bị giảm giá. Nó rất rõ ràng và hữu ích. Upvote từ tôi. – KyleT

+0

bất cứ điều gì bạn viết không trả lời câu hỏi và bạn đang bối rối giữa api cũ và mới. hoặc cũ sẽ được sử dụng hoặc mới sẽ được sử dụng –

Các vấn đề liên quan