dpg76975 2013-05-07 20:25
浏览 48

使用PHP使用GCM将数据与数据一起发送

I am sending android push notifications using php (curl). Below is my class that sends the notification.

public function send_notification($registatoin_ids, $message)
{
    include_once 'config.php';
    $url = 'https://android.googleapis.com/gcm/send';

            //issue here
    $fields = array('registration_ids' => $registatoin_ids, 'data' => $message);

    $headers = array('Authorization: key=' . GOOGLE_API_KEY, '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));
    }

Like this I am able to send the message to the desired recipient and I am succeeding. However, I can only send messages like this.

Suppose I want to send additional information, such as a link of a site, or other data that won't be attached to the message.

Where would I add that ?

Are 'registration_ids' and 'data' unique values ?

androids documentation does not include php example.

  • 写回答

1条回答 默认 最新

  • dongzhenyin2001 2013-05-07 20:32
    关注

    The variable data can be an array. So your send the message like this.

    $data = array("url" => "wwww.google.com", "message" => "Hello, Google");
    
    评论

报告相同问题?

悬赏问题

  • ¥15 无线电能传输系统MATLAB仿真问题
  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀
  • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100
  • ¥15 关于#hadoop#的问题