notificationapi_flutter_sdk 2.0.1
notificationapi_flutter_sdk: ^2.0.1 copied to clipboard
A Flutter plugin for integrating NotificationAPI push notifications into your mobile app.
example/lib/main.dart
import 'package:flutter/material.dart';
import 'package:notificationapi_flutter_sdk/notificationapi_flutter_sdk.dart';
void main() {
runApp(const MyApp());
}
class MyApp extends StatefulWidget {
const MyApp({super.key});
@override
State<MyApp> createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> {
bool _isReady = false;
String? _token;
List<PushNotification> _notifications = [];
bool _showForegroundNotifications = true;
@override
void initState() {
super.initState();
_setupNotificationAPI();
_listenToNotifications();
}
Future<void> _setupNotificationAPI() async {
try {
// Simple one-line setup with automatic native notification display!
await NotificationAPI.setup(
clientId: 'your_client_id_here',
userId: 'user123',
autoRequestPermission: true,
showForegroundNotifications:
true, // Native notifications when app is open
);
// Get the push token (FCM on Android, APN on iOS)
final token = await NotificationAPI.getToken();
setState(() {
_isReady = NotificationAPI.isReady;
_token = token;
});
} catch (e) {
print('Error setting up NotificationAPI: $e');
}
}
void _listenToNotifications() {
// Listen to foreground notifications (automatically displayed as native notifications)
NotificationAPI.onMessage.listen((notification) {
setState(() {
_notifications.insert(0, notification);
});
// No need for manual SnackBar - notification is automatically shown natively!
print('Foreground notification received: ${notification.title}');
});
// Listen to notifications that opened the app
NotificationAPI.onMessageOpenedApp.listen((notification) {
setState(() {
_notifications.insert(0, notification);
});
_showSnackBar('App opened from: ${notification.title}');
// Handle deep linking
if (notification.deepLink != null) {
print('Deep link: ${notification.deepLink}');
}
});
}
void _showSnackBar(String message) {
if (mounted) {
ScaffoldMessenger.of(context).showSnackBar(
SnackBar(content: Text(message)),
);
}
}
void _toggleForegroundNotifications() {
setState(() {
_showForegroundNotifications = !_showForegroundNotifications;
});
NotificationAPI.setShowForegroundNotifications(
_showForegroundNotifications);
_showSnackBar(_showForegroundNotifications
? 'Native notifications enabled'
: 'Native notifications disabled');
}
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(
title: const Text('NotificationAPI Example'),
backgroundColor: Colors.blue,
),
body: Padding(
padding: const EdgeInsets.all(16.0),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Card(
child: Padding(
padding: const EdgeInsets.all(16.0),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text(
'NotificationAPI Status',
style: Theme.of(context).textTheme.titleLarge,
),
const SizedBox(height: 8),
Row(
children: [
Icon(
_isReady ? Icons.check_circle : Icons.error,
color: _isReady ? Colors.green : Colors.red,
),
const SizedBox(width: 8),
Text(_isReady ? 'Ready' : 'Not Ready'),
],
),
if (_token != null) ...[
const SizedBox(height: 8),
Text(
'Push Token: ${_token!.substring(0, 20)}...',
style: Theme.of(context).textTheme.bodySmall,
),
],
const SizedBox(height: 8),
Row(
children: [
Icon(
_showForegroundNotifications
? Icons.notifications_active
: Icons.notifications_off,
color: _showForegroundNotifications
? Colors.green
: Colors.orange,
),
const SizedBox(width: 8),
Text(
'Native Notifications: ${_showForegroundNotifications ? "Enabled" : "Disabled"}',
),
],
),
],
),
),
),
const SizedBox(height: 16),
Row(
children: [
Expanded(
child: ElevatedButton.icon(
onPressed: () async {
final permission =
await NotificationAPI.requestPermission();
_showSnackBar(
permission
? 'Permission granted!'
: 'Permission denied',
);
},
icon: const Icon(Icons.security),
label: const Text('Request Permission'),
),
),
const SizedBox(width: 8),
Expanded(
child: ElevatedButton.icon(
onPressed: _toggleForegroundNotifications,
icon: Icon(_showForegroundNotifications
? Icons.notifications_off
: Icons.notifications_active),
label: Text(_showForegroundNotifications
? 'Disable Native'
: 'Enable Native'),
),
),
],
),
const SizedBox(height: 16),
Text(
'Notification History (${_notifications.length})',
style: Theme.of(context).textTheme.titleLarge,
),
const SizedBox(height: 8),
if (_notifications.isEmpty)
const Expanded(
child: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Icon(Icons.notifications_none,
size: 64, color: Colors.grey),
SizedBox(height: 16),
Text(
'No notifications yet',
style: TextStyle(color: Colors.grey, fontSize: 16),
),
SizedBox(height: 8),
Text(
'Notifications will appear here and as native alerts',
style: TextStyle(color: Colors.grey, fontSize: 12),
textAlign: TextAlign.center,
),
],
),
),
)
else
Expanded(
child: ListView.builder(
itemCount: _notifications.length,
itemBuilder: (context, index) {
final notification = _notifications[index];
return Card(
margin: const EdgeInsets.only(bottom: 8),
child: ListTile(
leading: const Icon(Icons.notifications,
color: Colors.blue),
title: Text(notification.title ?? 'No Title'),
subtitle: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text(notification.body ?? 'No Body'),
if (notification.deepLink != null)
Text(
'Deep Link: ${notification.deepLink}',
style: const TextStyle(
fontSize: 12,
fontStyle: FontStyle.italic,
),
),
],
),
trailing: Text(
notification.receivedAt
.toString()
.substring(11, 19),
style: Theme.of(context).textTheme.bodySmall,
),
),
);
},
),
),
],
),
),
),
);
}
}