【问题标题】:Android GCM send multiple message to deviceAndroid GCM 向设备发送多条消息
【发布时间】:2014-03-02 17:33:52
【问题描述】:

我有点卡在一个问题上。我需要向设备发送消息通知和警报通知。当用户向其他用户发送消息时发送消息通知,并生成警报通知以从服务器提醒用户。这意味着我需要多种通知类型。我在我的服务器上使用它来向我的设备推送通知。

// This is code from the class that take user input
$notify = "This is message"
$msg = array("Message" => $notify);
$sense = $gcm->send_notification($registatoin_ids, $msg);

//This is the send notification in GCM class
public function send_notification($registatoin_ids, $message) {
    // 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'
    );
    // 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;
}

如何调整上述代码以满足我的要求。我尝试发送

$fields = array(

    "registration_ids" : registatoin_ids,
    "data" : {
        "type" : "Message",
        "Message" : $message,
    }

     );

但我得到了错误

 E/JSON(3444): <b>Parse error</b>:  syntax error, unexpected ':', expecting ')' in <b>/home/a8709494/public_html/mobile/GCM.php</b> on line <b>25</b><br />

【问题讨论】:

    标签: php android google-cloud-messaging


    【解决方案1】:

    您需要为此使用=&gt; 运算符:

    $fields = array(
      "registration_ids" => "registatoin_ids",
      "data" => array(
        "type" => "Message",
        "Message" => $message,
      )
    );
    

    【讨论】:

    • 谢谢它有效,但我需要将'registration_ids' 放在单引号上,我再次收到Received: Bundle[{android.support.content.wakelockid=1, collapse_key=do_not_collapse, from=xxxxxxxxxxxx, type=Message, Message={"Message":"From : Me to you."}}] 的消息,为什么它不能使用双引号。以及我现在如何在客户端处理这个字符串。我将 GCMIntentService 实施为private void sendNotification(String msg),我得到msg = ""Message":"From : Me to you."}"
    • 您可以使用您已经使用过的相同技术:定义几个嵌套数组来实现这一点。如果您传递变量,单引号将不会进行替换,而双引号将扩展变量的值。一旦你实现了你的目标,无论是声誉还是反馈,都不要忘记接受对你最有帮助的答案。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-11-10
    • 2016-10-22
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多