android – GCM谷歌服务器响应空消息

我正在关注
this链接,但在向GCM API发送消息后,我的Android收到一条空消息(值设置为null).

这是我的Django View,将消息发送到GCM API:

@csrf_exempt
def send_notification(request):
    message = {"name": "Kousik","email": "kousik.tict@gmail.com"}
    registration_ids = ['xxxxxxxxxxxxxxx','xxxxxxxxxxxxxxxxxx']
    post_data = {"data":message,"registration_ids":registration_ids}
    headers = {'Content-Type': 'application/json', 'Authorization':'key=xxxxxxxxx'}
    import requests    
    post_response = requests.post(url='https://android.googleapis.com/gcm/send', data=simplejson.dumps(post_data), headers=headers)
    to_json = {'status':str(post_response)}
    return HttpResponse(simplejson.dumps(to_json), mimetype='application/json')

但是,流量报告显示根本没有发送消息:

接收GCM消息的Android代码:

static final String EXTRA_MESSAGE = "message";
private final BroadcastReceiver mHandleMessageReceiver = new BroadcastReceiver() {
    @Override
    public void onReceive(Context context, Intent intent) {
        Bundle bundle = intent.getExtras();
        for(String key: bundle.keySet()){
            Log.d("gen", key+" - "+bundle.get(key));
        }


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


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

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

我在这里提出了一个断点,我得到了以下内容:

请注意hashmap中的消息值,它为null.

最佳答案 以下是我可以说的一些事情:

> Google API控制台不显示GCM统计信息,显示您不应期望在那里看到任何内容.
>您的服务器不发送消息密钥(它只发送名称和电子邮件,因此我不知道调试器屏幕截图中的消息密钥是什么.
>我不明白这一行:

private final BroadcastReceiver mHandleMessageReceiver = new BroadcastReceiver()

您不必实例化从GCM接收消息的BroadcastReceiver.您应该在清单中声明它,如许多教程(包括官方GCM编程指南)所示.

我的猜测是你在你的应用程序中本地调用BroadcastReceiver,而你从服务器发送的消息没有到达应用程序.

您应该在问题中包含服务器从Google获得的响应,以便我们确定问题不在您的服务器端.您还应该包括您的清单.

这不是一个答案,但是评论太长了,我认为它可以指出你解决问题的方向.

点赞