This package makes it easy to send notifications using Facebook with Laravel 5.3.
You can install the package via composer:
composer require laravel-notification-channels/facebookYou must install the service provider:
// config/app.php 'providers' => [ NotificationChannels\Facebook\FacebookServiceProvider::class, ];Follow the Getting Started guide and get a page token.
Then, configure your Facebook Page Token:
// config/services.php 'facebook' => [ 'page-token' => env('FACEBOOK_PAGE_TOKEN', 'YOUR BOT TOKEN HERE') ]You can now use the channel in your via() method inside the Notification class.
use NotificationChannels\Facebook\FacebookChannel; use NotificationChannels\Facebook\FacebookMessage; use NotificationChannels\Facebook\Attachment\Button; use NotificationChannels\Facebook\NotificationType; use Illuminate\Notifications\Notification; class InvoicePaid extends Notification { public function via($notifiable) { return [FacebookChannel::class]; } public function toFacebook($notifiable) { $url = url('/invoice/' . $this->invoice->id); return FacebookMessage::create() ->to($this->user->fb_messenger_user_id) // Optional ->text('One of your invoices has been paid!') ->notificationType(NotificationType::REGULAR) // Optional ->buttons([ Button::create('View Invoice', $url)->isTypeWebUrl(), Button::create('Call Us for Support!', '+1(212)555-2368')->isTypePhoneNumber(), Button::create('Start Chatting', ['invoice_id' => $this->invoice->id])->isTypePostback() // Custom payload sent back to your server ]); // Buttons are optional as well. } }Here's a screenshot preview of the above notification on Facebook Messenger:
You can either send the notification by providing with the page-scoped user id (PSID) of the recipient to the to($userId) method like shown in the above example or add a routeNotificationForFacebook() method in your notifiable model:
... /** * Route notifications for the Facebook channel. * * @return int */ public function routeNotificationForFacebook() { return $this->fb_messenger_user_id; } ...to($userIdOrPhoneNumber): (string) Recipient's page-scoped User ID or Phone number of with the format+1(212)555-2368. NOTE: Sending a message to phone numbers requires thepages_messaging_phone_numberpermission. Refer docs for more information.text(''): (string) Notification message.buttons($buttons = []): (array) An array of "Call to Action" buttons (Created usingNotificationChannels\Facebook\Attachment\Button::create()). You can add up to 3 buttons of one of the following types:web_url,postbackorphone_number. See Button methods below for more details.notificationType(''): (string) Push Notification type:REGULARwill emit a sound/vibration and a phone notification;SILENT_PUSHwill just emit a phone notification,NO_PUSHwill not emit either. You can make use ofNotificationType::REGULAR,NotificationType::SILENT_PUSHandNotificationType::NO_PUSHto make it easier to work with the type. This is an optional method, defaults toREGULARtype.
title(''): (string) Button Title.data(''): (string) Button Data - It can be a web url, postback data or a formated phone number.type(''): (string) Button Type -web_url,postbackorphone_number.isTypeWebUrl(): Helper method to create aweb_urltype button.isTypePhoneNumber(): Helper method to create aphone_numbertype button.isTypePostback(): Helper method to create apostbacktype button.
Please see CONTRIBUTING for details.
The MIT License (MIT). Please see License File for more information.
