首页
学习
活动
专区
圈层
工具
发布

GCM澄清
EN

Stack Overflow用户
提问于 2013-08-26 07:48:40
回答 2查看 131关注 0票数 0

我知道这些方法是不推荐的,但是由于新的GCM似乎是错误的,我将回到这些方法,直到Google推出一个稳定的版本。

我们在舱单内宣布这个接收器。

代码语言:javascript
复制
        <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" />

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

代码语言:javascript
复制
   @Override
protected void onMessage(Context context, Intent intent) 
{
    Log.i(TAG, "Received message");
    String message = intent.getExtras().getString("msg");
}

1.但是,一旦收到消息,就不会调用该方法。为什么?

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

代码语言:javascript
复制
registerReceiver(mHandleMessageReceiver, new IntentFilter("intent_filter_string"));

与下面的类关联。

代码语言:javascript
复制
private final BroadcastReceiver mHandleMessageReceiver = new BroadcastReceiver() 
   {
    @Override
    public void onReceive(Context context, Intent intent) 
   {
        String newMessage = intent.getExtras().getString("data");
    }
 };

它在onPause中未注册。

  1. 为什么我们需要创建这个广播接收器?
  2. 我们不能在舱单上这么做吗?
  3. 不是onMessage()已经在GCMIntentService类中讨论过了吗?
  4. 挂起的意图字符串扮演什么角色?

我们很感谢你的回答。

EN

回答 2

Stack Overflow用户

发布于 2013-08-26 09:05:06

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

代码语言:javascript
复制
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.

我们不能在舱单上这么做吗?

代码语言:javascript
复制
Yes, you can do it in manifest too.

不是onMessage()已经在GCMIntentService类中讨论过了吗?

代码语言:javascript
复制
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.

挂起的意图扮演什么角色?

根据文档

代码语言:javascript
复制
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.
票数 1
EN

Stack Overflow用户

发布于 2013-08-26 08:11:43

你把你的应用程序/设备组合注册到你的GCm项目了吗?您必须首先在onRegistered方法中这样做。你添加了所有必要的权限吗?Google说你不需要在Android4.0之上添加自定义权限,但是我的应用程序在没有它们的情况下是不会工作的。如果您正在寻找一种更容易使用GCM的方法,我建议使用apiOmat:http://www.apiomat.com,我知道它的后端是一个服务。但是,如果你的应用是小的,你不需要支付任何东西,这要容易得多。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/18439173

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档