我正在关注此链接,但在向 GCM API 发送消息后,我的 Android 收到一条空消息(值设置为null
)。
这是我的 Django 视图,将消息发送到 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();
}
};
我在这里放了一个断点,得到以下信息:
注意message
哈希图中的值,它是null
.