Bel*_*nar 5 android google-cloud-messaging
我在我的应用程序中实现了GCM.但是在一些版本之前它停止接收来自gcm的消息.当我通过http post发送消息时,谷歌说"InvalidRegistration".
调查显示,问题在于subscribeTopics(),它在最新版本中引发了INVALID_PARAMETERS.代码是
RegistrationIntentService.java
private void subscribeTopics(String token) throws IOException {
for (String topic : TOPICS) {
GcmPubSub pubSub = GcmPubSub.getInstance(RegApp.getContext());
try {
Log.i("KK_Reg", "Token to pubsub " + token);
pubSub.subscribe(token, "/topics/" + topic, null);
Log.i("KK_Reg", "PubSub subscribed");
} catch (java.io.IOException ex) {
Log.e("KK_Reg", "Subscribe topics error");
Log.e("KK_Reg", ex.toString());
}
}
}
Run Code Online (Sandbox Code Playgroud)
工作版和失败版之间的差异如下所示:
--- failed release/RegistrationIntentService.java
+++ working release/RegistrationIntentService.java
@@ -54,7 +54,8 @@
public class RegistrationIntentService extends IntentService {
private static final String TAG = "RegIntentService";
- private static final String[] TOPICS = {"ru-konvent-reg-reg-all"};
+ private static final Object VTAG = new Object();
+ private static final String[] TOPICS = {"all"};
private static final String gcm_reg_url;
@@ -85,11 +86,11 @@
Log.i(TAG, "GCM Registration Token: " + token);
local_token = token;
- RegApp.setGcmToken(local_token);
+ RegApp.setGcmToken(token);
RegApp.sendRegistrationToServer();
// Subscribe to topic channels
- subscribeTopics(local_token);
+ subscribeTopics(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,
@@ -120,15 +121,8 @@
// [START subscribe_topics]
private void subscribeTopics(String token) throws IOException {
for (String topic : TOPICS) {
- GcmPubSub pubSub = GcmPubSub.getInstance(RegApp.getContext());
- try {
- Log.i("KK_Reg", "Token to pubsub " + token);
- pubSub.subscribe(token, "/topics/" + topic, null);
- Log.i("KK_Reg", "PubSub subscribed");
- } catch (java.io.IOException ex) {
- Log.e("KK_Reg", "Subscribe topics error");
- Log.e("KK_Reg", ex.toString());
- }
+ GcmPubSub pubSub = GcmPubSub.getInstance(this);
+ pubSub.subscribe(token, "/topics/" + topic, null);
}
}
// [END subscribe_topics]
Run Code Online (Sandbox Code Playgroud)
当然,我无法想象使代码失败的变化.请帮忙.
| 归档时间: |
|
| 查看次数: |
1553 次 |
| 最近记录: |