GCM Google server responding with null message

2019-05-03 11:17发布

问题:

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.

回答1:

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.