notification_kit 1.1.5
notification_kit: ^1.1.5 copied to clipboard
Flutter package for FCM, local and push notifications, permissions, Android notification channels, and notification tap handling with a single initialization call.
import 'package:flutter/material.dart';
import 'package:notification_kit/notification_kit.dart';
// A mock FirebaseOptions instance for demonstration/compilation purposes.
// Replace with DefaultFirebaseOptions.currentPlatform in a real application.
const mockFirebaseOptions = FirebaseOptions(
apiKey: "AIzaSyDummyKeyForBootstrapPackageDemoOnly",
appId: "1:1234567890:android:abcd1234567890",
messagingSenderId: "1234567890",
projectId: "bootstrap-package-demo",
);
void main() async {
WidgetsFlutterBinding.ensureInitialized();
// Initialize Firebase and notification handlers in a single call.
await FirebaseBootstrap.start(
firebaseOptions: mockFirebaseOptions,
config: const FirebaseBootstrapConfig(
appName: "Bootstrap Package Demo",
channelId: "demo_default_channel",
channelName: "General Notifications",
channelDescription: "All app notifications and news updates",
notificationIcon: "@mipmap/ic_launcher",
enableLogs: true,
requestPermissionOnStartup: true,
showForegroundNotification: true,
clearNotificationsOnLaunch: true,
),
callbacks: FirebaseBootstrapCallbacks(
onNotificationTap: (payload) {
// Handle notification taps here. E.g. trigger navigation or state updates.
debugPrint("User tapped notification. Title: ${payload.title}, Data: ${payload.data}");
},
onForegroundMessage: (message) {
// Custom foreground message processing.
debugPrint("Received foreground push: ${message.notification?.title}");
},
onTokenRefresh: (token) {
// Persist token refresh update.
debugPrint("FCM Token updated: $token");
},
onPermissionGranted: () {
debugPrint("Notification permissions granted!");
},
onPermissionDenied: () {
debugPrint("Notification permissions denied!");
},
),
);
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({super.key});
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Bootstrap Demo',
theme: ThemeData(
colorScheme: ColorScheme.fromSeed(seedColor: Colors.deepPurple),
useMaterial3: true,
),
home: const DemoHomeScreen(),
);
}
}
class DemoHomeScreen extends StatefulWidget {
const DemoHomeScreen({super.key});
@override
State<DemoHomeScreen> createState() => _DemoHomeScreenState();
}
class _DemoHomeScreenState extends State<DemoHomeScreen> {
String _status = "Loading permission status...";
String _token = "No token fetched yet.";
@override
void initState() {
super.initState();
_checkStatus();
}
Future<void> _checkStatus() async {
final granted = await FirebaseBootstrap.isPermissionGranted();
final token = await FirebaseBootstrap.getToken();
setState(() {
_status = granted ? "Permissions: Granted" : "Permissions: Denied";
_token = token ?? "Unavailable";
});
}
Future<void> _requestPermission() async {
final success = await FirebaseBootstrap.requestPermission();
setState(() {
_status = success ? "Permissions: Granted" : "Permissions: Denied";
});
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text("Firebase Bootstrap Demo"),
backgroundColor: Theme.of(context).colorScheme.inversePrimary,
),
body: Padding(
padding: const EdgeInsets.all(16.0),
child: Column(
crossAxisAlignment: CrossAxisAlignment.stretch,
children: [
Card(
child: Padding(
padding: const EdgeInsets.all(16.0),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text(
_status,
style: Theme.of(context).textTheme.titleMedium,
),
const SizedBox(height: 8),
Text(
"FCM Token:",
style: Theme.of(context).textTheme.bodySmall?.copyWith(fontWeight: FontWeight.bold),
),
SelectableText(
_token,
style: Theme.of(context).textTheme.bodySmall,
),
],
),
),
),
const SizedBox(height: 24),
ElevatedButton.icon(
onPressed: _requestPermission,
icon: const Icon(Icons.security),
label: const Text("Request Permissions"),
),
const SizedBox(height: 12),
ElevatedButton.icon(
onPressed: () => FirebaseBootstrap.clearNotifications(),
icon: const Icon(Icons.clear_all),
label: const Text("Clear Active Notifications"),
),
const SizedBox(height: 12),
ElevatedButton.icon(
onPressed: () => FirebaseBootstrap.openSettings(),
icon: const Icon(Icons.settings),
label: const Text("Open App Settings"),
),
],
),
),
);
}
}