doutuo4285 2016-02-16 13:57
浏览 32
已采纳

从mysql数据库一次向多个用户发送gcm

I'm developing an app and I'm on the final face where registered devices in the database are to receive push notifications at a time.

Currently, with the script I have, it can only send to one device. I have a table in my database called devices and the field that saves the device ID is called device_id. Please any help on how to send to multiple users?

<?php

$message = $_POST['title'];
$id = $_POST['id'];

// Replace with the real server API key from Google APIs
$apiKey = "AIzaSyDn9RPYmnlbs9*****WHKksz73klOqxM";

// Replace with the real client registration IDs
$registrationIDs = array("APA91bHhTiGeAKgphENoGH6********Blw9PspLCPAiprTSDN6mRuW3k253PZyppxgJaqPftTzYZOiWPt5C4XFVmkuqjbXp7MV3mAmpDAYM-11LdIdU0");

// Message to be sent

// Set POST variables
$url = 'https://android.googleapis.com/gcm/send';

$fields = array(
  'registration_ids' => $registrationIDs,
  'data' => array( "message" => $message,"title" => "Today's Devotion","id" => $id),
);
$headers = array(
  'Authorization: key=' . $apiKey,
  'Content-Type: application/json'
);

// Open connection
$ch = curl_init();

// Set the URL, number of POST vars, POST data
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_POSTFIELDS, json_encode( $fields));

curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
//curl_setopt($ch, CURLOPT_POST, true);
//curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode( $fields));

// Execute post
$result = curl_exec($ch);

// Close connection
curl_close($ch);
//echo $result;
//print_r($result);
//var_dump($result);
?>
  • 写回答

2条回答 默认 最新

  • dongshedan4672 2016-02-16 14:09
    关注

    Fetch all the device ids from your database as below:

    $registrationIDs = array();
    
    $query = "select device_id from devices";
    
    while($row = $db->database_fetch_assoc($query))
    {
        $registrationIDs[] = $row;
    }
    

    Now pass this array to the $field array.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?