Skip to content

Instantly share code, notes, and snippets.

@iespino00
Created August 30, 2017 22:47

Revisions

  1. iespino00 revised this gist Aug 30, 2017. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion Push Notification
    Original file line number Diff line number Diff line change
    @@ -34,7 +34,7 @@
    return $result;
    }

    $conn = mysqli_connect("localhost","root","Password1","fcm");
    $conn = mysqli_connect("localhost","root","Password","fcm");
    $sql = "select token from users";
    $result = mysqli_query($conn,$sql);
    $tokens = array();
  2. iespino00 created this gist Aug 30, 2017.
    58 changes: 58 additions & 0 deletions Push Notification
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,58 @@
    <?php

    function send_notification($tokens, $message)
    {
    $url='https://fcm.googleapis.com/fcm/send';

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


    $headers = array(
    'Authorization:key = AOyUEjHurDZNKM-D7hcGG7wnSwSEY_ngA4P-w',
    'Content-Type: application/json'
    );

    $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_RETURNTRANSFER, true);
    curl_setopt($ch,CURLOPT_SSL_VERIFYHOST, 0);
    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($ch));
    }
    curl_close($ch);
    return $result;
    }

    $conn = mysqli_connect("localhost","root","Password1","fcm");
    $sql = "select token from users";
    $result = mysqli_query($conn,$sql);
    $tokens = array();

    if(mysqli_num_rows($result) > 0)
    {
    while ($row = mysqli_fetch_assoc($result))
    {
    $tokens[] = $row["Token"];
    }

    }

    mysqli_close($conn);

    $message = array("message" => "FCM PUSH NOTIFICATION TEST");
    $message_status = send_notification($tokens, $message);

    echo $message_status;

    ?>