0

I try push notification to android device with PHP. When i insert a text into text field and send it to android device. It sent success but no content in message on device.Why? And how do i fix it? This is my PHP code:

<?php
include('config/dbconnect_log.php');
if(isset($_POST['submit'])){
function sendPushNotification($registration_ids, $message) {

    $url = 'https://android.googleapis.com/gcm/send';
    $fields = array(
        'registration_ids' => $registration_ids,
        'data' => $message,
    );

    define('GOOGLE_API_KEY', 'my google api key');

    $headers = array(
        'Authorization:key=' . GOOGLE_API_KEY,
        'Content-Type: application/json'
    );
    echo json_encode($fields);
    $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_SSL_VERIFYPEER, false);
    curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($fields));

    $result = curl_exec($ch);
    if($result === false)
        die('Curl failed ' . curl_error());

    curl_close($ch);
    return $result;

}

$pushStatus = '';

    $query = "SELECT DISTINCT googleid FROM ggevent";
    if($query_run = mysql_query($query)) {

        $gcmRegIds = array();
        $i = 0;
        while($query_row = mysql_fetch_assoc($query_run)) {
            $i++;
            $gcmRegIds[floor($i/1000)][] = $query_row['googleid'];
        }

    }
    $pushMessage = $_POST['message'];
    if(isset($gcmRegIds) && isset($pushMessage)) {

        $message = array('msg' => $pushMessage);
        $pushStatus = array();
        foreach($gcmRegIds as $val) $pushStatus[] = sendPushNotification($val, $message);

    }   

}
?>

<html>
    <head>

    </head>
    <body>
  <article class="module width_full">
    <form method = 'POST' action = ''>
        <fieldset>
            <label>Content</label>
            <textarea rows = 2 name = "message"  placeholder = 'Messages to Transmit via GCM'></textarea>
        </fieldset>
        <div class="clear"></div>
                </div>
            <footer>
                <div class="submit_link">
                    <input type="submit" name='submit' value="Send Push Notification" class="alt_btn">
                </div>
            </footer>
    </form>
    </article>

    </body>
</html>
mrdragon
  • 247
  • 1
  • 2
  • 14

1 Answers1

0

First register the device to GCM and get the gcm key. Send the notification to that device manually using the GCM key. Curl request to do so is

curl --header "Authorization: key=" --header Content-Type:"application/json" https://android.googleapis.com/gcm/send -XPOST -d '{"data":{"New message through curl"},"registration_ids":["APA91bGe-Ihcjil6YXgLNEtf1fZCvcrgcn35rTOsHPrK7mxoz70TP6jmniChLXmIbLd642CWGo0fem9XrECa2ukvAvNYfNY7HcJ0JcxTInBxNn5Pml9YQIU6qwRVQ4v8GIE4oZv4vjoc"]}'

If you get the notification using this then your device has been registered successfully

For more info and detailed working classed for test go through following link

GCM with PHP (Google Cloud Messaging)

Community
  • 1
  • 1
Santosh Jagtap
  • 995
  • 8
  • 17