phayes0289's avatar

How do I Send IOS (or Android) a Critical or Standard Notification to their phone screen?

I have been building a large application for Fire Departments. It involves MANY modules. One of those modules is a display of all ACTIVE 911 / Emergency incidents for a given fire department (see attached screenshot). That data is being sent to my Laravel API from the 911 Center and being inserted into my main mysql database for use in the app.

https://app.screencast.com/HUG0dbGNdN8Tx

I want to send an IOS notification (standard and possibly critical) to users who "subscribe" for a new incidents the get inserted into the database. I have absolutely no idea where to begin. Can anybody provide some guidance or sample code? RIght now, I will be the only subscriber (User ID = 1).

The idea will be to alert the user of the new "911 incident". If the user clicks on the notification it will bring them to a laravel blade page that outputs the data into a human readable format.

0 likes
1 reply
LaryAI's avatar
Level 58

To send iOS (or Android) notifications, including critical alerts, you'll need to use a service like Apple Push Notification service (APNs) for iOS devices or Firebase Cloud Messaging (FCM) for Android devices. Since you're using Laravel, you can leverage packages like Laravel Notification Channels or Laravel-FCM to simplify the process.

Here's a step-by-step guide to get you started with sending notifications to iOS devices:

  1. Set up APNs:

    • Obtain an APNs authentication key from the Apple Developer Member Center.
    • Register your app and enable Push Notifications in the Capabilities section.
  2. Install Laravel Notification Channels:

    • Use Composer to install the package for APNs:
      composer require laravel-notification-channels/apn
      
  3. Create a Notification:

    • Use the php artisan make:notification command to create a new notification class.
      php artisan make:notification IncidentAlertNotification
      
    • In the created notification class, add the toApn method to format the notification payload.
  4. Send the Notification:

    • Use the Notification facade to send the notification when a new incident is inserted into the database.

Here's an example of what your notification class might look like:

use Illuminate\Notifications\Notification;
use NotificationChannels\Apn\ApnChannel;
use NotificationChannels\Apn\ApnMessage;

class IncidentAlertNotification extends Notification
{
    private $incident;

    public function __construct($incident)
    {
        $this->incident = $incident;
    }

    public function via($notifiable)
    {
        return [ApnChannel::class];
    }

    public function toApn($notifiable)
    {
        return ApnMessage::create()
            ->title('New 911 Incident')
            ->body("Incident #{$this->incident->id} has been reported.")
            ->critical(1) // Set to 1 for critical alerts
            ->custom('incident_id', $this->incident->id);
    }
}

To send the notification, you can use something like this in your code where the incident is created:

use Notification;

$user = User::find(1); // Your subscriber, in this case, user with ID 1
$incident = Incident::create($data); // The incident data

$user->notify(new IncidentAlertNotification($incident));

For critical alerts, you'll need to request special entitlements from Apple as they are meant for situations that require immediate attention.

For Android notifications, you can follow a similar process using the laravel-notification-channels/fcm package or directly with Firebase Cloud Messaging.

Remember to handle the receiving of notifications in your iOS and Android applications accordingly, and set up the logic to navigate the user to the appropriate blade page when they click on the notification.

Please note that this is a simplified example and you'll need to adjust the code to fit your application's architecture and requirements. Additionally, you'll need to handle user subscriptions and device token management to send notifications to the correct devices.

Please or to participate in this conversation.