Reputation: 61
<?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;
}
?>
This above is my php code. When I run my php, I get this result. It shows the 2 tokens from my database however it is not working.
I've referenced many other posts but still couldn't fix this problem. Can anyone tell me how to change my codes to make it work?
[{"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"}]}
Upvotes: 0
Views: 107
Reputation: 29
Put your $registrationIds
into array like array('eKZu8R9f3lQ','eKZu8R9f3lQ')
.
If not fixed above solution, take a updated GCM token from devices.
See: Using GCM to send notifications on app, returns InvalidRegistration error
Upvotes: 1