我正在将 GCM 集成到我的应用程序中(基于 this Google 的文章)。他们声称“它还提供了推荐的流线型注册 API。”我觉得这很痛苦。我解决了几层问题,现在我陷入了以下问题:
文章提到:
MyBroadcastReceiver
(在 list 中)和GcmBroadcastReceiver
(在代码中)。我假设它是同一件事, list 中的正确名称应该是
GcmBroadcastReceiver
MyIntentService
(在 list 中)并且根本没有在代码中提及。据我了解,这是一项应该扩展/实现
GCMBaseIntentService
的服务。
这项服务让我很困惑:
我正在使用
String regID = GoogleCloudMessaging.register(SENDER_ID);
。在这种情况下,GCMBaseIntentService.onRegistered()
似乎没有必要获取 regID。此外,我还有一个广播接收器
GcmBroadcastReceiver
,它接收推送的消息。在这种情况下,GCMBaseIntentService.onMessage()
是不必要的。
考虑到这一点,我觉得我应该使用 GCMBaseIntentService
+ GcmBroadcastReceiver
或 GCMBaseIntentService
。
有人可以解释一下我应该将这些哪些组合与这个讨厌的简化注册 API 一起使用?
最佳答案
An Android application running on a mobile device registers to receive messages by calling the GoogleCloudMessaging method register(senderID...). This method registers the application for GCM and returns the registration ID. This streamlined approach replaces the previous GCM registration process.
基于此,GCMBaseIntentService
在使用新的注册方式时已过时。它被替换为 GcmBroadcastReceiver
和 GoogleCloudMessaging
。
我不知道新注册流程比旧注册流程好在哪里。如果您不打算使用 GCM 将消息从设备发送到云端,您可以坚持使用旧的 GCMBaseIntentService
。 This link展示了旧的做事方式。旧方法还需要广播接收器,但您使用的是 Google 提供的类,您唯一需要编写的代码(除了启动注册之外)是一个扩展 GCMBaseIntentService
的类,您可以在其中实现onRegistered
和 onMessage
。
如果您想使用新的“精简”注册,您只需要GcmBroadcastReceiver
来处理消息和GoogleCloudMessaging.register
来注册。
至于 list :
.MyIntentService
是可选的:
An intent service to handle the intents received by the broadcast receiver. Optional.
.MyBroadcastReceiver
可能是指您应该实现的广播接收器,GcmBroadcastReceiver
就是此类接收器的示例。
关于android - Android GCM 的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16619097/