我知道这些方法是不推荐的,但是由于新的GCM似乎是错误的,我将回到这些方法,直到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" />我们在onMessage类中有GCMIntentService ()方法。
@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中未注册。
我们很感谢你的回答。
发布于 2013-08-26 09:05:06
为什么我们需要创建这个广播接收器?
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.不是onMessage()已经在GCMIntentService类中讨论过了吗?
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.挂起的意图扮演什么角色?
根据文档
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.发布于 2013-08-26 08:11:43
你把你的应用程序/设备组合注册到你的GCm项目了吗?您必须首先在onRegistered方法中这样做。你添加了所有必要的权限吗?Google说你不需要在Android4.0之上添加自定义权限,但是我的应用程序在没有它们的情况下是不会工作的。如果您正在寻找一种更容易使用GCM的方法,我建议使用apiOmat:http://www.apiomat.com,我知道它的后端是一个服务。但是,如果你的应用是小的,你不需要支付任何东西,这要容易得多。
https://stackoverflow.com/questions/18439173
复制相似问题