notification_manager 1.0.1 copy "notification_manager: ^1.0.1" to clipboard
notification_manager: ^1.0.1 copied to clipboard

A comprehensive Flutter notification manager package that provides local and remote notification support.

example/lib/main.dart

import 'dart:async';

import 'package:firebase_core/firebase_core.dart';
import 'package:flutter/material.dart';
import 'package:flutter_shared_utilities/flutter_shared_utilities.dart';
import 'package:notification_manager/notification_manager.dart';
import 'package:notification_manager_example/screens/comprehensive_demo_screen.dart';

import 'firebase_options.dart';
import 'screens/background_test_screen.dart';
import 'screens/enhanced_permission_demo_screen.dart';
import 'screens/permission_test_screen.dart';
import 'screens/scheduling_debug_screen.dart';
import 'screens/settings_screen.dart';

void main() async {
  WidgetsFlutterBinding.ensureInitialized();

  // Initialize Firebase
  await Firebase.initializeApp(options: DefaultFirebaseOptions.currentPlatform);

  runApp(const NotificationManagerExampleApp());
}

/// Notification manager example app.
class NotificationManagerExampleApp extends StatefulWidget {
  /// Creates a notification manager example app.
  const NotificationManagerExampleApp({super.key});

  @override
  State<NotificationManagerExampleApp> createState() =>
      _NotificationManagerExampleAppState();
}

class _NotificationManagerExampleAppState
    extends State<NotificationManagerExampleApp> {
  ComprehensiveNotificationManager? _notificationManager;
  StreamSubscription<NotificationPayload>? _notificationReceivedSubscription;
  StreamSubscription<NotificationPayload>? _notificationOpenedSubscription;
  bool _isInitializing = true;
  String? _initializationError;

  // Global key for ScaffoldMessenger
  final GlobalKey<ScaffoldMessengerState> _scaffoldMessengerKey =
      GlobalKey<ScaffoldMessengerState>();

  @override
  void initState() {
    super.initState();
    _initializeNotifications();
  }

  @override
  void dispose() {
    _notificationReceivedSubscription?.cancel();
    _notificationOpenedSubscription?.cancel();
    _notificationManager?.dispose();
    super.dispose();
  }

  Future<void> _initializeNotifications() async {
    try {
      final ComprehensiveNotificationManager manager =
          await ComprehensiveNotificationManager.getInstance();

      final NotificationFailure? error = await manager.initialize(
        autoRequestPermissions:
            false, // Don't auto-request to allow manual control
      );
      if (error != null) {
        safeDebugLog('Failed to initialize notifications: ${error.message}');
        if (mounted) {
          setState(() {
            _initializationError = error.message;
            _isInitializing = false;
          });
        }
        return;
      }

      // Listen to notification events
      _notificationReceivedSubscription = manager.onNotificationReceived.listen(
        (NotificationPayload payload) {
          safeDebugLog('Notification received: ${payload.title}');
          _showSnackBar('Notification received: ${payload.title}. ');
        },
      );

      _notificationOpenedSubscription = manager.onNotificationOpened.listen((
        NotificationPayload payload,
      ) {
        safeDebugLog('Notification opened: ${payload.title}');
        _showSnackBar('Notification opened: ${payload.title}');
      });

      if (mounted) {
        setState(() {
          _notificationManager = manager;
          _isInitializing = false;
        });
      }
    } catch (e) {
      safeDebugLog('Failed to create notification manager: $e');
      if (mounted) {
        setState(() {
          _initializationError = e.toString();
          _isInitializing = false;
        });
      }
    }
  }

  void _showSnackBar(String message) {
    final ScaffoldMessengerState? scaffoldMessenger =
        _scaffoldMessengerKey.currentState;
    if (scaffoldMessenger != null) {
      scaffoldMessenger.showSnackBar(SnackBar(content: Text(message)));
    }
  }

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Notification Manager Example',
      theme: ThemeData(primarySwatch: Colors.blue, useMaterial3: true),
      scaffoldMessengerKey: _scaffoldMessengerKey,
      home: _buildHome(),
    );
  }

  Widget _buildHome() {
    if (_isInitializing) {
      return const Scaffold(
        body: Center(
          child: Column(
            mainAxisAlignment: MainAxisAlignment.center,
            children: <Widget>[
              CircularProgressIndicator(),
              SizedBox(height: 16),
              Text('Initializing notification services...'),
            ],
          ),
        ),
      );
    }

    if (_initializationError != null) {
      return Scaffold(
        body: Center(
          child: Column(
            mainAxisAlignment: MainAxisAlignment.center,
            children: <Widget>[
              const Icon(Icons.error, size: 64, color: Colors.red),
              const SizedBox(height: 16),
              Text(
                'Failed to initialize notifications',
                style: Theme.of(context).textTheme.headlineSmall,
              ),
              const SizedBox(height: 8),
              Text(
                _initializationError!,
                textAlign: TextAlign.center,
                style: Theme.of(context).textTheme.bodyMedium,
              ),
              const SizedBox(height: 16),
              ElevatedButton(
                onPressed: () {
                  setState(() {
                    _isInitializing = true;
                    _initializationError = null;
                  });
                  _initializeNotifications();
                },
                child: const Text('Retry'),
              ),
            ],
          ),
        ),
      );
    }

    final ComprehensiveNotificationManager? manager = _notificationManager;
    if (manager == null) {
      return const Scaffold(
        body: Center(child: Text('Notification manager not available')),
      );
    }

    return _ExampleHomeScreen(notificationManager: manager);
  }
}

class _ExampleHomeScreen extends StatelessWidget {
  const _ExampleHomeScreen({required this.notificationManager});

  final ComprehensiveNotificationManager notificationManager;

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text('Notification Manager Examples'),
        centerTitle: true,
      ),
      body: Padding(
        padding: const EdgeInsets.all(16),
        child: SingleChildScrollView(
          child: Column(
            crossAxisAlignment: CrossAxisAlignment.stretch,
            children: <Widget>[
              const Text(
                'Choose a demo to explore notification features:',
                style: TextStyle(fontSize: 16),
                textAlign: TextAlign.center,
              ),
              const SizedBox(height: 32),
              Card(
                child: ListTile(
                  leading: const Icon(Icons.star),
                  title: const Text('Comprehensive Demo'),
                  subtitle: const Text(
                    'Full-featured notification manager showcase',
                  ),
                  trailing: const Icon(Icons.arrow_forward_ios),
                  onTap: () {
                    Navigator.push(
                      context,
                      MaterialPageRoute<void>(
                        builder: (BuildContext context) =>
                            ComprehensiveDemoScreen(
                              notificationManager: notificationManager,
                            ),
                      ),
                    );
                  },
                ),
              ),
              const SizedBox(height: 16),
              Card(
                child: ListTile(
                  leading: const Icon(Icons.science),
                  title: const Text('Background Testing'),
                  subtitle: const Text(
                    'Test Firebase & local background notifications',
                  ),
                  trailing: const Icon(Icons.arrow_forward_ios),
                  onTap: () {
                    Navigator.push(
                      context,
                      MaterialPageRoute<void>(
                        builder: (BuildContext context) => BackgroundTestScreen(
                          notificationManager: notificationManager,
                        ),
                      ),
                    );
                  },
                ),
              ),
              const SizedBox(height: 16),
              Card(
                child: ListTile(
                  leading: const Icon(Icons.enhanced_encryption),
                  title: const Text('Enhanced Permission Demo'),
                  subtitle: const Text(
                    'Advanced permission troubleshooting with force re-request',
                  ),
                  trailing: const Icon(Icons.arrow_forward_ios),
                  onTap: () {
                    Navigator.push(
                      context,
                      MaterialPageRoute<void>(
                        builder: (BuildContext context) =>
                            const EnhancedPermissionDemoScreen(),
                      ),
                    );
                  },
                ),
              ),
              const SizedBox(height: 16),
              Card(
                child: ListTile(
                  leading: const Icon(Icons.verified_user),
                  title: const Text('Permission Tests'),
                  subtitle: const Text(
                    'Comprehensive automated permission testing suite',
                  ),
                  trailing: const Icon(Icons.arrow_forward_ios),
                  onTap: () {
                    Navigator.push(
                      context,
                      MaterialPageRoute<void>(
                        builder: (BuildContext context) =>
                            const PermissionTestScreen(),
                      ),
                    );
                  },
                ),
              ),
              const SizedBox(height: 16),
              Card(
                child: ListTile(
                  leading: const Icon(Icons.settings),
                  title: const Text('Settings'),
                  subtitle: const Text('Manage notification settings'),
                  trailing: const Icon(Icons.arrow_forward_ios),
                  onTap: () {
                    Navigator.push(
                      context,
                      MaterialPageRoute<void>(
                        builder: (BuildContext context) => SettingsScreen(
                          notificationManager: notificationManager,
                        ),
                      ),
                    );
                  },
                ),
              ),
              const SizedBox(height: 16),
              Card(
                color: Colors.orange.shade50,
                child: ListTile(
                  leading: Icon(
                    Icons.bug_report,
                    color: Colors.orange.shade700,
                  ),
                  title: Text(
                    'Scheduling Debug',
                    style: TextStyle(
                      fontWeight: FontWeight.bold,
                      color: Colors.orange.shade800,
                    ),
                  ),
                  subtitle: const Text(
                    'Troubleshoot scheduled notification issues',
                  ),
                  trailing: Icon(
                    Icons.arrow_forward_ios,
                    color: Colors.orange.shade700,
                  ),
                  onTap: () {
                    Navigator.push(
                      context,
                      MaterialPageRoute<void>(
                        builder: (BuildContext context) =>
                            SchedulingDebugScreen(
                              notificationManager: notificationManager,
                            ),
                      ),
                    );
                  },
                ),
              ),
              const SizedBox(height: 32),
              const Card(
                child: Padding(
                  padding: EdgeInsets.all(16),
                  child: Column(
                    crossAxisAlignment: CrossAxisAlignment.start,
                    children: <Widget>[
                      Text(
                        'Features Included:',
                        style: TextStyle(
                          fontSize: 16,
                          fontWeight: FontWeight.bold,
                        ),
                      ),
                      SizedBox(height: 8),
                      Text('• Firebase Cloud Messaging (FCM)'),
                      Text('• Local Notifications'),
                      Text('• Notification Templates'),
                      Text('• Scheduling & Recurring'),
                      Text('• User Preferences'),
                      Text('• Permission Management'),
                      Text('• Rich Media Support'),
                      Text('• Interactive Actions'),
                      Text('• Cross-platform Support'),
                    ],
                  ),
                ),
              ),
            ],
          ),
        ),
      ),
    );
  }
}
0
likes
150
points
55
downloads

Publisher

unverified uploader

Weekly Downloads

A comprehensive Flutter notification manager package that provides local and remote notification support.

Repository (GitHub)
View/report issues

Topics

#notification #push-notifications #firebase-messaging #firebase-notification #local-notifications

Documentation

API reference

License

MIT (license)

Dependencies

async, firebase_core, firebase_messaging, flutter, flutter_local_notifications, flutter_shared_utilities, timezone

More

Packages that depend on notification_manager