Notifications

Scaffold comes with prebuilt notifications that work elegantly in the basic app structure. In general you have 2 main forms of notifying a user, you can either email them or you can leave them a notification in the app. In-app notifications are also sent as web push notifications to the user's devices when they allow it. In order to streamline the process we made some handy Notification components in Scaffold.

Email Based

Any time you need to email a user some information, you can utilize the StandardEmail notification. It sends a basic email with a subject, greeting, message and a login button, using the standard email templates from Laravel.

use Illuminate\Support\Facades\Notification;

Notification::route('mail', $user->email)
    ->notify(new StandardEmail($user->name, 'Subject', 'this is a test'));

In App Based

Sometimes you don't need to email a user or, more so, you don't want to email the user. In these cases you can use the InAppNotification. It is stored in the database, and the user can mark it as read, delete it, or clear all their notifications.

$notification = new InAppNotification('this is a test');
$notification->isImportant();

auth()->user()->notify($notification);

The unread count in the navigation is the NotificationBadge Livewire component.

Web Push

An InAppNotification is also sent through the web push channel (laravel-notification-channels/webpush) when the user allows push notifications and has registered at least one device. The push shows the app name and the message, and opens the notifications page when tapped. Important notifications are sent with high urgency.

Pushes are sent inline, so config/webpush.php keeps the client timeout short so a slow push service can't stall a request.

Setup

Generate VAPID keys and set them in your .env. These keys must not change once users have subscribed.

php artisan webpush:vapid
VAPID_SUBJECT=mailto:[email protected]
VAPID_PUBLIC_KEY=
VAPID_PRIVATE_KEY=

Push needs the service worker, which only registers in production by default. Set PWA_SERVICE_WORKER=true to try it locally. See PWA.

Subscriptions

The browser side lives in resources/js/script-components/push.js. Subscribing has to start from a click, since Safari and iOS refuse permission prompts that aren't user gestures. Subscriptions are stored and removed through these routes, and a test push can be sent from the user's settings:

POST    user/push-subscriptions
DELETE  user/push-subscriptions
POST    user/push-subscriptions/test    // throttled to 6 a minute

The RemovePushSubscriptionOnLogout listener removes the browser's subscription when the user logs out.

Announcements

Admins with the announcements permission can send an important in-app notification to every user from admin/announcements/create.

Pusher

There is also simple Pusher notifications built in using the GeneralPusherEvent and the UserPusherEvent. These can be incredibly useful for triggering events in the UI or simply providing your users more information.

Helpers

All of these come with global helpers so you can easily call them as follows:

app_notify($message, $isImportant = false, $user = null)
email_notify($subject, $message, $user = null)
pusher_notify_general($data)
pusher_notify_user($user, $data)

When no user is given, app_notify and email_notify use the logged in user. email_notify only sends when the user allows email based notifications.

* The pusher notifications by default expect a message in the data for resources/js/script-components/echo-events.js to process. Any further interactions should be addressed in that file.