[英]Send Android Push Notifications to Multiple Devices using PHP, PDO, MYSQL
I have seen many tutorials for sending push notifications to Android devices using php and mysql but i would like to use PDO to implement this, i have been trying to figure out the problem, i am getting "invalid registration" but it is working if i directly assign gcm registration id to variable but not if i am getting gcm registration id from database. 我已经看过许多使用php和mysql向Android设备发送推送通知的教程,但是我想使用PDO来实现这一点,我一直在试图找出问题,我正在“无效注册”,但是如果我直接将gcm注册ID分配给变量,但是如果我从数据库中获取gcm注册ID,则不可以。 I think the problem is with the structure of array am getting from PDO
我认为问题在于数组结构是从PDO获取的
Here is the error i am getting 这是我遇到的错误
{"multicast_id":5263664448936997879,"success":0,"failure":1,"canonical_ids":0,"results":[{"error":"InvalidRegistration"}]}
When i echo the array i am sending 当我回显我要发送的数组时
Array ( [registration_ids] => Array ( [0] => Array ( [gcm_id] => APA91bGZs7DIb2lVvtlx-Ltgz2_wbRcMIvy-MfYPxoXt6SyDQlUjEnxgNTEw2vS6U5fe9u62i1LZo_gfhipUqT-FCgDj0U7JAwWwOvVmEhx9xIcs0k6mBsg7AgY6pxCVuaXYhqde0mZd ) ) ) [data] => Array ( [update] => thbtrhrt ) )
and my code is 我的代码是
function send_notification($registatoin_ids, $message) {
$url = 'https://android.googleapis.com/gcm/send';
$fields = array(
'registration_ids' => $registatoin_ids,
'data' => $message,
);
print_r($fields);
$headers = array(
'Authorization: key=AShthdfDeac36l6c6G1huGogerJDUvtvOPQ6hA',
'Content-Type: application/json'
);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
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);
if ($result === FALSE) {
die('Curl failed: ' . curl_error($ch));
}
curl_close($ch);
return $result;
}
function getGCMIds($univid)
{
global $dbConnect;
$query = $dbConnect->prepare("SELECT gcmid FROM android_gcm_table WHERE univid = ?");
$query->bindValue(1, $univid);
$query->execute();
try
{
return $results = $query->fetchAll(PDO::FETCH_ASSOC);
}
catch(PDOException $e)
{
die($e->getMessage());
}
}
$gcmIds = getGCMIds($_POST['univ']); // if i directly paste the gcm reg id here and put $gcmIds in array then its working.
$notification = $_POST['message'];
$notification = array("update" => $notification);
echo $resultLast = send_notification($gcmIds, $notification);
It would be great if someone help me, Thank You 如果有人帮助我,那太好了,谢谢
$query->fetchAll(PDO::FETCH_COLUMN) Worked for me $ query-> fetchAll(PDO :: FETCH_COLUMN)为我工作
function getGCMIds($univid)
{
global $dbConnect;
$query = $dbConnect->prepare("SELECT gcmid FROM android_gcm_table WHERE univid = ?");
$query->bindValue(1, $univid);
$query->execute();
try
{
return $results = $query->fetchAll(PDO::FETCH_COLUMN);
}
catch(PDOException $e)
{
die($e->getMessage());
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.