GCM Google服务器响应空消息 [英] GCM Google server responding with null message

查看:338
本文介绍了GCM Google服务器响应空消息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在追踪这个链接,但是在向GCM API发送消息后,我的Android收到一个空消息(值设置为 null )。



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

  @csrf_exempt 
def send_notification(request):
message = {name:Kousik,email:kousik.tict@gmail.com}
registration_ids = ['xxxxxxxxxxxxxxx ''''xxxxxxxxxxxxxxxx']
post_data = {data:message,registration_ids:registration_ids}
headers = {'Content-Type':'application / json','Authorization' = xxxxxxxxx'}
导入请求
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
//如果睡眠,唤醒移动
WakeLocker.acquire(getApplicationContext());


//显示收到的消息
lblMessage.append(newMessage +\\\
);
Toast.makeText(getApplicationContext(),New Message:+ newMessage,Toast.LENGTH_LONG).show();

//释放唤醒锁
WakeLocker.release();
}
};

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





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

解决方案

这里有一些我可以说的话:


  1. Google API控制台不显示GCM统计信息,显示您不应该看到任何内容。


  2. 您的服务器不发送消息键(它只发送名称电子邮件,所以我不知道是什么


  3. 我不明白这一行:消息 / p>


private final BroadcastReceiver mHandleMessageReceiver = new BroadcastReceiver() p>

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



我的猜测是您正在本地调用BroadcastReceiver在您的应用程序中,您从服务器发送的消息尚未到达该应用程序。



您应该在服务器端包含您的服务器从Google获取的答复,以确保问题不在您的服务器端。你也应该包括你的清单。



这不完全是一个答案,但是评论太长了,我认为它可以指示你解决你的问题问题。


I am following this link, but after sending a message to the GCM API my Android receives an empty message (value set to null).

This is my Django View, sending the message to the 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')

However, the traffic report shows no messages being sent at all:

Android Code receiving the GCM message:

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();
    }
};

I put a break point here and I got the following:

Note the message value in the hashmap, it is null.

解决方案

Here are some things I can say :

  1. Google APIs console doesn't show GCM statistics, show you shouldn't expect to see anything there.

  2. Your server doesn't send a message key (it sends only name and email, so I don't know what's the message key that is null in the debugger screenshot.

  3. I don't understand this line :

private final BroadcastReceiver mHandleMessageReceiver = new BroadcastReceiver()

You don't have to instantiate the BroadcastReceiver that receives the messages from GCM. You should declare it in your manifest as shown on many tutorials (including the official GCM programming guide).

My guess is that you are invoking the BroadcastReceiver locally in your app, and the message you are sending from your server is not reaching the app.

You should include the response your server gets from Google in your question, so that we can be sure the problem is not on your server side. You should also include your manifest.

This is not exactly an answer, but it's too long for a comment, and I think it can point you in the direction of solving your problem.

这篇关于GCM Google服务器响应空消息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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