dongle19863 2014-01-21 07:06
浏览 27
已采纳

使用GCM发送多条消息

I've been dealing with this issue since 2 days. What I want to do is, I've to send multiple messages to registered GCM device. Till now I can send single message to device. Below is the code to send message.

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;
    }
}
?>

Here message is being received on receiver side.. // code for this is in GCM.php

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

But I want to send multiple messages in single notification. for that what I did...

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

?>

And in android I wrote the function to receive message like this...

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

But I'm getting null result for "price" and getting result for "extra". How can I receive multiple message string in single notification?

  • 写回答

4条回答 默认 最新

  • doushadu0901 2014-01-21 07:16
    关注

    In send_message.php

    Put both messages in the same object. I don't know why you call it price, but try like this:

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

    In GCM.php

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

    In your Android Service

    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);
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥15 有人会SIRIUS 5.8.0这个软件吗
  • ¥30 comsol仿真等离激元
  • ¥15 静电纺丝煅烧后如何得到柔性纤维
  • ¥15 (标签-react native|关键词-镜像源)
  • ¥100 照片生成3D人脸视频
  • ¥15 伪装视频时长问题修改MP4的时长问题,
  • ¥15 JETSON NANO
  • ¥15 VS开发qt时如何在paintgl函数中用pushbutton控制切换纹理
  • ¥20 关于 openpyxl 处理excel文件地问题
  • ¥15 MS中不知道高分子的构型怎么构建模型