laravel-notification-channels / fcm

Firebase Cloud Messaging (FCM) notifications channel for Laravel
https://laravel-notification-channels.com/
MIT License
491 stars 126 forks source link

Getting Started with fcm #82

Closed acedesigns closed 2 years ago

acedesigns commented 3 years ago

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;
    }
}
seanlabs commented 3 years ago

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')));
}
TristanWeij commented 3 years ago

@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,

rizqi-perdana commented 3 years ago

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

TristanWeij commented 3 years ago

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