Skip to content
This repository has been archived by the owner on Sep 5, 2024. It is now read-only.

Added event support #100

Open
wants to merge 9 commits into
base: master
Choose a base branch
from
Open

Added event support #100

wants to merge 9 commits into from

Conversation

plokko
Copy link

@plokko plokko commented Feb 12, 2018

Added Laravel event triggering to handle FCM response events.
Due to non-uniformed response pattern only sendTo method will trigger those events but can be easly exended ( see LaravelFCM\Sender\FCMSender\dispatchEvents method implementation)

@plokko
Copy link
Author

plokko commented Feb 12, 2018

Writing event-based code is a lot better especially in big project because you can write all the logic of token update/create/delete in one place and don't repeat it everywhere but just do what the function is supposed to do.

A short example of a real listener:

<?php
namespace App\Listeners;

use App\Device;
use App\DeviceRegistrationRequest;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Contracts\Queue\ShouldQueue;
use LaravelFCM\Events\UpdateToken;

class UpdateFCMTokenListener
{
    /**
     * Create the event listener.
     *
     * @return void
     */
    public function __construct()
    {
        //
    }

    /**
     * Handle the event.
     *
     * @param  UpdateToken $event
     * @return void
     */
    public function handle(UpdateToken $event)
    {
        $devices = Device::where('fcm_token',$event->originalToken)->get();
        foreach($devices AS $device){
            /**@var \App\Device $device**/
            $device->update(['fcm_token'=>$event->newToken]);
        }

        $requests = DeviceRegistrationRequest::where('fcm_token',$event->originalToken)->get();
        foreach($requests AS $request){
            /**@var \App\DeviceRegistrationRequest $request**/
            $request->update(['fcm_token'=>$event->newToken]);
        }
    }
}

@francislavoie
Copy link

francislavoie commented Mar 9, 2018

You have mixed tabs and spaces, messy syntax (spaces around operators are missing in some spots). Needs cleanup. Please try to copy the syntax the project already uses.

@plokko
Copy link
Author

plokko commented Mar 9, 2018

@francislavoie no worries, in the time it took to have at least a reply from someone to my fixes i already implemented the FCM v1 API AND Firebase database API from scratch, including a Laravel package that uses it...

@williamdes
Copy link

Hi !
Feel free to re-open this pull-request to code-lts/Laravel-FCM

This is a fork that will be maintained as long as possible by @code-lts members 💪

🚀 1.4.0 was released See how to install it (composer require code-lts/laravel-fcm)

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants