Skip to content

Notifications

Notifications ship in the official pushinbr/pam-native-notifications plugin, so apps that never notify carry neither the code nor the POST_NOTIFICATIONS permission:

Terminal window
pam composer require pushinbr/pam-native-notifications
pam doctor --fix

It provides two APIs in the Pam\Native\Notifications namespace:

  • Notifications schedules and cancels local notifications created by the app itself — reminders, “your export is ready”, a timer that finished — and builds conversation notifications with inline reply.
  • PushNotifications registers the device for push delivery (Firebase Cloud Messaging on Android, APNs on iOS) and delivers received and opened notifications to PHP.

Both share one event stream: when the user taps a notification — local or push — your app receives an Opened event and can navigate to its deep link.

On Android 13+ and on iOS, the user must allow notifications. The plugin declares POST_NOTIFICATIONS for you; ask at a meaningful moment — for example after the user enables reminders — not on first launch:

use Pam\Native\Notifications\Notifications;
Notifications::requestPermission(
function (bool $granted): void {
$this->remindersEnabled = $granted;
},
fn (string $message) => $this->error = $message,
);

You can also use the generic permissions API with PermissionKind::Notifications to read the status (including whether it is blocked) before asking.

src/Reminders/OrderReminder.php
<?php
declare(strict_types=1);
namespace App\Reminders;
use Pam\Native\Notifications\NotificationImportance;
use Pam\Native\Notifications\Notifications;
final class OrderReminder
{
public static function schedule(int $orderId, int $minutes): void
{
Notifications::schedule(
id: "order-ready:{$orderId}",
title: 'Order ready',
body: "Order #{$orderId} is ready for pickup.",
delaySeconds: $minutes * 60,
importance: NotificationImportance::High,
data: ['orderId' => $orderId],
deepLink: "acme://orders/{$orderId}",
);
}
public static function cancel(int $orderId): void
{
Notifications::cancel("order-ready:{$orderId}");
}
}
Parameter Notes
id Stable identifier, 1–128 bytes. Scheduling the same ID again replaces the pending notification.
title / body Title 1–4,096 bytes, body up to 16,384 bytes.
delaySeconds 0 shows it now; clamped to at most one year.
importance NotificationImportance::Low, Default, High, or Urgent.
data JSON-encodable array, at most 256 KiB once encoded. Delivered back on open.
deepLink Optional URI (at most 8,192 bytes) opened when the user taps it.
callback Optional closure called once the notification is scheduled.

Use IDs derived from your domain (order-ready:42) so rescheduling replaces the intended item instead of stacking duplicates.

Android groups notifications into channels the user controls in system settings. PAM creates one channel per importance level the first time it is used (pam-native-1 … pam-native-4) and maps them to the platform importance:

NotificationImportance Android importance
Low IMPORTANCE_LOW — no sound
Default IMPORTANCE_DEFAULT
High IMPORTANCE_HIGH — heads-up
Urgent IMPORTANCE_MAX

Reserve High and Urgent for things the user is waiting for. Once the user lowers a channel in settings, the app cannot raise it again.

The PAM Native host compiles Firebase Cloud Messaging into the Android build only when a Firebase client file is present. Download google-services.json from the Firebase console and save it at .pam/google-services.json (preferred) or at the project root. Without the file, no Firebase code or dependency is packaged.

The optional pushinbr/pam-native-firebase plugin adds Analytics, Remote Config, Crashlytics, and Messaging token APIs; presentation and routing of incoming notifications stay in the core API described here.

Enable the Push Notifications capability for your app identifier and configure APNs for your server. The iOS host forwards the device token, foreground delivery, and taps through PamPushNotifications.

src/Push/PushRegistration.php
<?php
declare(strict_types=1);
namespace App\Push;
use Pam\Native\Http\Http;
use Pam\Native\Http\HttpResponse;
use Pam\Native\Notifications\PushToken;
use Pam\Native\Notifications\PushNotifications;
final class PushRegistration
{
public static function register(string $apiToken): void
{
PushNotifications::register(
static function (PushToken $token) use ($apiToken): void {
Http::json(
method: 'POST',
url: 'https://api.example.com/devices',
data: [
'token' => $token->value,
'provider' => $token->provider->name, // "Fcm" or "Apns"
],
callback: static fn (HttpResponse $response) => null,
headers: ['Authorization' => "Bearer {$apiToken}"],
);
},
static function (string $message): void {
// Missing client configuration or provider failure: keep the app usable.
},
);
}
public static function unregister(): void
{
// Remove the token on your server first, then:
PushNotifications::unregister(
static fn () => null,
static fn (string $message) => null,
);
}
}

PushToken has value and provider (PushProvider::Fcm or PushProvider::Apns). unregister() invalidates the FCM token on Android and unregisters from APNs on iOS. Delete the token on your server first so a provider failure can be retried safely.

PushNotifications::listen() delivers every PushMessage. listenAndRoute() does the same and also opens the deep link of any opened notification on a navigator. Set it up once, next to your routes:

index.php
<?php
declare(strict_types=1);
use App\Screens\HomeScreen;
use App\Screens\OrderScreen;
use Pam\Native\App;
use Pam\Native\Notifications\PushEventType;
use Pam\Native\Notifications\PushMessage;
use Pam\Native\Routing\Route;
use Pam\Native\Notifications\PushNotifications;
require __DIR__.'/vendor/autoload.php';
$navigator = Route::stack('main', initial: 'home', routes: function (): void {
Route::screen('home', HomeScreen::class);
Route::screen('order', OrderScreen::class);
});
PushNotifications::listenAndRoute($navigator, function (PushMessage $message): void {
if ($message->event === PushEventType::Received) {
// Arrived while the app was running: refresh a badge, a list, etc.
}
});
App::run($navigator);
PushMessage property Meaning
event PushEventType::Received (1) or PushEventType::Opened (2)
id, title, body The notification content
data Decoded data payload (array)
deepLink URI to open, or null
rendered true when a native push-rendering rule already displayed it

Routing happens only for Opened events that carry a deep link. Map your deep link URIs to routes as described in Navigation.

Open the tapped notification on the first frame

Section titled “Open the tapped notification on the first frame”

When a tap cold-starts the app, read it synchronously before the first render so the app opens directly on the right screen instead of flashing the home screen first:

index.php (excerpt)
use Pam\Native\Notifications\PushNotifications;
$launch = PushNotifications::takeLaunchNotification(); // ?PushMessage, Opened
if ($launch?->deepLink !== null) {
$navigator->openInitial($launch->deepLink);
}

It returns the tap once per process, and the same tap is never delivered again to listen() or listenAndRoute(). On Android it covers notifications shown by the plugin; notifications that Firebase displays itself in the background are not reported this way.

Send the deep link in the data payload under deepLink (or deep_link):

FCM message (server side)
{
"message": {
"token": "DEVICE_TOKEN",
"notification": { "title": "Order ready", "body": "Order #42 is ready." },
"data": { "deepLink": "acme://orders/42", "orderId": "42" }
}
}
  • Events that arrive before PHP subscribes — for example the tap that launched the app — are queued natively (up to 64) and delivered when you call listen() or listenAndRoute(). Android persists unconsumed events across process startup.
  • The data payload is bounded to 256 KiB, deep links to 8,192 bytes.
  • Call PushNotifications::unsubscribe($subscription) when a short-lived owner no longer needs events. An app-wide listener set up in index.php lives for the whole process.

Notifications::conversation($key) builds a messaging-style notification with people, grouping, inline reply, and mark-as-read actions; taps and replies are delivered to Notifications::onAction(), even when they happened while PHP was not running. On Android, PushRendering rules can render Firebase data-only pushes natively while the app is closed. See the plugin’s README for the full builder API.

  • Schedule a local notification with delaySeconds: 5, background the app, and tap it to verify routing without a server.
  • For push, send a test message from the Firebase console or your server and check PushNotifications::listen() in DevTools, which shows module calls, failures, and lifecycle events.
  • Testing provides bridge fakes to assert on notification calls in PHPUnit.