Skip to content

Instantly share code, notes, and snippets.

@blackcj
Last active March 4, 2016 14:49
Show Gist options
  • Select an option

  • Save blackcj/5976493 to your computer and use it in GitHub Desktop.

Select an option

Save blackcj/5976493 to your computer and use it in GitHub Desktop.

Revisions

  1. blackcj revised this gist Jul 11, 2013. 2 changed files with 12 additions and 14 deletions.
    21 changes: 10 additions & 11 deletions gcm_sender.html
    Original file line number Diff line number Diff line change
    @@ -1,19 +1,18 @@
    <!DOCTYPE html>
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
    <title>GCM Sender</title>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
    <title>GCM Sender</title>
    </head>

    <body>
    <h2>GCM Sender</h2>
    <form method="POST" name="input" action="gcm_sender.php" />
    Device Token:<input name="deviceToken" /> <br />
    API Key:<input name="apiKey" /> <br />
    Message:<input name="message" /> <br />
    Collapse Key:<input name="collapseKey" /> <br />
    <input type="submit" value="Send Message">
    </form>

    <h2>GCM Sender</h2>
    <form method="POST" name="input" action="gcm_sender.php" />
    Device Token:<input name="deviceToken" /> <br />
    API Key:<input name="apiKey" /> <br />
    Message:<input name="message" /> <br />
    Collapse Key:<input name="collapseKey" /> <br />
    <input type="submit" value="Send Message">
    </form>
    </body>
    </html>
    5 changes: 2 additions & 3 deletions gcm_sender.php
    Original file line number Diff line number Diff line change
    @@ -1,10 +1,10 @@
    <?php
    function sendMessageToPhone()
    {
    $deviceToken = $_POST['deviceToken']; // Returned from GCM when a user opens the app, device specific
    $deviceToken = $_POST['deviceToken']; // Returned from GCM when a user opens the app, device specific
    $messageText = $_POST['message']; // Message that will be send to the user
    $collapseKey = $_POST['collapseKey']; // Key used to group messages, for example '123'
    $yourKey = $_POST['apiKey']; // Retrieve from https://code.google.com/apis/console/
    $yourKey = $_POST['apiKey']; // Retrieve from https://code.google.com/apis/console/

    $headers = array('Authorization:key=' . $yourKey);
    $data = array(
    @@ -42,7 +42,6 @@ function sendMessageToPhone()
    }

    if (isset($_POST['deviceToken'])) {

    sendMessageToPhone();
    }

  2. blackcj revised this gist Jul 11, 2013. 2 changed files with 20 additions and 21 deletions.
    19 changes: 19 additions & 0 deletions gcm_sender.html
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,19 @@
    <!DOCTYPE html>
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
    <title>GCM Sender</title>
    </head>

    <body>
    <h2>GCM Sender</h2>
    <form method="POST" name="input" action="gcm_sender.php" />
    Device Token:<input name="deviceToken" /> <br />
    API Key:<input name="apiKey" /> <br />
    Message:<input name="message" /> <br />
    Collapse Key:<input name="collapseKey" /> <br />
    <input type="submit" value="Send Message">
    </form>

    </body>
    </html>
    22 changes: 1 addition & 21 deletions gcm_sender.php
    Original file line number Diff line number Diff line change
    @@ -46,24 +46,4 @@ function sendMessageToPhone()
    sendMessageToPhone();
    }

    ?>

    <!DOCTYPE html>
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
    <title>GCM Test</title>
    </head>

    <body>
    <h2>GCM Test</h2>
    <form method="POST" />
    Device Token:<input name="deviceToken" /> <br />
    API Key:<input name="apiKey" /> <br />
    Message:<input name="message" /> <br />
    Collapse Key:<input name="collapseKey" /> <br />
    <input type="submit" value="Send Message">
    </form>

    </body>
    </html>
    ?>
  3. blackcj revised this gist Jul 11, 2013. No changes.
  4. blackcj created this gist Jul 11, 2013.
    69 changes: 69 additions & 0 deletions gcm_sender.php
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,69 @@
    <?php
    function sendMessageToPhone()
    {
    $deviceToken = $_POST['deviceToken']; // Returned from GCM when a user opens the app, device specific
    $messageText = $_POST['message']; // Message that will be send to the user
    $collapseKey = $_POST['collapseKey']; // Key used to group messages, for example '123'
    $yourKey = $_POST['apiKey']; // Retrieve from https://code.google.com/apis/console/

    $headers = array('Authorization:key=' . $yourKey);
    $data = array(
    'to' => $deviceToken,
    'collapse_key' => $collapseKey,
    'data.message' => $messageText,
    'delay_while_idle' => false,
    'time_to_live' => 108,
    'message_id' => uniqid('nerd_'));

    $ch = curl_init();

    curl_setopt($ch, CURLOPT_URL, "https://android.googleapis.com/gcm/send");
    if ($headers)
    curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
    curl_setopt($ch, CURLOPT_POST, true);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $data);

    $response = curl_exec($ch);
    $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
    if (curl_errno($ch)) {
    //request failed
    return false;//probably you want to return false
    }
    if ($httpCode != 200) {
    //request failed
    return false;//probably you want to return false
    }
    curl_close($ch);
    $arr = array ('response'=>$response);
    echo json_encode($arr);
    return $response;
    }

    if (isset($_POST['deviceToken'])) {

    sendMessageToPhone();
    }

    ?>

    <!DOCTYPE html>
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
    <title>GCM Test</title>
    </head>

    <body>
    <h2>GCM Test</h2>
    <form method="POST" />
    Device Token:<input name="deviceToken" /> <br />
    API Key:<input name="apiKey" /> <br />
    Message:<input name="message" /> <br />
    Collapse Key:<input name="collapseKey" /> <br />
    <input type="submit" value="Send Message">
    </form>

    </body>
    </html>