【问题标题】:GCM clarificationsGCM 说明
【发布时间】:2013-08-28 15:36:30
【问题描述】:

我知道这些方法已被弃用,但由于新的 GCM API 似乎有问题,我将恢复使用这些方法,直到 Google 推出稳定版本。

我们在清单中声明这个接收器。

        <receiver
        android:name="com.google.android.gcm.GCMBroadcastReceiver"
        android:permission="com.google.android.c2dm.permission.SEND" >
        <intent-filter>

            <!-- Receives the actual messages. -->
            <action android:name="com.google.android.c2dm.intent.RECEIVE" />
            <!-- Receives the registration id. -->
            <action android:name="com.google.android.c2dm.intent.REGISTRATION" />
            <category android:name="com.myApp" />
        </intent-filter>
    </receiver>

  <service android:name=".GCMIntentService" />

我们在 GCMIntentService 类中有 onMessage() 方法。

   @Override
protected void onMessage(Context context, Intent intent) 
{
    Log.i(TAG, "Received message");
    String message = intent.getExtras().getString("msg");
}

1. 但是,在收到消息后,永远不会调用此方法。为什么 ?

此外,我遵循的示例使用以下内容。

registerReceiver(mHandleMessageReceiver, new IntentFilter("intent_filter_string"));

与以下类相关。

private final BroadcastReceiver mHandleMessageReceiver = new BroadcastReceiver() 
   {
    @Override
    public void onReceive(Context context, Intent intent) 
   {
        String newMessage = intent.getExtras().getString("data");
    }
 };

在 onPause 中取消注册。

  1. 为什么我们需要创建这个广播接收器?
  2. 我们不能在清单中这样做吗?
  3. 这不是已经被 GCMIntentService 类中的 onMessage() 覆盖了吗?
  4. Pending Intent String 起什么作用?

感谢您的回答。

【问题讨论】:

    标签: android google-cloud-messaging


    【解决方案1】:

    为什么我们需要创建这个广播接收器?

    In some cases you might be interested in updating the UI if the app is running.
    So you create a broadcast receiver at runtime and unregister it when the app
    goes into background.
    

    我们不能在清单中这样做吗?

    Yes, you can do it in manifest too.
    

    这不是已经被 GCMIntentService 类中的 onMessage() 覆盖了吗?

    GCMIntentService extends GCMBaseIntentService. So any message coming from gcm,
    will first be recieved in the onMessage of the GCMIntentService.
    Its upto you to decide how you handle the message.
    You can create a notification or send a broadcast to your custom broadcast
    receivers and update the UI when the app is running.
    

    Pending Intent 扮演什么角色?

    根据docs

    A PendingIntent itself is simply a reference to a token maintained by the system 
    describing the original data used to retrieve it. This means that, even if its
    owning application's process is killed, the PendingIntent itself will remain
    usable from other processes that have been given it.
    

    【讨论】:

    • 很好的解释。我有一个问题,我在 intenetService 中的 onMessage 没有被调用,但 onRegistered 被调用。有什么提示吗?
    • 设备注册了吗?您是否尝试向设备发送消息?另外最好用真机测试GCM。
    • 据我所知,是的,它已注册,因为我正在获取密钥。 (我怎么能确定它确实如此?)我使用的是真实设备。准确地说是运行 4.0.3 的 LG L5。
    • 在一些文本视图中记录注册ID。当您从服务器发送消息时,请尝试检查响应。
    • 我将它保存在一个共享偏好中,也将它发送到我的在线数据库,我还将它设置为 registerOnServer(true).. 来自服务器的 php 响应是肯定的消息正在发送。 ..仍然没有触发onMessage..
    【解决方案2】:

    您是否已将您的应用/设备组合注册到您的 GCm 项目中?您必须首先在 onRegistered 方法中执行此操作。您是否添加了所有必要的权限?谷歌说你不必在 android 4.0 以上添加自定义权限,但我的应用没有它们就无法工作。 如果您正在寻找使用 GCM 的更简单方法,我推荐 apiOmat:http://www.apiomat.com 我知道它的后端即服务。但是,如果您的应用很小,则无需支付任何费用,而且使用起来会容易得多。

    【讨论】:

    • 是的,我在 onRegistered 中这样做。我在清单中也有所需的权限。我的 onRegistered 方法工作正常。我更喜欢坚持使用原生格式和服务器而不是第三方。
    猜你喜欢
    • 2018-01-01
    • 2011-01-04
    • 2015-04-03
    • 2013-10-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多