Push Notifications
ShipFlutter uses Firebase Cloud Messaging (FCM) to handle push notifications across all platforms. The notifications module provides a complete setup for handling notifications in the foreground, background, and when the app is terminated.
Setup
-
Follow the FCM setup guide in the Firebase Console
-
For iOS, upload your APNs certificate:
- Go to Firebase Console > Project Settings > Cloud Messaging
- Upload your Apple Push Notification service certificate
-
For Android, the setup is handled automatically by the Firebase CLI
-
For Web, add your Firebase web configuration to
app/web/firebase-messaging-sw.js
Notification Service
The NotificationsService
handles device registration and message handling:
// Request permission and register devicefinal device = await notificationsService.instance.register( requestPermission: true, // To automatically request permission if needed);
// Get FCM tokenprint('Token: ${device.token}');
// Listen to foreground messagesnotificationsService.instance.lastMessage.listen((message) { if (message != null) { // Handle the message print('Title: ${message.notification?.title}'); print('Body: ${message.notification?.body}'); print('Data: ${message.data}'); }});
// Messages can include deep linksfinal link = message.getLink();if (link != null) { context.push(link);}
Local Notifications
When the app is in the foreground, FCM messages are displayed using flutter_local_notifications
:
// Display notification with optional deep linkawait notification.display(link: message.getLink());
The notification configuration includes:
- Custom notification channel for Android
- Sound and badge support for iOS
- Deep link handling when tapped
Notifications Settings
The settings_view.dart
contains a notifications tile that handles permission requests and device registration:
- Permission requests
- Device registration
- Opening system settings
- Loading states
Background Handling
Background messages are handled in notifications_background.dart
:
@pragma('vm:entry-point')Future<void> firebaseMessagingBackgroundHandler( RemoteMessage message,) async { // Initialize Firebase await Firebase.initializeApp( options: DefaultFirebaseOptions.currentPlatform, );
// TODO: Handle the message print('Handling background message: ${message.messageId}');}