Skip to content

Instantly share code, notes, and snippets.

@laptrinhcomvn
Last active February 26, 2018 18:19
Show Gist options
  • Select an option

  • Save laptrinhcomvn/d8e8e5ee396ff01dbc861646e9f4ada4 to your computer and use it in GitHub Desktop.

Select an option

Save laptrinhcomvn/d8e8e5ee396ff01dbc861646e9f4ada4 to your computer and use it in GitHub Desktop.

Revisions

  1. laptrinhcomvn revised this gist Feb 26, 2018. 1 changed file with 9 additions and 0 deletions.
    9 changes: 9 additions & 0 deletions send-fcm-pushnotification.php
    Original file line number Diff line number Diff line change
    @@ -10,6 +10,15 @@
    'vibrate' => 1,
    'sound' => 1,
    );

    // custom sound should be existing in res/raw/notification.mp3
    $msg = array
    (
    'body' => $_GET['body'],
    'title' => $_GET['title'],
    'vibrate' => 1,
    'sound' => 'notification',
    );
    $fields = array
    (
    'registration_ids' => $registrationIds,
  2. laptrinhcomvn created this gist Feb 26, 2018.
    35 changes: 35 additions & 0 deletions send-fcm-pushnotification.php
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,35 @@
    <?php
    // API access key from Google API's Console; change to cloud messaging tab to get the server token
    define( 'API_ACCESS_KEY', 'YOUR_FIREBASE_API_ACCESS_KEY' );
    $registrationIds = array( $_GET['id'] );
    // prep the bundle
    $msg = array
    (
    'body' => $_GET['body'],
    'title' => $_GET['title'],
    'vibrate' => 1,
    'sound' => 1,
    );
    $fields = array
    (
    'registration_ids' => $registrationIds,
    'notification' => $msg
    );

    $headers = array
    (
    'Authorization: key=' . API_ACCESS_KEY,
    'Content-Type: application/json'
    );

    $ch = curl_init();
    curl_setopt( $ch,CURLOPT_URL, 'https://fcm.googleapis.com/fcm/send' );
    curl_setopt( $ch,CURLOPT_POST, true );
    curl_setopt( $ch,CURLOPT_HTTPHEADER, $headers );
    curl_setopt( $ch,CURLOPT_RETURNTRANSFER, true );
    curl_setopt( $ch,CURLOPT_SSL_VERIFYPEER, false );
    curl_setopt( $ch,CURLOPT_POSTFIELDS, json_encode( $fields ) );
    $result = curl_exec($ch );
    curl_close( $ch );
    echo $result;
    ?>