GCM客户端没有收到下行消息 [英] GCM client does not receive downstream messages

查看:246
本文介绍了GCM客户端没有收到下行消息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的应用程序服务器被成功发送消息至GCM,然而,在客户端,没有被接收的消息,即使它具有令牌并且已经订阅了不同的主题。
对于听传入的消息,我使用GcmListenerService的延伸。由于这是一个服务,它从在MainActivity开始。贝娄是清单的相关部分:

 <! -  [START gcm_permission]  - >
<使用许可权的android:NAME =com.google.android.c2dm.permission.RECEIVE/>
<使用许可权的android:NAME =android.permission.WAKE_LOCK/>
<! - [END gcm_permission] - >
&所述;! - [开始gcm_receiver] - GT;
    <接收
        机器人:名字=com.google.android.gms.gcm.GcmReceiver
        机器人:出口=真
        机器人:权限=com.google.android.c2dm.permission.SEND>
        &所述;意图滤光器>
            <作用机器人:名字=com.google.android.c2dm.intent.RECEIVE/>
            <类机器人:名字=my.app/>
        &所述; /意图滤光器>
    < /接收器>
    <! - [END gcm_receiver] - >
    &所述;! - [开始gcm_listener] - GT;
    <服务
        机器人:名字=my.app.MyGcmListenerService
        机器人:出口=假>
        &所述;意图滤光器>
            <作用机器人:名字=com.google.android.c2dm.intent.RECEIVE/>
        &所述; /意图滤光器>
    < /服务>
    <! - [END gcm_listener] - >
    &所述;! - [开始instanceId_listener] - GT;
    <服务
        机器人:名字=my.app.MyInstanceIDListenerService
        机器人:出口=假>
        &所述;意图滤光器>
            <作用机器人:名字=com.google.android.gms.iid.InstanceID/>
        &所述; /意图滤光器>
    < /服务>
    <! - [END instanceId_listener] - >
    <服务
        机器人:名字=my.app.RegisterIntentService
        机器人:出口=假>
    < /服务>

该GCM监听器是如下:

 公共类MyGcmListenerService扩展GcmListenerService {私有静态最后弦乐TAG =MyGcmListenerService;
// [START RECEIVE_MESSAGE]
@覆盖
公共无效onMessageReceived(String一个,捆绑数据){
    字符串消息= data.getString(信息);
    Log.d(TAG,发件人:+的);
    Log.d(TAG消息:+消息);
    如果(from.startsWith(/主题/)){
        //从一些主题接收到的消息。
        // TOPICS = {新闻报,更新,GeneralInfo};        如果(from.split(/)[2] .equals(新闻)){
            ....

由于这是前面提到的,GCM监听器正在从在MainActivity作为常规服务启动。

  //启动消息的侦听器服务
    intentGCMListen =新意图(这一点,MyGcmListenerService.class);
    startService(intentGCMListen);

在这个例子中( GCM快速入门),我紧随其后,这基本上是我的部署为核心GCM在这部分,不存在这样的GCM侦听器服务启动。我认为它应该开始,因为这是一个服务。
任何想法,为什么这些消息未从GCM服务器下载?

更新

这是我使用从服务器到GCM发送消息的bash脚本是:

 #!/斌/庆典
API_KEY = AIadasdasdsa
消息= $ 1
话题= /主题/ GeneralInfo
卷曲--header授权:键= $ API_KEY--header内容类型:应用/ JSONhttps://gcm-http.googleapis.com/gcm/send -d{\\到\\:\\ $主题\\,\\DATA \\:{\\消息\\:\\$消息\\}}


解决方案

我认为你缺少供应接收已经为您的应用程序客户端发送的GCM服务器广播的目的广播接收器在code处理。您需要检查在code以下行:

  / **
     *接收推送消息
     * * /
    私人最终的BroadcastReceiver mHandleMessageReceiver =新的广播接收器(){
        @覆盖
        公共无效的onReceive(上下文的背景下,意图意图){            字符串NewMessage作为= intent.getExtras()的getString(EXTRA_MESSAGE)。
            //醒来移动,如果它是睡觉
            WakeLocker.acquire(getApplicationContext());            / **
            *采取适当的行动对这个消息
            *根据您的应用需求
            *现在我只是显示在屏幕上
            * * /            //显示接收到的消息
            lblMessage.append(NewMessage作为+\\ n);
            Toast.makeText(getApplicationContext(),新邮件+ NewMessage作为,Toast.LENGTH_LONG).show();            //释放锁唤醒
            WakeLocker.release();
       }
   };

有关更多code说明看看这个<一个href=\"http://www.androidhive.info/2012/10/android-push-notifications-using-google-cloud-messaging-gcm-php-and-mysql/\"相对=nofollow>教程。

My app server is sending out messages to GCM successfully, however, in the client side, the messages are not being received, even though it has the token and it has been subscribed to different topics. For listening to the incoming messages, I am using an extension of GcmListenerService. Since this is a service, it starts from the mainactivity. Bellow is the relevant part of the manifest:

 <!-- [START gcm_permission] -->
<uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
<!-- [END gcm_permission] -->
<!-- [START gcm_receiver] -->
    <receiver
        android:name="com.google.android.gms.gcm.GcmReceiver"
        android:exported="true"
        android:permission="com.google.android.c2dm.permission.SEND" >
        <intent-filter>
            <action android:name="com.google.android.c2dm.intent.RECEIVE" />
            <category android:name="my.app" />
        </intent-filter>
    </receiver>
    <!-- [END gcm_receiver] -->
    <!-- [START gcm_listener] -->
    <service
        android:name="my.app.MyGcmListenerService"
        android:exported="false" >
        <intent-filter>
            <action android:name="com.google.android.c2dm.intent.RECEIVE" />
        </intent-filter>
    </service>
    <!-- [END gcm_listener] -->
    <!-- [START instanceId_listener] -->
    <service
        android:name="my.app.MyInstanceIDListenerService"
        android:exported="false">
        <intent-filter>
            <action android:name="com.google.android.gms.iid.InstanceID"/>
        </intent-filter>
    </service>
    <!-- [END instanceId_listener] -->
    <service
        android:name="my.app.RegisterIntentService"
        android:exported="false">
    </service>

The GCM listener is below:

public class MyGcmListenerService  extends GcmListenerService {

private static final String TAG = "MyGcmListenerService";


// [START receive_message]
@Override
public void onMessageReceived(String from, Bundle data) {
    String message = data.getString("message");
    Log.d(TAG, "From: " + from);
    Log.d(TAG, "Message: " + message);


    if (from.startsWith("/topics/")) {
        // message received from some topic.
        //TOPICS = {"News","Updates","GeneralInfo"};

        if (from.split("/")[2].equals("News")) {
            ....

As it was mentioned before, the GCM listener is being started from the mainactivity as a regular service.

 //starting the listener service for messages
    intentGCMListen = new Intent(this,MyGcmListenerService.class);
    startService(intentGCMListen);

In the example (GCM quickstart) that I followed, which is basically the core of my deployment for GCM in this part, there is no such a start for the GCM listener service. I assumed that it should be started because it is a service. Any ideas why the messages are not being downloaded from the GCM server?

UPDATE

The bash script that I am using for sending the messages from the server to GCM is:

#!/bin/bash
api_key=AIadasdasdsa
message=$1
topic=/topics/GeneralInfo
curl --header "Authorization: key=$api_key" --header Content-Type:"application/json" https://gcm-http.googleapis.com/gcm/send  -d "{\"to\":\"$topic\",\"data\":{\"message\":\"$message\"}}"

解决方案

I think you are missing the BroadcastReceiver in your code that serves the purpose of receiving the Broadcast that has been send by the GCM server for your app clients to process. You need to check for the following lines in the code:

    /**
     * Receiving push messages
     * */
    private final BroadcastReceiver mHandleMessageReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {

            String newMessage = intent.getExtras().getString(EXTRA_MESSAGE);
            // Waking up mobile if it is sleeping
            WakeLocker.acquire(getApplicationContext());

            /**
            * Take appropriate action on this message
            * depending upon your app requirement
            * For now i am just displaying it on the screen
            * */

            // Showing received message
            lblMessage.append(newMessage + "\n");           
            Toast.makeText(getApplicationContext(), "New Message: " + newMessage, Toast.LENGTH_LONG).show();

            // Releasing wake lock
            WakeLocker.release();
       }
   };

For more code description take a look at this tutorial.

这篇关于GCM客户端没有收到下行消息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆