The Vibes — the unofficial Laracon US Day 3 event. Early Bird tickets available until March 31!
Plugin Marketplace

nativephp/mobile-firebase

Firebase plugin for NativePHP Mobile (Push notifications via FCM/APNs)

#Firebase Plugin for NativePHP Mobile

Push notifications via Firebase Cloud Messaging (FCM) for Android and Apple Push Notification service (APNs) for iOS.

#Overview

The PushNotifications API handles device registration for Firebase Cloud Messaging to receive push notifications.

#Installation

Copied!
composer require nativephp/mobile-firebase

#Configuration

#Android Setup

  1. Create a Firebase project at Firebase Console
  2. Add your Android app to the project
  3. Download google-services.json and place it in your project root
  4. The plugin compiler will automatically copy it to the Android project

#iOS Setup

  1. In your Firebase project, add your iOS app
  2. Download GoogleService-Info.plist and place it in your project root
  3. Enable Push Notifications capability in your Apple Developer account
  4. The plugin compiler will handle the configuration

#Usage

#PHP (Livewire/Blade)

Copied!
use Native\Mobile\Facades\PushNotifications;
 
// Check permission status before prompting
$status = PushNotifications::checkPermission();
// Returns: "granted", "denied", "not_determined", "provisional", or "ephemeral"
 
// Enroll for push notifications (triggers system dialog if not_determined)
PushNotifications::enroll();
 
// Get current token
$token = PushNotifications::getToken();

#JavaScript (Vue/React/Inertia)

Copied!
import { PushNotifications, On, Off, Events } from '#nativephp';
 
// Check permission status before prompting
const result = await PushNotifications.checkPermission();
const status = result.status; // "granted", "denied", "not_determined", etc.
 
// Only prompt if not yet determined
if (status === 'not_determined') {
await PushNotifications.enroll();
}
 
// Get current token
const tokenResult = await PushNotifications.getToken();
const token = tokenResult.token; // APNS token on iOS, FCM token on Android

#Events

#TokenGenerated

Fired when a push notification token is successfully generated.

PHP

Copied!
use Native\Mobile\Attributes\OnNative;
use Native\Mobile\Events\PushNotification\TokenGenerated;
 
#[OnNative(TokenGenerated::class)]
public function handlePushToken(string $token)
{
// Send token to your backend
$this->sendTokenToServer($token);
}

Vue

Copied!
import { On, Off, Events } from '#nativephp';
import { onMounted, onUnmounted } from 'vue';
 
const handleTokenGenerated = (payload) => {
const { token } = payload;
// Send token to your backend
sendTokenToServer(token);
};
 
onMounted(() => {
On(Events.PushNotification.TokenGenerated, handleTokenGenerated);
});
 
onUnmounted(() => {
Off(Events.PushNotification.TokenGenerated, handleTokenGenerated);
});

React

Copied!
import { On, Off, Events } from '#nativephp';
import { useEffect } from 'react';
 
const handleTokenGenerated = (payload) => {
const { token } = payload;
// Send token to your backend
sendTokenToServer(token);
};
 
useEffect(() => {
On(Events.PushNotification.TokenGenerated, handleTokenGenerated);
 
return () => {
Off(Events.PushNotification.TokenGenerated, handleTokenGenerated);
};
}, []);

#Permission Flow

  1. App calls checkPermission() to inspect current state
  2. If not_determined, show user a pre-prompt explaining the value
  3. User taps "Enable Notifications"
  4. App calls enroll()
  5. System shows permission dialog
  6. If granted, FCM generates token
  7. TokenGenerated event fires with token
  8. App sends token to backend
  9. Backend stores token for user
  10. Server can now send notifications to this device

#Permission Status Values

Status Description
granted User has granted notification permission
denied User has denied notification permission
not_determined Permission has not been requested yet
provisional Provisional (quiet) notifications allowed (iOS only)
ephemeral Ephemeral notifications allowed (iOS App Clips only)

#Best Practices

  • Call checkPermission() before enroll() to avoid triggering the system dialog prematurely
  • Show a custom pre-prompt UI explaining the value of notifications when status is not_determined
  • When status is denied, direct users to system settings instead of calling enroll() again
  • Handle permission denial gracefully with alternative engagement options