2016-12-04 76 views
0
<?php 
require 'connecting.php'; 

$title="SiQi:"; 
$message="Are you safe?"; 
sendPush($title,$message); 

function sendPush($registrationIds,$title,$message) 
{ 
$result = mysql_query("SELECT regids FROM user"); 
while($row = mysql_fetch_assoc($result)){ 
$json[] = $row; 
} 

echo json_encode($json); 
// API access key from Google API's Console 
// replace API 
define('API_ACCESS_KEY', 'AIzaSyAToG8Xw3g6PbpM55-ZmTckLebGsYUGLns'); 
$registrationIds = $json; 
print_r($registrationIds); 
$msg = array 
(
'message' => $message, 
'title' => $title, 
'vibrate' => 1, 
'sound' => 1 

// you can also add images, additional Data 
); 

$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_POSTFIELDS, json_encode($fields)); 
$result = curl_exec($ch); 
curl_close($ch); 
echo $result; 
} 
?> 

這是我的php代碼。當我運行我的PHP時,我得到了這個結果。它顯示了我的數據庫中的2個令牌,但它不起作用。PHP gcm推送通知到多個設備錯誤

我引用了很多其他帖子,但仍無法解決此問題。任何人都可以告訴我如何更改我的代碼,使其工作?

[{"regids":"eKZu8R9f3lQ:APA91bH8hL8Mi1-CizwW3Gir2tQ-Ojx6GoBmYPmnPtZv6z_jf_HO27KJQX1YQSHV6Z7Av8ievbtU8VOgtJVMVj0D5AeRmRuUmNhSYKQ-cU4mbbJt-_2E0SqT4xda3WcCumCYKd6uxqIy"},{"regids":"c5JsjAL-gQ4:APA91bF0XmzjD7D2UpcHpNpsUU-kw911yCymga792n19ZtI5QfA3schZQ3Do9ZO5UUwktyCa3SMNmcdH5G1EBsERZ-6vwFNWzXLB7Y0MxQvA54slfXm-QgNjWL-RftlYYXQvdIhmTjtx"}]Array ([0] => Array ([regids] => eKZu8R9f3lQ:APA91bH8hL8Mi1-CizwW3Gir2tQ-Ojx6GoBmYPmnPtZv6z_jf_HO27KJQX1YQSHV6Z7Av8ievbtU8VOgtJVMVj0D5AeRmRuUmNhSYKQ-cU4mbbJt-_2E0SqT4xda3WcCumCYKd6uxqIy) [1] => Array ([regids] => c5JsjAL-gQ4:APA91bF0XmzjD7D2UpcHpNpsUU-kw911yCymga792n19ZtI5QfA3schZQ3Do9ZO5UUwktyCa3SMNmcdH5G1EBsERZ-6vwFNWzXLB7Y0MxQvA54slfXm-QgNjWL-RftlYYXQvdIhmTjtx)) {"multicast_id":7329308833098908144,"success":0,"failure":2,"canonical_ids":0,"results":[{"error":"InvalidRegistration"},{"error":"InvalidRegistration"}]} 

回答