Setup Push Notifications
Complete guide to setting up Firebase Cloud Messaging (FCM) for push notifications.
Push notifications allow you to keep your users engaged and informed. This guide will walk you through the process of setting up Firebase Cloud Messaging (FCM), from creating a project to configuring your backend and mobile app.
1. Create a Firebase Project
The first step is to create a project in the Firebase Console.
- Go to the Firebase Console.
- Click Add project.
- Enter a project name (e.g.,
My Awesome App) and click Continue. - (Optional) Enable Google Analytics for your project and click Continue.
- Click Create project and wait for it to be ready.
2. Enable Cloud Messaging
Cloud Messaging is typically enabled by default, but you should verify it.
- In the left-hand menu, navigate to Project Settings (the gear icon).
- Click on the Cloud Messaging tab.
- Ensure that the Firebase Cloud Messaging API (V1) is enabled.
3. Generate Service Account Key
To allow your backend to send notifications, you need a service account key.
- In Project Settings, go to the Service accounts tab.
- Under "Firebase Admin SDK", ensure Node.js or PHP is selected (it doesn't affect the JSON file content).
- Click Generate new private key.
- A warning modal will appear; click Generate key.
- A
.jsonfile will be downloaded to your computer. Keep this file secure!
4. Backend Configuration (Laravel)
Once you have the service account JSON file, you need to place it in your backend server so it can authenticate with Firebase.
Placement
- Rename the downloaded JSON file to
firebase-auth.json. - Move the file to your Laravel backend:
- Path:
backend/laravel/storage/app/firebase-auth.json
- Path:
Important: Make sure to keep the file secure and never commit it to version control(Add it to .gitignore).
Verification
The NotificationSenderService in the Laravel backend is pre-configured to look for this file at that specific path.
// app/Services/NotificationSenderService.php
public function __construct()
{
$credentialsPath = storage_path('app/firebase-auth.json');
if (file_exists($credentialsPath)) {
$factory = (new Factory)->withServiceAccount($credentialsPath);
$this->messaging = $factory->createMessaging();
}
}5. Configure Mobile App (Flutter)
The most efficient way to link your mobile app to Firebase is using the FlutterFire CLI.
Prerequisites
- Install the Firebase CLI.
- Log in to Firebase:
firebase login - Install the FlutterFire CLI:
dart pub global activate flutterfire_cli
Configuration
- From the root of your Flutter project (
mobile/flutter), run:flutterfire configure - Select your Firebase project from the list.
- Select the platforms you want to support (Android and iOS).
- This will automatically:
- Create/Update
lib/firebase_options.dart. - Create
google-services.jsonfor Android. - Create
GoogleService-Info.plistfor iOS.
- Create/Update
6. iOS Specific Setup
iOS requires additional steps because of Apple's security model.
- APNs Authentication Key:
- Go to the Apple Developer Portal.
- Create a new Key for "Apple Push Notifications service (APNs)".
- Download the
.p8file. - In the Firebase Console, go to Project Settings > Cloud Messaging.
- Upload the
.p8file under Apple app shares.
- Xcode Configuration:
- Open
ios/Runner.xcworkspacein Xcode. - Go to Signing & Capabilities.
- Add Push Notifications and Background Modes (check "Remote notifications").
- Open
7. Testing Notifications
You can test if everything is working correctly using the provided notification services in the backend or by sending a test message from the Firebase Console.
Via Laravel Tinker
php artisan tinker$user = App\Models\User::first();
app(App\Services\NotificationSenderService::class)->sendToUser($user, "Hello!", "This is a test notification");Via Firebase Console
- In the Firebase Console, go to Engage > Messaging.
- Click Create your first campaign.
- Select Firebase Cloud Messaging messages.
- Fill in the title and body, then click Send test message.
- Enter your device token (obtained from the app logs) and click Test.