fcm icon indicating copy to clipboard operation
fcm copied to clipboard

Getting Started with fcm

Open acedesigns opened this issue 3 years ago • 4 comments

Hi All.

So I ha a dashboard that I created with AdminLTE. So now I want to send a person a notification when I click on a button. This may be. a silly question/PR but I can not get it to work.

I am unable to figure out how to implement this to work. The ReadMe is not understandable to me

This is the function that runs when I click on that button

public function moveOrder( Request $request) {

        $exists = Order::where('id', $request->order_id)->exists();
        $userDeviceToken = User::select('device_token')->where('id', $request->user_id)->get();

        try {
            if ( $exists ) {
                $status = Order::where('id', $request->order_id)->first();

                if ( $status->status_id === 1 ) {
                    Order::where('id', $request->order_id)
                        ->update(['status_id' => 2, 'order_time' => $request->order_time]);
                    // Tell the user that the order is on its way, please be patient.
                    // $userDeviceToken <--- This is the device token that belongs to the user
                } elseif ( $status->status_id === 2 ) {
                    Order::where('id', $request->order_id)
                        ->update(['status_id' => 5]);

                    // Tell the user that the order is complete.
                    // $userDeviceToken <--- This is the device token that belongs to the user
                }
            }
            return redirect()->route('order.index')
                ->with(['flash_message_success', 'Order Was Successfully updated.' , 'response' => 200]);
        } catch ( \Exception $exception ) {
            $this->exceptionResponse($exception);
        }

    }

I was able to send and receive the notification when testing using Firebase on the browser

My User.php

class User extends Authenticatable
{
    use Notifiable;

    ....

    public function routeNotificationForFcm()
    {
        return $this->device_token;
    }
}

acedesigns avatar Mar 20 '21 10:03 acedesigns

You need to create a new Notification class that extends notification (i.e. AccountActivated). You can do this via php artisan make:notification <Class Name Here>

Then, in that class you have to implement the "via" method and "toFcm" method like below from the instructions. Then, once you have that setup, then you call the notify method on the User passing in the new Notification class like so... $user->notify(new AccountActivated);

++++++

public function via($notifiable) { return [FcmChannel::class]; }

public function toFcm($notifiable)
{
    return FcmMessage::create()
        ->setData(['data1' => 'value', 'data2' => 'value2'])
        ->setNotification(\NotificationChannels\Fcm\Resources\Notification::create()
            ->setTitle('Account Activated')
            ->setBody('Your account has been activated.')
            ->setImage('http://example.com/url-to-image-here.png'))
        ->setAndroid(
            AndroidConfig::create()
                ->setFcmOptions(AndroidFcmOptions::create()->setAnalyticsLabel('analytics'))
                ->setNotification(AndroidNotification::create()->setColor('#0A0A0A'))
        )->setApns(
            ApnsConfig::create()
                ->setFcmOptions(ApnsFcmOptions::create()->setAnalyticsLabel('analytics_ios')));
}

seanlabs avatar Mar 25 '21 15:03 seanlabs

@acedesigns Have you followed all the steps documented in the README? have you followed all the steps documented in: https://github.com/kreait/laravel-firebase?

Cheers,

TristanWeij avatar Jun 18 '21 18:06 TristanWeij

You need to create a new Notification class that extends notification (i.e. AccountActivated). You can do this via php artisan make:notification

Then, in that class you have to implement the "via" method and "toFcm" method like below from the instructions. Then, once you have that setup, then you call the notify method on the User passing in the new Notification class like so... $user->notify(new AccountActivated);

++++++

public function via($notifiable) { return [FcmChannel::class]; }

public function toFcm($notifiable)
{
    return FcmMessage::create()
        ->setData(['data1' => 'value', 'data2' => 'value2'])
        ->setNotification(\NotificationChannels\Fcm\Resources\Notification::create()
            ->setTitle('Account Activated')
            ->setBody('Your account has been activated.')
            ->setImage('http://example.com/url-to-image-here.png'))
        ->setAndroid(
            AndroidConfig::create()
                ->setFcmOptions(AndroidFcmOptions::create()->setAnalyticsLabel('analytics'))
                ->setNotification(AndroidNotification::create()->setColor('#0A0A0A'))
        )->setApns(
            ApnsConfig::create()
                ->setFcmOptions(ApnsFcmOptions::create()->setAnalyticsLabel('analytics_ios')));
}

Then how you define the recipient of the notification? still does not make sense to me

rizqi-perdana avatar Oct 14 '21 15:10 rizqi-perdana

You call the notification like so: $recipient->notify(new AccountActivated);. You do not have to define the recipient in the notification itself.

TristanWeij avatar Oct 14 '21 17:10 TristanWeij