• Send Push Notification to Android Using Firebase

    Create API Route

    We need to create a POST request api route which will be hit by android device. Open up the file /routes/api.php and copy the given code into and paste it.


    //..Other routes

    Route::post('send-notification', [App\Http\Controllers\NotificationController::class, 'send']);

    Create Controller & Add Method

    Let’s create a notification controller into application. So to create need to run this artisan command.

    $ php artisan make:controller NotificationController

    After running this command, you will find a controller created at /app/Http/Controllers/NotificationController.php

    Open up the file and paste the give code.

    <?php
      
    namespace App\Http\Controllers;
      
    use Illuminate\Http\Request;
    use App\Models\User;
      
    class NotificationController extends Controller
    {
        /**
         * Create a new controller instance.
         *
         * @return void
         */
        public function send(Request $request)
        {
            return $this->sendNotification($request->device_token, array(
              "title" => "Sample Message", 
              "body" => "This is Test message body"
            ));
        }
      
        /**
         * Write code on Method
         *
         * @return response()
         */
        public function sendNotification($device_token, $message)
        {
            $SERVER_API_KEY = '<YOUR-SERVER-API-KEY>';
      
            // payload data, it will vary according to requirement
            $data = [
                "to" => $device_token, // for single device id
                "notification" => $message
    ]; $dataString = json_encode($data); $headers = [ 'Authorization: key=' . $SERVER_API_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_SSL_VERIFYPEER, false); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_POSTFIELDS, $dataString); $response = curl_exec($ch); curl_close($ch); return $response; } }

    The above code will send notification to a single device ID which we will pass into request.

    Let’s say that we want to send notifications to multiple devices

    So how will we send ?

    NotificationController.php
    <?php
      
    namespace App\Http\Controllers;
      
    use Illuminate\Http\Request;
    use App\Models\User;
      
    class NotificationController extends Controller
    {
        /**
         * Create a new controller instance.
         *
         * @return void
         */
        public function send(Request $request)
        {
            return $this->sendNotification(array(
              $request->device_token1, 
              $request->device_token2,
              $request->device_token3
              //..
            ), array(
              "title" => "Sample Message", 
              "body" => "This is Test message body"
            ));
        }
      
        /**
         * Write code on Method
         *
         * @return response()
         */
        public function sendNotification($device_tokens, $message)
        {
            $SERVER_API_KEY = '<YOUR-SERVER-API-KEY>';
      
            // payload data, it will vary according to requirement
            $data = [
                "registration_ids" => $device_tokens, // for multiple device ids
                "data" => $message
            ];
            $dataString = json_encode($data);
        
            $headers = [
                'Authorization: key=' . $SERVER_API_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_SSL_VERIFYPEER, false);
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
            curl_setopt($ch, CURLOPT_POSTFIELDS, $dataString);
                   
            $response = curl_exec($ch);
          
            curl_close($ch);
          
            return $response;
        }
    }
    $data = [
        "to" => $device_token, // for single device id
        "data" => $message
    ];

    And for multiple devices, we have the key as registration_ids. If we want use this registration_ids to send only to a single device, it also works. Only we need to pass a single device token.

    $data = [
        "registration_ids" => $device_tokens, // for multiple device ids
        "data" => $message
    ];

    We hope this article helped you to learn about Laravel 8 Send Push Notification to Android Using Firebase 

  • 0 comments:

    Post a Comment

    FAVOURITE LINE

    To steal ideas from one is plagiarism. To steal from many is Research.

    ADDRESS

    Mumbai , Maharashtra

    EMAIL

    shikha.pathak6@gmail.com
    shikha.the.swt.pari@gmail.com

    Skype

    shikha_pari