
One of the many powerful features of the Laravel is the Laravel Notification introduced in the Laravel 5.3. This notification feature is fully available in the later versions of the framework. Laravel notification can be used to send E-Mail, send SMS (via Nexmo), and Slack Notifications. In addition to that Laravel notifications can be stored in the Database as well, so you can display them anywhere in your application. Now in the blog, we will be using FacebookPoster Notification to automatically post to the Facebook page.
I will give you a full overview of the available methods and code snippet so that you can understand it. Out of many notification channels
Package Installation
To install the package via composer. Open your command prompt and go to your desired project folder and type the following command.
1 |
composer require laravel-notification-channels/facebook-poster |
If you are below any version of Laravel below 5.5 you need to register the service provider class to the config/app.php file
1 2 3 4 5 6 |
... 'providers' => [ ... NotificationChannels\FacebookPoster\FacebookPosterServiceProvider::class, ], ... |
Now you need to go to your facebook page to get the App Id and App Secret Key in order to start using this notification channel.After this place them in your .env file. In order to load them, add this to your config/services.php
file:
1 2 3 4 5 6 7 |
... 'facebook_poster' => [ 'app_id' => getenv('FACEBOOK_APP_ID'), 'app_secret' => getenv('FACEBOOK_APP_SECRET'), 'access_token' => getenv('FACEBOOK_ACCESS_TOKEN'), ] ... |
Read Also :- Turbolinks js- Make Naviagtion In Web Application Faster
Setup Model
Now we are going to set up our Model and we will use the Notifiable trait in the Model class so that we can use Notifications.
1 2 3 4 5 6 7 8 9 |
use Illuminate\Notifications\Notifiable; /** * Class Article * @package App */ class Article extends Model { use Notifiable; |
Now next is the creation of the Notification using the following command.
1 |
php artisan make:notification ArticlesPublished |
Follow the path app\Notifications\ArticlesPublished.php and change the via() method of the Notification class with FacebookPoster method as shown below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
<?php namespace App\Notifications; use App\Article; use Illuminate\Bus\Queueable; use Illuminate\Notifications\Notification; use NotificationChannels\FacebookPoster\FacebookPosterChannel; use NotificationChannels\FacebookPoster\FacebookPosterPost; /** * Class ArticlesPublished * @package App\Notifications */ class ArticlesPublished extends Notification { use Queueable; /** * Get the notification's delivery channels. * * @param mixed $notifiable * @return array */ public function via($notifiable) { return [FacebookPosterChannel::class]; } /** * @param $article */ public function toFacebookPoster($article) { return with(new FacebookPosterPost($article->title)) ->withLink($article->getLink()); } } |
In the above code, we have published the title of the article with lits link.
It is possible to publish link with your post too. You just have to pass the url to the withLink
method.
Publish Facebook post with image
To publish the Facebook post with the image just add the ->withImage($article->imageurl); in place of the withLink() method above as shown below.
1 2 3 |
public function toFacebookPoster($notifiable) { return (new FacebookPosterPost($article->title))->withImage($article->imageurl); } |
Publish Facebook scheduled post
To use the facebook’s schedule post feature you just need to add the UNIX timestamp in your method with the time you want the post to be published as shown below.
1 2 3 4 |
public function toFacebookPoster($notifiable) { return (new FacebookPosterPost($article->title)) ->scheduledFor(1538061702); } |
Development Issue Faced
If you are trying this package on localhost , then just like me it might be possible that link will not be posted and it will throw an exception , so be aware of that.
Conclusion
Thanks for reading this up to the end. If you have any question or query related to this, you can comment down below. Full code with all the available methods can be found here.
Cheers ! Happy Coding