【问题标题】:Gcm subscribe on topics in Ionic/CordovaGcm 订阅 Ionic/Cordova 中的主题
【发布时间】:2016-06-15 19:51:55
【问题描述】:

我正在尝试根据用户的喜好订阅特定主题的用户。当应用程序在 device.ready() 中启动时,我可以应用主题首选项:

var push = PushNotification.init({
  android: {
    senderID: "872486308701",
    topics: myTopics
  }
});

但我需要在用户使用应用程序时实时进行。 Android native gcm topic subscribe method

编辑:

如果我这样做,它会起作用吗?

var push = PushNotification.init({
  android: {
    senderID: "872486308701",
    topics: ['foo']
  }
});

push.on('registration', function(data) {
  console.log(data);
});

push.unregister(function() {
  console.log('success');
}, function() {
  console.log('error');
});

var push = PushNotification.init({
  android: {
    senderID: "872486308701",
    topics: ['foo','bar','baz']
  }
});

【问题讨论】:

  • 为确保客户端从主题中注销,请写:push.unregister(function() { console.log('success'); }, function() { console.log('error'); }, ["/topics/foo"]);

标签: android cordova ionic-framework push-notification google-cloud-messaging


【解决方案1】:

您可以随时在代码中调用PushNotification.init(),因为deviceReady 事件已经触发,而不仅仅是device.ready()。您可以多次执行此操作。

注意,要更改用户的主题,您需要先调用push.unregister(successclb,errorclb,["topics/foo","topics/bar"]),然后才能订阅不同的主题。

【讨论】:

  • 我需要在PushNotification.init() 之前拨打push.unregister() 吗?我需要调用push.off()push.on() 来重置监听器?
  • 由于缺少任何unregisterTopic(),您需要注销才能摆脱旧主题。如果您不使用当前注册的主题调用unregister 并再次初始化,则两个主题都将注册到客户端。
  • 当我再次拨打PushNotification.init() 时,应用停止接收消息。
  • 你用相同的参数调用它吗?您需要再次提供所有信息(senderId 等)。回调中有错误吗?
  • 我遇到了一个我似乎无法解决的类似问题。请您看一下:stackoverflow.com/questions/39616067/…
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2019-04-24
  • 1970-01-01
  • 2017-11-26
  • 1970-01-01
  • 1970-01-01
  • 2015-09-06
  • 2020-09-20
相关资源
最近更新 更多