Google服务器响应空消息

Google服务器响应空消息

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

问题描述

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

  • 我不明白这一行:消息 / 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": "[email protected]"}
        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服务器响应空消息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

    08-19 17:55