Laravel - Setup Expo Push Notifications

nikoladomazetovikj

Nikola Domazetovikj

Posted on January 29, 2024

Laravel - Setup Expo Push Notifications

Hey there, fellow developers! Today, I'll guide you through each step on how to make your Laravel app send Expo push notifications. I'll break it down into simple, easy-to-understand explanations. Let's make your app shout out with Expo push notifications!

In this example I will use the following package:
https://github.com/Alymosul/laravel-exponent-push-notifications

Before we begin, make sure you've got Laravel installed and know a bit about notifications in Laravel. Also, have a working React or React Native app ready to roll.

  • Install package:
composer require alymosul/laravel-exponent-push-notifications
Enter fullscreen mode Exit fullscreen mode
  • In your .env file add this variables:
EXPONENT_PUSH_NOTIFICATION_INTERESTS_STORAGE_DRIVER=database
EXPONENT_PUSH_NOTIFICATION_DEBUG=true
Enter fullscreen mode Exit fullscreen mode
  • Next, step is to publish the migration files:
php artisan vendor:publish --provider="NotificationChannels\ExpoPushNotifications\ExpoPushNotificationsServiceProvider" --tag="migrations"
Enter fullscreen mode Exit fullscreen mode
  • Run the migrations:
php artisan migrate
Enter fullscreen mode Exit fullscreen mode
  • This step is optional, you may use it if you want to customize the configuration
php artisan vendor:publish --provider="NotificationChannels\ExpoPushNotifications\ExpoPushNotificationsServiceProvider" --tag="config"
Enter fullscreen mode Exit fullscreen mode
  • Add following lines in your routes/api.php file:
Route::group(['prefix' => 'api/exponent/devices', 'middleware' => 'expo.middleware'], function () {
    Route::post('subscribe', [
        'as'    =>  'register-interest',
        'uses'  =>  'NotificationChannels\ExpoPushNotifications\Http\ExpoController@subscribe',
    ]);

    Route::post('unsubscribe', [
        'as'    =>  'remove-interest',
        'uses'  =>  'NotificationChannels\ExpoPushNotifications\Http\ExpoController@unsubscribe',
    ]);
});
Enter fullscreen mode Exit fullscreen mode

Feel free to change things up as per your requirements

  • Create notification class:
php artisan make:notification ExpoNotification
Enter fullscreen mode Exit fullscreen mode
  • In your notification class you will need to add following lines of code:
 public function via($notifiable)
    {
        return [ExpoChannel::class];
    }

    public function toExpoPush($notifiable)
    {
        return ExpoMessage::create()
            ->badge(1)
            ->enableSound()
            ->title("Congratulations!")
            ->body("Your {$notifiable->service} account was approved!");
    }
Enter fullscreen mode Exit fullscreen mode
  • In order to send notification:
$user->notify(new ExpoNotification());
Enter fullscreen mode Exit fullscreen mode

Just a heads up – feel free to change any initial files as you see fit. The migration files set up a new table with two columns: key and value. When a user logs into the app, you'll need to use the subscribe API with the expo_token in the body. And when a user logs out, just use the unsubscribe API. Keep it simple and make it work for you!

{
    "expo_token" : "the value of your token"
}
Enter fullscreen mode Exit fullscreen mode

This article shows you how to integrate Expo push notifications into your Laravel app. I hope you find this guide helpful! 🚀📲

💖 💪 🙅 🚩
nikoladomazetovikj
Nikola Domazetovikj

Posted on January 29, 2024

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related