我正在尝试理解Google GCM快速入门示例背后的代码。具体来说,我不明白代码如何检查注册是否已完成。
MainActivity:
RegistrationIntentService:
在RegistrationIntentService中,注释说明最初调用是为了检索令牌,但后续调用是本地的。这是否意味着它只是检查应用程序是否已经有令牌,而不再进行调用?我真的不理解这部分内容,也没有看到任何示例代码在哪里检查令牌的存在。
MainActivity:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
...
if (checkPlayServices()) {
// Start IntentService to register this application with GCM.
Intent intent = new Intent(this, RegistrationIntentService.class);
startService(intent);
}
}
RegistrationIntentService:
@Override
protected void onHandleIntent(Intent intent)
{
SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this);
try {
// [START register_for_gcm]
// Initially this call goes out to the network to retrieve the token, subsequent calls
// are local.
// [START get_token]
InstanceID instanceID = InstanceID.getInstance(this);
// R.string.gcm_defaultSenderId (the Sender ID) is typically derived from google-services.json.
// See https://developers.google.com/cloud-messaging/android/start for details on this file.
String token = instanceID.getToken(getString(R.string.gcm_defaultSenderId),
GoogleCloudMessaging.INSTANCE_ID_SCOPE, null);
// [END get_token]
Log.i(TAG, "GCM Registration Token: " + token);
// TODO: Implement this method to send any registration to your app's servers.
sendRegistrationToServer(token);
// You should store a boolean that indicates whether the generated token has been
// sent to your server. If the boolean is false, send the token to your server,
// otherwise your server should have already received the token.
sharedPreferences.edit().putBoolean(AppSharedPreferences.SENT_TOKEN_TO_SERVER, true).apply();
// [END register_for_gcm]
} catch (Exception e) {
Log.d(TAG, "Failed to complete token refresh", e);
// If an exception happens while fetching the new token or updating our registration data
// on a third-party server, this ensures that we'll attempt the update at a later time.
sharedPreferences.edit().putBoolean(AppSharedPreferences.SENT_TOKEN_TO_SERVER, false).apply();
}
// Notify UI that registration has completed, so the progress indicator can be hidden.
Intent registrationComplete = new Intent(QuickstartPreferences.REGISTRATION_COMPLETE);
LocalBroadcastManager.getInstance(this).sendBroadcast(registrationComplete);
}
在RegistrationIntentService中,注释说明最初调用是为了检索令牌,但后续调用是本地的。这是否意味着它只是检查应用程序是否已经有令牌,而不再进行调用?我真的不理解这部分内容,也没有看到任何示例代码在哪里检查令牌的存在。
R.string.gcm_defaultSenderId(发送者ID)通常是从google-services.json中获取的。您知道这是如何完成的吗?我的意思是,gcm sdk如何读取json并将其加载到R.string中?
- Weishi Z