我收到通知:“registration_ids”字段不是收到的JSON数组消息。任何人都可以帮我解决这个问题吗?
如果我手动注册代码,那么它可以完美地运行
function sendNotification($id_user, $msg_type) {
define('API_ACCESS_KEY','AIzaSyDLJmlsR5Px8t4BoGqs1UsGPY1R8TmUIxI');
$userData = mysql_query('SELECT * FROM tbl_users WHERE id = "'.$id_user.'"') or die(mysql_error());
if(mysql_num_rows($userData) == 1) {
$userData = mysql_fetch_array($userData);
$message = $title = '';
switch($msg_type) {
case "new_order":
$title = "New order added.";
$message = "New order has been added by ".$userData["username"];
break;
default:
$message = $title = 'Default Message & Title';
break;
}
$msg = array
(
'message' => $message,
'title' => $title,
'subtitle' => '',
'tickerText' => '',
'vibrate' => 1,
'sound' => 1,
'largeIcon' => 'large_icon',
'smallIcon' => 'small_icon'
);
$parentQuery = mysql_query('SELECT id_admin FROM tbl_user_relation WHERE id_user = "'.$id_user.'"') or die(mysql_error());
if(mysql_num_rows($parentQuery) == 1) {
$parentData = mysql_fetch_array($parentQuery);
$regQ = mysql_query('SELECT registration_id FROM tbl_users WHERE id = "'.$parentData['id_admin'].'" ') or die(mysql_error());
if(mysql_num_rows($regQ) == 1) {
$parentData = mysql_fetch_array($regQ);
$fields = array
(
'registration_ids' => $parentData,
'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;
}
}
}
}