简体   繁体   English

使用PHP,PDO,MYSQL将Android推送通知发送到多个设备

[英]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.

相关问题 PHP:使用Firebase将多个推送通知发送到不同的设备 - PHP : send multiple push notifications to difeerent devices using firebase 使用PHP将GCM通知发送到多个设备 - Send GCM notifications to multiple devices using PHP 如何使用 FCM 使用 php 脚本向多个设备发送推送通知? - How to send push notifications to multiple devices using php script using FCM? 从PHP服务器向Android设备发送推送通知 - Send Push Notifications from PHP Server to Android Devices iOS使用PHP代码的多个推送通知不会发送到所有设备 - iOS Multiple push notifications using php code does not send to all devices 使用GCM向多个Android设备发送推送通知 - sending push notifications to multiple android devices using GCM 使用MySQL查询作为PHP脚本的标识,将FCM Push通知发送到android应用中的特定设备 - Send FCM Push notifcations to specific devices in android app using MySQL query as identification from a PHP script 使用GCM为我的Android应用程序发送推送通知通知给所有设备 - Send Push Notifications notification to all devices using GCM for my android app 在 PHP 中使用 Parse 将相同的通知推送到多个 Android 设备 - Push same notification to multiple Android devices using Parse in PHP 如何在PHP中使用UDID发送推送通知 - How to send push notifications using UDID in PHP
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM