Thursday, December 26, 2024
Google search engine
HomeLanguagesLaravel 10 Firebase Push Notification to Android and IOS

Laravel 10 Firebase Push Notification to Android and IOS

If you want to send push notification to android and ios mobile devices using your Laravel web application with firebase. So for this, you must have fcm key of google firebase. Then through this, you can.

Send push notifications to android and ios mobile in laravel. In this tutorial, you will learn how to send push notifications to android and ios mobiles using google firebase in Laravel 10 app.

Laravel 10 Push Notification to Android and IOS Google Firebase

By using the following steps, you can send push notifications to Android and ios firebase from laravel apps:

  • Step 1: Create a Helper File
  • Step 2: Add Fcm Key in .env
  • Step 3: Send Puth Notification to Android and IOS Mobile

Step 1: Create a Helper File

First of all, Navigate to App directory and inside this directory create a new file new helpers.php.

You can check checkout this => how to create helpers in Laravel 10

Then update the following code into your helpers.php file as follow:

app/helpers.php

<?php
 
  function send_notification_FCM($notification_id, $title, $message, $id,$type) {

    $accesstoken = env('FCM_KEY');

    $URL = 'https://fcm.googleapis.com/fcm/send';


        $post_data = '{
            "to" : "' . $notification_id . '",
            "data" : {
              "body" : "",
              "title" : "' . $title . '",
              "type" : "' . $type . '",
              "id" : "' . $id . '",
              "message" : "' . $message . '",
            },
            "notification" : {
                 "body" : "' . $message . '",
                 "title" : "' . $title . '",
                  "type" : "' . $type . '",
                 "id" : "' . $id . '",
                 "message" : "' . $message . '",
                "icon" : "new",
                "sound" : "default"
                },

          }';
        // print_r($post_data);die;

    $crl = curl_init();

    $headr = array();
    $headr[] = 'Content-type: application/json';
    $headr[] = 'Authorization: ' . $accesstoken;
    curl_setopt($crl, CURLOPT_SSL_VERIFYPEER, false);

    curl_setopt($crl, CURLOPT_URL, $URL);
    curl_setopt($crl, CURLOPT_HTTPHEADER, $headr);

    curl_setopt($crl, CURLOPT_POST, true);
    curl_setopt($crl, CURLOPT_POSTFIELDS, $post_data);
    curl_setopt($crl, CURLOPT_RETURNTRANSFER, true);

    $rest = curl_exec($crl);

    if ($rest === false) {
        // throw new Exception('Curl error: ' . curl_error($crl));
        //print_r('Curl error: ' . curl_error($crl));
        $result_noti = 0;
    } else {

        $result_noti = 1;
    }

    //curl_close($crl);
    //print_r($result_noti);die;
    return $result_noti;
}

Step 2: Add Fcm Key in .env

Once you have created send push notification function. Now, you need to visit project root directory and open .env file. Then update fcm key as follow:

FCM_KEY =

Now, you can use your google fcm key as follow

$accesstoken = env('FCM_KEY');

Step 3: Send Puth Notification to Android and IOS Mobile

Next step, open your controller file and you can call send_notification_FCM($notification_id, $title, $message, $id,$type) from the helpers.php file using the following code and parameters.

public function notifyUser(Request $request){

   $user = User::where('id', $request->id)->first();

   $notification_id = $user->notification_id;
   $title = "Greeting Notification";
   $message = "Have good day!";
   $id = $user->id;
   $type = "basic";

   $res = send_notification_FCM($notification_id, $title, $message, $id,$type);

   if($res == 1){

      // success code

   }else{

     // fail code
   }
   

}

Note that:- When you want to send push notification to the mobile device, that time you need to call the send_notification_FCM() with specified parameters like $notification_id, $title, $message, $id,$type..

Conclusion

In send push notification in laravel tutorial, you have learned how to send Push notification fcm using CURL and Helper without using package in laravel.

Recommended Laravel Posts

RELATED ARTICLES

Most Popular

Recent Comments