Categories: Laravel Application, Laravel Framework - The PHP Framework For Web Artisans

Post On Facebook Page Via Laravel Notification3 min read

facebook post using Laravel Notification
Reading Time: 2 minutes

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 avaialbale on the Git Hub we will be using Laravel-notification-channel.

Package Installation

To install the package via composer. Open your command prompt and go to your desired project folder and type the following command.

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

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:

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.

Now next is the creation of the Notification using the following command.

Follow the path app\Notifications\ArticlesPublished.php and change the via() method of the Notification class with FacebookPoster method as shown below.

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.

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.

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

All Rights Reserved @ Techanical-Atom
%d bloggers like this: