【问题标题】:Send multiple messages with GCM使用 GCM 发送多条消息
【发布时间】:2014-01-21 07:06:32
【问题描述】:

我这两天一直在处理这个问题。我想要做的是,我必须向注册的 GCM 设备发送多条消息。到目前为止,我可以向设备发送一条消息。下面是发送消息的代码。

send_message.php

<?php

    if (isset($_REQUEST["regId"]) && isset($_REQUEST["message"])) {
        $regId = $_REQUEST["regId"];
        $message = $_REQUEST["message"];

        include_once './GCM.php';

        $gcm = new GCM();

        $registatoin_ids = array($regId);
        $message = array("price" => $message);

        $result = $gcm->send_notification($registatoin_ids, $message);
        echo $registatoin_ids; echo $message; 
        echo $result;

    }

GCM.php

<?php

class GCM {

    //put your code here
    // constructor
    function __construct() {

    }


    //Sending Push Notification

    public function send_notification($registatoin_ids, $message) {
        // include config
        include_once './config.php';


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

        $fields = array(
            'registration_ids' => $registatoin_ids,
        'message' => $message, 
          );


        $headers = array(
            'Authorization: key=' . GOOGLE_API_KEY,
            'Content-Type: application/json'
        );

        //print_r($headers); exit();
        // 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);

        // Disabling SSL Certificate support temporarly
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);

        curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($fields));

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

        if ($result === FALSE) {
            die('Curl failed: ' . curl_error($ch));
        }

        // Close connection
        curl_close($ch);
        echo $result;
    }
}
?>

这里的消息正在接收方接收.. // 代码在 GCM.php 中

 $fields = array(
            'registration_ids' => $registatoin_ids,
            'message' => $message, 
          );

但我想在单个通知中发送多条消息。为此我做了什么......

send_message.php

<?php


if (isset($_REQUEST["regId"]) && isset($_REQUEST["message"]) && isset($_REQUEST["data"])) {
    $regId = $_REQUEST["regId"];
    $message = $_REQUEST["message"];
    $data = $_REQUEST["data"]; //added third parameter


    include_once './GCM.php';

    $gcm = new GCM();

    $registatoin_ids = array($regId);
    $message = array("price" => $message);
    $data = array("extra" => $data);


    $result = $gcm->send_notification($registatoin_ids, $message, $data);
    echo $registatoin_ids; echo $message; echo $data;
    echo $result;
}

GCM.php

<?php

class GCM {

    //put your code here
    // constructor
    function __construct() {

    }

    /**
     * Sending Push Notification
     */
    public function send_notification($registatoin_ids, $message, $data) {
        // include config
        include_once './config.php';


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

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


        $headers = array(
            'Authorization: key=' . GOOGLE_API_KEY,
            'Content-Type: application/json'
        );

        //print_r($headers); exit();
        // 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);

        // Disabling SSL Certificate support temporarly
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);

        curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($fields));

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

        if ($result === FALSE) {
            die('Curl failed: ' . curl_error($ch));
        }

        // Close connection
        curl_close($ch);
        echo $result;
    }
}

?>

在 android 中,我编写了接收这样消息的函数...

@Override
protected void onMessage(Context context, Intent intent) {
    Log.i(TAG, "Received message");
    String message = intent.getStringExtra("price");
    String newmessage = intent.getStringExtra("extra");
    displayMessage(context, message + newmessage);
    generateNotification(context, message + newmessage);
}

但我得到“价格”的空结果并得到“额外”的结果。 如何在单个通知中接收多个消息字符串?

【问题讨论】:

  • 单条消息是什么意思?它是在收到一条消息后停止,还是您想发送一系列消息?
  • 是的,我想发送消息队列...
  • 你是如何尝试在 php 中发送多条消息的?
  • 首先我试图在浏览器中运行 .php 文件,例如“localhost/gcm_server_php/send_message.php?regId=deviceid&message=Hello&data=welcome..”

标签: php android push-notification google-cloud-messaging


【解决方案1】:

在 send_message.php 中

将两条消息放在同一个对象中。我不知道你为什么叫它price,但试试这样:

$message = array('message' => $message,
                 'extra' => $data
);

在 GCM.php 中

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

在您的 Android 服务中

protected void onMessage(Context context, Intent intent) {
    //log the message in JSON format
    Log.i(TAG, "Received message >> " + intent.getExtras().toString());
    //Retrieve message and extra
    String message = intent.getExtras().getString("message");
    String newmessage = intent.getExtras().getString("extra");
    //Now display the message
    displayMessage(context, message + newmessage);
    generateNotification(context, message + newmessage);
}

【讨论】:

  • 是的,你必须从字段数组中获取键,它必须匹配
  • 你知道为什么这个 GCM 服务会在几分钟后发送通知吗? (延迟)
  • 我不确定,如果您只在一台设备上测试应该是即时的...generateNotification 方法有什么作用?
  • 它在设备上显示收到的消息,当用户点击该通知时,会打开特定的活动...
  • 应该不会超过一分钟。
【解决方案2】:

您在 JSON 中传递的所有有效负载参数都应该在 data 元素内。

您的 JSON 应如下所示:

{
  "registration_ids":["xxx", "yyy"],
  "data": {
    "price": "price value",
    "extra": "extra value"
  }
}

不是这样的:

{
  "registration_ids":["xxx", "yyy"],
  "message": {
    "price": "price value"
  },
  "data": {
    "extra": "extra value"
  }
}

【讨论】:

    【解决方案3】:

    您可以编写包含多条消息的 JSON 响应

    然后你在 android 中得到 JSON,解析它并得到你的多个消息。

    【讨论】:

      【解决方案4】:

      send_message.php

      <?php
      
      if (isset($_REQUEST["regId"]) && isset($_REQUEST["message"]) && isset($_REQUEST["extra"])) {
          $regId = $_REQUEST["regId"];
          $message = $_REQUEST["message"];
          $extra= $_REQUEST["extra"];
      
      
          include_once './GCM.php';
      
          $gcm = new GCM();
      
          $registatoin_ids = array($regId);
          $message = array("message" => $message, "extra" => $extra);
      
          $result = $gcm->send_notification($registatoin_ids, $message);
      
      
      }
      

      GCM.php

      <?php
      
      class GCM {
      
          function __construct() {
      
          }
      
          public function send_notification($registatoin_ids, $message) {
              // include config
              include_once './config.php';
      
      
              // Set POST variables
              $url = 'https://android.googleapis.com/gcm/send';
      
              $fields = array(
      
              'registration_ids' => $registatoin_ids,
                  'data' => $message, 
              );
      
      
              $headers = array(
                  'Authorization: key=' . GOOGLE_API_KEY,
                  'Content-Type: application/json'
              );
      
              //print_r($headers); exit();
              // 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);
      
              // Disabling SSL Certificate support temporarly
              curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
      
              curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($fields));
      
              // Execute post
              $result = curl_exec($ch);
      
              if ($result === FALSE) {
                  die('Curl failed: ' . curl_error($ch));
              }
      
              // Close connection
              curl_close($ch);
              echo 'result';
              echo json_encode($fields);
              echo $result;
          }
      
      }
      

      安卓端

      /**
           * Method called on Receiving a new message
           * */
          @Override
          protected void onMessage(Context context, Intent intent) {
              Log.i(TAG, "Received message");
              String message =  intent.getExtras().getString("extra");
              String newmessage = intent.getExtras().getString("message");
              displayMessage(context, message + newmessage);
              generateNotification(context, message + newmessage);
          }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多