A Flutter plugin for integrating NotificationAPI push notifications into your mobile app.
Cross-platform push notifications with native performance:
- Android: Full FCM (Firebase Cloud Messaging) support
- iOS: Direct APN (Apple Push Notifications) integration - no Firebase routing required!
This means you get native iOS push notifications with reduced latency and fewer dependencies.
Add to your pubspec.yaml
:
dependencies:
notificationapi_flutter_sdk: ^1.0.3
import 'package:notificationapi_flutter_sdk/notificationapi_flutter_sdk.dart';
// That's it! This handles initialization, user identification, and permission requests
await NotificationAPI.setup(
clientId: 'your_client_id_here',
userId: 'user123',
autoRequestPermission: true, // automatically request push permissions
showForegroundNotifications: true, // show native notifications when app is open
);
// Listen to notifications received while app is open
// Note: These are automatically shown as native notifications by default
NotificationAPI.onMessage.listen((notification) {
print('Received: ${notification.title}');
// Optional: Handle notification data or custom actions
});
// Listen to notifications that opened the app
NotificationAPI.onMessageOpenedApp.listen((notification) {
print('App opened from: ${notification.title}');
// Handle deep linking or navigation
});
// Check if everything is ready
if (NotificationAPI.isReady) {
print('NotificationAPI is ready to receive notifications!');
}
// Get the push token (FCM on Android, APN on iOS)
String? token = await NotificationAPI.getToken();
import 'package:flutter/material.dart';
import 'package:notificationapi_flutter_sdk/notificationapi_flutter_sdk.dart';
class MyApp extends StatefulWidget {
@override
_MyAppState createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> {
@override
void initState() {
super.initState();
_setupNotifications();
}
Future<void> _setupNotifications() async {
try {
await NotificationAPI.setup(
clientId: 'your_client_id',
userId: 'user123',
showForegroundNotifications: true, // Auto-show native notifications
);
// Optional: Listen for custom handling
NotificationAPI.onMessage.listen((notification) {
print('Foreground notification: ${notification.title}');
// Notification is automatically displayed as native notification
});
NotificationAPI.onMessageOpenedApp.listen((notification) {
print('App opened from notification: ${notification.title}');
// Handle navigation or deep linking
if (notification.deepLink != null) {
// Navigate to specific screen
}
});
} catch (e) {
print('Error setting up notifications: $e');
}
}
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(title: Text('NotificationAPI Example')),
body: Center(
child: Column(
children: [
Text('Ready: ${NotificationAPI.isReady}'),
SizedBox(height: 20),
ElevatedButton(
onPressed: () async {
bool granted = await NotificationAPI.requestPermission();
ScaffoldMessenger.of(context).showSnackBar(
SnackBar(content: Text('Permission: ${granted ? "Granted" : "Denied"}')),
);
},
child: Text('Request Permission'),
),
SizedBox(height: 20),
ElevatedButton(
onPressed: () {
// Toggle foreground notification display
NotificationAPI.setShowForegroundNotifications(false);
ScaffoldMessenger.of(context).showSnackBar(
SnackBar(content: Text('Foreground notifications disabled')),
);
},
child: Text('Disable Foreground Notifications'),
),
],
),
),
),
);
}
}
Background notifications are automatically handled by the push package. The SDK uses:
- FCM for Android background notifications
- APN for iOS background notifications
No additional setup is required for basic background notification handling.
When users tap a notification while the app is terminated, it's automatically handled:
class _MyAppState extends State<MyApp> {
@override
void initState() {
super.initState();
_setupNotifications();
}
Future<void> _setupNotifications() async {
await NotificationAPI.setup(
clientId: 'your_client_id',
userId: 'user123',
);
// Handle notifications that opened the app
NotificationAPI.onMessageOpenedApp.listen((notification) {
_handleNotificationTap(notification);
});
}
void _handleNotificationTap(PushNotification notification) {
print('Notification tapped: ${notification.title}');
// Handle deep linking or navigation
if (notification.deepLink != null) {
// Navigate to specific screen
Navigator.pushNamed(context, notification.deepLink!);
}
}
}
- Requires Firebase setup for FCM
- Background notifications work automatically
- Uses native APN - no Firebase required!
- Requires Apple Developer Program membership
- Enable Push Notifications capability in Xcode
- Background notifications work automatically
By default, when showForegroundNotifications: true
is set in setup()
, the SDK automatically displays received notifications as native push notifications even when your app is in the foreground. This provides a consistent user experience.
App State | Notification Display |
---|---|
Foreground | Native notification banner/alert (automatic) |
Background | System notification (handled by OS) |
Terminated | System notification (handled by OS) |
await NotificationAPI.setup(
// ... other parameters
showForegroundNotifications: true, // Enable native notifications in foreground
);
// You can also toggle this later
NotificationAPI.setShowForegroundNotifications(false); // Disable
NotificationAPI.setShowForegroundNotifications(true); // Re-enable
Method | Description | Returns |
---|---|---|
setup() |
One-call setup with initialization, identification, and optional permission request | Future<void> |
requestPermission() |
Request push notification permission | Future<bool> |
getToken() |
Get the current push token (FCM on Android, APN on iOS) | Future<String?> |
onMessage |
Stream of foreground notifications (auto-displayed as native notifications) | Stream<PushNotification> |
onMessageOpenedApp |
Stream of notifications that opened the app | Stream<PushNotification> |
currentUser |
Get current user info | NotificationUser? |
isReady |
Check if SDK is ready | bool |
setShowForegroundNotifications() |
Enable/disable automatic native notification display | void |
await NotificationAPI.setup({
required String clientId, // Your NotificationAPI client ID
required String userId, // User's unique identifier
String? hashedUserId, // Hashed user ID for privacy (optional)
bool autoRequestPermission = true, // Auto-request push permission (optional)
bool showForegroundNotifications = true, // Auto-show native notifications (optional)
});
- User IDs can be hashed for additional privacy
- All communication uses HTTPS
- Push tokens are securely stored in NotificationAPI backend
- Create a Firebase project at Firebase Console
- Add your Android app to the project
- Download
google-services.json
and place it inandroid/app/
- Follow the FlutterFire setup guide
- Requires Apple Developer Program membership ($99/year)
- Enable Push Notifications capability in Xcode
- Create APNs key in Apple Developer Console
- Configure your server to send notifications directly to APNs
-
Notifications not received
- Android: Verify Firebase setup is correct
- iOS: Ensure APNs key is configured correctly
- Check if permission was granted:
await NotificationAPI.requestPermission()
- Ensure your NotificationAPI client ID is correct
-
Foreground notifications not showing
- Make sure
showForegroundNotifications: true
in setup - Check that notification permissions are granted
- Verify app is actually in foreground
- Make sure
-
Token is null
- Android: Firebase may not be properly initialized
- iOS: APNs may not be properly configured
- Check device/simulator supports push notifications
-
iOS Specific Issues
- Ensure you have an Apple Developer Program membership
- Push Notifications capability must be enabled in Xcode
- APNs keys must be properly configured on your server
-
Android Specific Issues
- Verify
google-services.json
is in the correct location - Check that Firebase project includes your Android app's package name
- Verify
This project is licensed under the MIT License - see the LICENSE file for details.