Getting Started

Summary: This post tell you to send push notification using php to android application.I am developing chat app which is based on Firebase Database and Storage. Everything is working fine, but now I need implementation of FCM to receive notification on app when ap

Sending a push notification is only a matter of sending a post request to FCM servers.

Here is working example:

send fcm notification using php flow

<?php

namespace App;

class Helpers
{

// firebase push notification funcation
public static function androidPush($device_token, $msg_type, $msg, $badge)
{

    $url = 'https://fcm.googleapis.com/fcm/send';
    $registrationIDs = array($device_token);
    $key = "AAAA1lwzVzs:APA91bHhqmtWiewGkegc45U40IsWjTaBkqH9w9I71VEGhVrk16cv4sG3jWW-F66wye0JWbdXEbu4tHPy3c4l7SKnhXseBmp7DGJbvFTnkrP21H2";
//Just replace this fcm key with your fcm key.

    $message = array("msg_type" => $msg_type, "msg" => $msg, "badge" => $badge);
    $headers = array(
        'Authorization: key=' . $key,
        'Content-Type: application/json'
    );

    $fields = array(
        'registration_ids' => $registrationIDs,
        'data' => $message,
    );
    $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_VERIFYHOST, 0);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($fields));
    $result = curl_exec($ch);
    if (curl_errno($ch)) {
        echo 'Curl error: ' . curl_error($ch);
    }
    curl_close($ch);
    return $result;
}
}

So how i call this function in my class it's just simple :

<?php

namespace App\Jobs;

class Jobs {
/**
 * Execute the job detail.
 *
 * @return void
 */
public function jobData()
{

$user_data = User::find($this->user_id);
Helpers::androidPush($user_data->device_token, 'job', 'Congratulations you are eligible', '0');

}

}
?>

Comments

Sign in to publish a comment

2 comments

Vishnu commented on
Oct 24, 2018, 8:06 PM

Thanks Senior Vikas Kumar Srivastava

Vikas Kumar Srivastava commented on
Oct 24, 2018, 10:30 AM

Good Job!!!