In this tutorial you will learn about the Laravel 8 Push Notification to Android and IOS Tutorial and its application with practical example.
In this Laravel 8 Push Notification to Android and IOS Example tutorial, I’ll show you how to send push notification to android and Ios in laravel. In this tutorial you will learn send push notification in laravel 8 project. In this example we will create a custom helper function to send push notification. In this article I will share example to send push notification in laravel 8 application. In this step by step guide I’ll demonstrate you the process to send push notification in laravel 8.
Laravel 8 Push Notification to Android and IOS Tutorial
- Step 1 –Create a Helper File
- Step 2 –Add Fcm Key in .env
- Step 3 –Send Notification.
Step 1: Create a Helper File
In this step we will create a helper file. Go to App directory and inside this directory create a new file new helpers.php. Now put the following code into your helpers.php file as follow:
app/helpers.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 |
<?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
In this step we will add fcm key in laravel project’s .env file. Go to your project root directory and open .env file. Then update fcm key as follow:
1 |
FCM_KEY = |
Now, you can use your google fcm key as follow
1 2 |
1 $accesstoken = env('FCM_KEY'); |
Step 3: Send Notification
In this step, open your controller file and you can call send_notification_FCM($notification_id, $title, $message, $id,$type) from helpers.php with following parameters.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
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. Now when you want to send push notification you need to call the send_notification_FCM() with parameters as specified.