+977-9860352183        [email protected]

Blog Detail

A Standard Blog Post

For this topic, I will cover how to send flutter push notification using Laravel .

 

follow the firebase console procedure and add google-service.json file in 

android>app>

 

In pubspec.yaml

firebase_messaging:

url_launcher:

In app>build.gradle

dependencies {

 *******************

    implementation platform('com.google.firebase:firebase-bom:26.4.0')

    implementation 'com.google.firebase:firebase-analytics'

}

apply plugin: 'com.android.application'

apply plugin: 'com.google.gms.google-services'

In build.gradle

dependencies {

       ******************

        classpath 'com.google.gms:google-services:4.3.5'

    }

 

import 'package:flutter/cupertino.dart';

import 'package:flutter/material.dart';

 

import 'home_page.dart';

 

void main() {

  runApp(MyApp());

}

 

class MyApp extends StatelessWidget {

  @override

  Widget build(BuildContext context) {

    return MaterialApp(

      debugShowCheckedModeBanner: false,

      title: 'Flutter Demo',

      theme: ThemeData(

        primaryColor: Colors.white,

      ),

      home: HomePage(),

    );

  }

}

In home_page.dart

import 'package:flutter/cupertino.dart';

import 'package:flutter/material.dart';

 

import 'home_page.dart';

 

void main() {

  runApp(MyApp());

}

 

class MyApp extends StatelessWidget {

  @override

  Widget build(BuildContext context) {

    return MaterialApp(

      debugShowCheckedModeBanner: false,

      title: 'Flutter Demo',

      theme: ThemeData(

        primaryColor: Colors.white,

      ),

      home: HomePage(),

    );

  }

}

 

in AndroidManifest.xml

<intent-filter>

    <action android:name="FLUTTER_NOTIFICATION_CLICK" />

    <category android:name="android.intent.category.DEFAULT" />

</intent-filter>

& test cloud messaging notification in firebase console

Now in Laravel

Create New Project

php artisan make:model PushNotification

for controller,

php artisan make:controller PushNotificationController  -mc

then setup database in .env file 

In migration of notification

Schema::create('push_notifications', function (Blueprint $table) {

            $table->id();

            $table->string('title');

            $table->string('body');

            $table->string('image');

            $table->timestamps();

        });

 

Run Database 

php artisan migrate

 

create blade file in view>notification>create.blade.php

<form action="{{route('bulksend')}}" method="post" enctype="multipart/form-data">

    @csrf

    <div class="form-group">

        <label for="exampleInputEmail1">Title</label>

        <input type="text" class="form-control" id="exampleInputEmail1" aria-describedby="emailHelp" placeholder="Enter Notification Title" name="title">

    </div>

    <div class="form-group">

        <label for="exampleInputEmail1">Message</label>

        <input type="text" class="form-control" id="exampleInputEmail1" aria-describedby="emailHelp" placeholder="Enter Notification Description" name="body" required>

    </div>

    <div class="form-group">

        <label for="exampleInputEmail1">Click Link</label>

        <input type="text" class="form-control" id="exampleInputEmail1" aria-describedby="emailHelp" placeholder="Enter Notification Description" name="link" required>

    </div>

    <div class="form-group">

        <label for="exampleInputEmail1">Image Url</label>

        <input type="text" class="form-control" id="exampleInputEmail1" aria-describedby="emailHelp" placeholder="Enter image link" name="img">

    </div>

    <button type="submit" class="btn btn-primary">Send Notification</button>

</form>

<script>

    function loadPhoto(event) {

        var reader = new FileReader();

        reader.onload = function () {

            var output = document.getElementById('photo');

            output.src = reader.result;

        };

        reader.readAsDataURL(event.target.files[0]);

    }

</script>

In PushNotificationController.php

<?php

 

namespace App\Http\Controllers;

 

use Illuminate\Http\Request;

use App\Models\PushNotification;

class PushNotificationController extends Controller

{

        /**

         * Display a listing of the resource.

         *

         * @return \Illuminate\Http\Response

         */

        public function index()

        {

            $push_notifications = PushNotification::orderBy('created_at', 'desc')->get();

            return view('notification.index', compact('push_notifications'));

        }

        public function bulksend(Request $req){

    

            $comment = new PushNotification();

            $comment->title = $req->input('title');

            $comment->body = $req->input('body');

            $comment->img = $req->input('img');

            $comment->save();

             

    

    

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

            $dataArr = array('click_action' => 'FLUTTER_NOTIFICATION_CLICK','url' =>$req->link, 'id' => $req->id,'status'=>"done",'title'=>$req->title);

            $notification = array('title' =>$req->title, 'text' => $req->body, 'image'=> $req->img, 'sound' => 'default', 'badge' => '1',);

            $arrayToSend = array('to' => "/topics/all", 'notification' => $notification, 'data' => $dataArr, 'priority'=>'high');

            $fields = json_encode ($arrayToSend);

            $headers = array (

                'Authorization: key=' . "YOUR_SERVER_KEY_FROM_FIREBASE_MESSAGING_CONSOLE",

                'Content-Type: application/json'

            );

    

            $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_RETURNTRANSFER, true );

            curl_setopt ( $ch, CURLOPT_POSTFIELDS, $fields );

    

            $result = curl_exec ( $ch );

            //var_dump($result);

            curl_close ( $ch );

            return redirect()->back()->with('success', 'Notification Send successfully');

    

        }

    

        /**

         * Show the form for creating a new resource.

         *

         * @return \Illuminate\Http\Response

         */

        public function create()

        {

            return view('notification.create');

        }

    

    

        /**

         * Remove the specified resource from storage.

         *

         * @param  \App\Models\PushNotification  $pushNotification

         * @return \Illuminate\Http\Response

         */

        public function destroy(PushNotification $pushNotification)

        {

            //

        }

   

}

 

 

In web.php

Route::post('send',[PushNotificationController::class, 'bulksend'])->name('bulksend');

Route::get('all-notifications', [PushNotificationController::class, 'index']);

Route::get('get-notification-form', [PushNotificationController::class, 'create']);