Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/250.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用PHP、PDO、MYSQL向多个设备发送Android推送通知_Php_Android_Mysql_Pdo - Fatal编程技术网

使用PHP、PDO、MYSQL向多个设备发送Android推送通知

使用PHP、PDO、MYSQL向多个设备发送Android推送通知,php,android,mysql,pdo,Php,Android,Mysql,Pdo,我看过很多使用php和mysql向Android设备发送推送通知的教程,但我想使用PDO来实现这一点,我一直在努力解决这个问题,我得到了“无效注册”但若我直接将gcm注册id分配给变量,但若我从数据库获取gcm注册id,那个么它就不起作用了。我认为问题在于我从PDO获得的阵列结构 这是我得到的错误 {"multicast_id":5263664448936997879,"success":0,"failure":1,"canonical_ids":0,"results":[{"error":"I

我看过很多使用php和mysql向Android设备发送推送通知的教程,但我想使用PDO来实现这一点,我一直在努力解决这个问题,我得到了“无效注册”但若我直接将gcm注册id分配给变量,但若我从数据库获取gcm注册id,那个么它就不起作用了。我认为问题在于我从PDO获得的阵列结构

这是我得到的错误

{"multicast_id":5263664448936997879,"success":0,"failure":1,"canonical_ids":0,"results":[{"error":"InvalidRegistration"}]}
当我回显正在发送的阵列时

    Array ( [registration_ids] => Array ( [0] => Array ( [gcm_id] => APA91bGZs7DIb2lVvtlx-Ltgz2_wbRcMIvy-MfYPxoXt6SyDQlUjEnxgNTEw2vS6U5fe9u62i1LZo_gfhipUqT-FCgDj0U7JAwWwOvVmEhx9xIcs0k6mBsg7AgY6pxCVuaXYhqde0mZd ) ) ) [data] => Array ( [update] => thbtrhrt ) )
我的代码是

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);
如果有人能帮我,那就太好了,谢谢你

$query->fetchAll(PDO::FETCH_列)为我工作

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());
}
}
$query->fetchAll(PDO::FETCH_列)对我有用

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());
}
}

只是一些提示:您确定存储在数据库中的registrationId是完整的id吗?我的意思是,这个字段在DB中的长度是多少?是真的吗?尝试对该值使用一些
trim()
,因为它可能包含一个空格?只是一些提示:您确定存储在数据库中的registrationId是完整id吗?我的意思是,这个字段在DB中的长度是多少?是真的吗?尝试对该值也使用一些
trim()
,因为它可能包含空格?