Laravel 5.6 – Google Firebase Notification In Android

Laravel 5.6 – Google Firebase Notification In Android

In this post we will give you information about Laravel 5.6 – Google Firebase Notification In Android. Hear we will give you detail about Laravel 5.6 – Google Firebase Notification In AndroidAnd how to use it also give you demo for it if it is necessary.

Today, We are going to share with you guys how to send push notification in android with google fire-base using laravel. Sometime ago we have worked on one android application application managed in laravel. so, we need to fire a push notification on every android device when admin insert any new product from laravel application. so, we are done this push notification task with google firebase and google firebase is awesome for send push notification and other more functionality. today we are share with you how to work with google firebase for send push notification in android devise.


Simply foolow this step and done your push notification in android with google firebase using laravel backend.



What we done in this article:


We are create one simple post table and when admin insert any post then automatic fire notication on register user’s mobile by firebase token. so first you must be also get google firebase token from users android mobile by your android app.


Create New Project In Firebase:


First we need to create one fresh project in google firebase. you can create your project click by this link Google Firebase Console


How to crete your first project in google firebase please visite our this link How to create google firebase project.


After create your google firebase project you must be get following things from your created project.


1.) api_key


2.) auth_domain


3.) database_url


4.) secret


5.) Legacy server key


This all things you got from your google firebase project easily


Create post table migration:


Now, we are create one post table migration run by following command




php artisan make:migration create_post_table	
	


After run this command in your database/migration folder. so, open it and copy past following in this file.




use IlluminateSupportFacadesSchema;
use IlluminateDatabaseSchemaBlueprint;
use IlluminateDatabaseMigrationsMigration;

class CreateUsersTable extends Migration
{
    /**
     * Run the migrations.
     * @return void
     */
    public function up()
    {
        Schema::create('post', function (Blueprint $table) {
            $table->increments('id');
            $table->string('title');
            $table->text('description');
            $table->timestamps();
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::dropIfExists('post');
    }
}



Then run your post table migration by run following command.




php artisan migrate
	


Create Route:


Now create folloeing resource route in routes/web.php file.




Route::resource('post', 'PostController');



Create PostController:


Now create PostController.php in app/Http/Controllers/ folder and write following in store method so when your post create then Notification automaticly fire of users mobile by google firebase




namespace AppHttpControllers;

use AppHttpRequests;
use IlluminateHttpRequest;
use AppPost;

class PostController extends HomeController
{
    public function __construct()
    {
        parent::__construct();

        $this->Post = new Post;
    }
   
    public function store(Request $request)
    {
        $this->validate($request, [
            'title' => 'required|max:255',
            'description' => 'required',
        ]);

        $input = array_except($request->all(),array('_token'));

        $this->Post->AddData($input);

        $notification = DB::table('api_users')->get();

        foreach ($notification as $key => $value) {
            $this->notification($value->token, $request->get('title'));
        }

        Session::put('success','Post store and send notification successfully!!');

        return redirect()->route('post.index');
    }

    public function notification($token, $title)
    {
        $fcmUrl = 'https://fcm.googleapis.com/fcm/send';
        $token=$token;

        $notification = [
            'title' => $title,
            'sound' => true,
        ];
        
        $extraNotificationData = ["message" => $notification,"moredata" =>'dd'];

        $fcmNotification = [
            //'registration_ids' => $tokenList, //multple token array
            'to'        => $token, //single token
            'notification' => $notification,
            'data' => $extraNotificationData
        ];

        $headers = [
            'Authorization: key=Legacy server key',
            'Content-Type: applicationhttps://onlinecode.org/json'
        ];


        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL,$fcmUrl);
        curl_setopt($ch, CURLOPT_POST, true);
        curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($ch, CURLOPT_POSTFIELDS, json_en($fcmNotification));
        $result = curl_exec($ch);
        curl_close($ch);

        return true;
    }
}
	


In this store method you cann look we are get token from api_users table so we should be first get google firebase token from your android side app and that token store in your database. so, you can fire easily notification for all user.


If you can test notification manualy then you can use following for testing for one mobile devise.




public function test()
{
    $fcmUrl = 'https://fcm.googleapis.com/fcm/send';
    $token='diWhHpEdy1k:APA91bHfaE_zy4FUJ_GGDmO3XuJNz5qshyMeyjbIvvdLKI-DkR5rzhS00k9Hwc49yKzJLUraUPbu9-H-XOv8hbT-q-omtzXa8-uAv8Ewej52zO1gH0maKoGP4FLCu9FwVlLSpwBDC_3T';
    

    $notification = [
        'body' => 'this is test',
        'sound' => true,
    ];
    
    $extraNotificationData = ["message" => $notification,"moredata" =>'dd'];

    $fcmNotification = [
        //'registration_ids' => $tokenList, //multple token array
        'to'        => $token, //single token
        'notification' => $notification,
        'data' => $extraNotificationData
    ];

    $headers = [
        'Authorization: key=Legacy server key',
        'Content-Type: applicationhttps://onlinecode.org/json'
    ];


    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL,$fcmUrl);
    curl_setopt($ch, CURLOPT_POST, true);
    curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
    curl_setopt($ch, CURLOPT_POSTFIELDS, json_en($fcmNotification));
    $result = curl_exec($ch);
    curl_close($ch);


    dd($result);
}	
	


Legacy server key you can seen in your firebase setting then go in secound cloud messaging tab


We are hope you like this tutorials, if any question regarding any query please post your question in our forums click on bellow link Laravel’s Forums

Hope this and post will helped you for implement Laravel 5.6 – Google Firebase Notification In Android. if you need any help or any feedback give it in comment section or you have good idea about this post you can give it comment section. Your comment will help us for help you more and improve us. we will give you this type of more interesting post in featured also so, For more interesting post and Keep reading our blogs

For More Info See :: laravel And github

Leave a Comment

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

  +  7  =  15

We're accepting well-written guest posts and this is a great opportunity to collaborate : Contact US