从今天开始,我遇到了 GCM“订阅主题”的以下问题。Nexus 6、Android 6.0.1、Google Play 服务 9.0.83 在应用程序中使用 google-play-services:8.3.0。
步骤1
我按照 Google 的文档通过实例 ID 获取令牌。获得令牌后,我成功订阅了“主题/全局”主题并将令牌存储在共享首选项中。
protected void register() {
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.
// 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.
// [START get_token]
InstanceID instanceID = InstanceID.getInstance(this);
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.
SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this);
sharedPreferences.edit().putString("token", token).apply();
// 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(QuickstartPreferences.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(QuickstartPreferences.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);
}
第2步
经过一段时间/用户交互后,我想订阅其他主题。我从共享首选项中获取令牌并尝试像以前一样订阅,但这次失败并出现“java.io.IOException:InternalServerError”。异常当然被捕获,但我现在不知道如何进行。
private void subscribeTopics() throws IOException {
SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this);
String token = sharedPreferences.getString("token", null);
if(token == null) {
Log.e(TAG, "No token");
return;
}
GcmPubSub pubSub = GcmPubSub.getInstance(this);
for (String topic : TOPICS) {
pubSub.subscribe(token, "/topics/" + topic, null); // <--- FAILS HERE
}
Log.d(TAG, "Subscribed to topics.");
}
这个过程在过去的 5 个月里一直有效,没有出现任何问题。突然,从今天早上开始,订阅其他主题(第 2 步)失败了。知道切换到 Firebase 云消息传递 (FCM) 是否会带来重大变化?
目前我所有的客户端应用程序都不可用。非常感谢快速帮助。