Laravel 8 Push Notification to Android and IOS Tutorial

Laravel 8 Push Notification to Android and IOS Tutorial

Laravel 8 send push notification tutorial. In this tutorial, we will show you how to send push notification to android and ios mobiles in laravel 8 app. And How to Send Push Notifications in Laravel 8 using Firebase.

Sending push notifications to mobile devices are very easy things with laravel. You can use send push notification in android and ios device without using any laravel package.

This tutorial will guide you on how to send push notification in laravel with google FCM using CURL request and helper.

This tutorial uses CURL request for calling fcm api to send push notification on mobile devices, and custom helper, where you can define a send notification function. And last google FCM key.

Prerequisite

If you want to send push notification in laravel app without using package. So you need to one thing only that is google FCM.

Send Push Notification on Android and IOS using Google Firebase FCM in Laravel 8 Apps

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 8

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

Go to your 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 Notification

Next step, open your controller file and you can call send_notification_FCM($notification_id, $title, $message, $id,$type) from helpers.php with following 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
   }
   

}

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

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

AuthorAdmin

My name is Devendra Dode. I am a full-stack developer, entrepreneur, and owner of Tutsmake.com. I like writing tutorials and tips that can help other developers. I share tutorials of PHP, Python, Javascript, JQuery, Laravel, Livewire, Codeigniter, Node JS, Express JS, Vue JS, Angular JS, React Js, MySQL, MongoDB, REST APIs, Windows, Xampp, Linux, Ubuntu, Amazon AWS, Composer, SEO, WordPress, SSL and Bootstrap from a starting stage. As well as demo example.

6 replies to Laravel 8 Push Notification to Android and IOS Tutorial

  1. could you please tell which packages do i need to require

    • In this tutorial, push notifications have been sent without using any package.

  2. This code will also work in website?

  3. Nice post!

    But I am blocked at $notification_id. Where do I get $notification_id from?

    Thank you!

    • You will get the notification id from apis. Which will provide your iPhone and Android team member.

Leave a Reply

Your email address will not be published. Required fields are marked *