我想在android中用php發送通知。在android中用PHP發送通知
請看代碼
private function sendNotification(){
define('API_ACCESS_KEY', '**************');
$registrationIds = array("APA91bGeI8ILESgDzxoMnBRiP43k46CT31UySs0Q04FTHxyUFgBWrNBFKQKBGHwRvqn-vA7GdNRFrRMtmfQtQ9AGV8zvjYPa0nwWe64HTenIIwyIEIvQx-T0hmRpM7ybQvmmblK2GdnI");
$msg = array(
'message' => 'here is a message. message',
'title' => 'This is a title. title'
);
$fields = array(
'registration_ids' => $registrationIds,
'data' => $msg
);
$headers = array(
'Authorization: key=' . API_ACCESS_KEY,
'Content-Type: application/json'
);
$ch = curl_init();
curl_setopt($ch,CURLOPT_URL, 'https://android.googleapis.com/gcm/send');
curl_setopt($ch,CURLOPT_POST, true);
curl_setopt($ch,CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch,CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch,CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_IPRESOLVE, CURL_IPRESOLVE_V4);
curl_setopt($ch,CURLOPT_POSTFIELDS, json_encode($fields));
$result = curl_exec($ch);
curl_close($ch);
return $result;
}
當我運行這段代碼。它不會在移動設備上發送通知。
,並給予
{"multicast_id":6523573899048274101,"success":1,"failure":0,"canonical_ids":0,"results":[{"message_id":"0:1456312662566810%bb518b18f9fd7ecd"}]}
您需要爲Receive消息創建Broadcastreceiver和Intent Service – RushDroid