flutter_local_notification_plus 0.0.2 copy "flutter_local_notification_plus: ^0.0.2" to clipboard
flutter_local_notification_plus: ^0.0.2 copied to clipboard

Enhanced local notifications with rich media, actions, and scheduling for Flutter

example/lib/main.dart

import 'package:flutter/material.dart';
import 'package:flutter_local_notification_plus/flutter_local_notification_plus.dart';

void main() {
  runApp(const MyApp());
}

class MyApp extends StatelessWidget {
  const MyApp({super.key});

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Local Notification Plus Example',
      theme: ThemeData(
        colorScheme: ColorScheme.fromSeed(seedColor: Colors.deepPurple),
        useMaterial3: true,
      ),
      home: const MyHomePage(title: 'Flutter Local Notification Plus Example'),
    );
  }
}

class MyHomePage extends StatefulWidget {
  const MyHomePage({super.key, required this.title});

  final String title;

  @override
  State<MyHomePage> createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  bool _isInitialized = false;

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

  Future<void> _initializeNotifications() async {
    try {
      final result = await FlutterLocalNotificationPlus.initialize();

      if (result) {
        setState(() {
          _isInitialized = true;
        });
      }
    } catch (e) {
      if (mounted) {
        ScaffoldMessenger.of(context).showSnackBar(
          SnackBar(content: Text('Failed to initialize: $e')),
        );
      }
    }
  }

  Future<void> _showNotification() async {
    if (!_isInitialized) {
      ScaffoldMessenger.of(context).showSnackBar(
        const SnackBar(content: Text('Notifications not initialized')),
      );
      return;
    }

    try {
      await FlutterLocalNotificationPlus.showNotification(
        id: 1,
        title: 'Hello!',
        body:
            'This is a test notification from Flutter Local Notification Plus',
      );

      if (mounted) {
        ScaffoldMessenger.of(context).showSnackBar(
          const SnackBar(content: Text('Notification sent!')),
        );
      }
    } catch (e) {
      if (mounted) {
        ScaffoldMessenger.of(context).showSnackBar(
          SnackBar(content: Text('Failed to show notification: $e')),
        );
      }
    }
  }

  Future<void> _scheduleNotification() async {
    if (!_isInitialized) {
      ScaffoldMessenger.of(context).showSnackBar(
        const SnackBar(content: Text('Notifications not initialized')),
      );
      return;
    }

    try {
      final scheduledTime = DateTime.now().add(const Duration(seconds: 5));
      await FlutterLocalNotificationPlus.scheduleNotification(
        id: 2,
        title: 'Scheduled Notification',
        body: 'This notification was scheduled for 5 seconds from now',
        scheduledDate: scheduledTime,
      );

      if (mounted) {
        ScaffoldMessenger.of(context).showSnackBar(
          SnackBar(
            content:
                Text('Notification scheduled for ${scheduledTime.toString()}'),
          ),
        );
      }
    } catch (e) {
      if (mounted) {
        ScaffoldMessenger.of(context).showSnackBar(
          SnackBar(content: Text('Failed to schedule notification: $e')),
        );
      }
    }
  }

  Future<void> _cancelAllNotifications() async {
    if (!_isInitialized) {
      ScaffoldMessenger.of(context).showSnackBar(
        const SnackBar(content: Text('Notifications not initialized')),
      );
      return;
    }

    try {
      await FlutterLocalNotificationPlus.cancelAllNotifications();

      if (mounted) {
        ScaffoldMessenger.of(context).showSnackBar(
          const SnackBar(content: Text('All notifications cancelled!')),
        );
      }
    } catch (e) {
      if (mounted) {
        ScaffoldMessenger.of(context).showSnackBar(
          SnackBar(content: Text('Failed to cancel notifications: $e')),
        );
      }
    }
  }

  Future<void> _checkPermissions() async {
    if (!_isInitialized) {
      ScaffoldMessenger.of(context).showSnackBar(
        const SnackBar(content: Text('Notifications not initialized')),
      );
      return;
    }

    try {
      final enabled =
          await FlutterLocalNotificationPlus.areNotificationsEnabled();

      if (mounted) {
        ScaffoldMessenger.of(context).showSnackBar(
          SnackBar(
            content: Text('Notifications enabled: $enabled'),
          ),
        );
      }
    } catch (e) {
      if (mounted) {
        ScaffoldMessenger.of(context).showSnackBar(
          SnackBar(content: Text('Failed to check permissions: $e')),
        );
      }
    }
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        backgroundColor: Theme.of(context).colorScheme.inversePrimary,
        title: Text(widget.title),
      ),
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
            Text(
              'Status: ${_isInitialized ? "Initialized" : "Initializing..."}',
              style: Theme.of(context).textTheme.headlineSmall,
            ),
            const SizedBox(height: 20),
            ElevatedButton(
              onPressed: _isInitialized ? _showNotification : null,
              child: const Text('Show Notification'),
            ),
            const SizedBox(height: 10),
            ElevatedButton(
              onPressed: _isInitialized ? _scheduleNotification : null,
              child: const Text('Schedule Notification (5s)'),
            ),
            const SizedBox(height: 10),
            ElevatedButton(
              onPressed: _isInitialized ? _cancelAllNotifications : null,
              child: const Text('Cancel All Notifications'),
            ),
            const SizedBox(height: 10),
            ElevatedButton(
              onPressed: _isInitialized ? _checkPermissions : null,
              child: const Text('Check Permissions'),
            ),
          ],
        ),
      ),
    );
  }
}
1
likes
160
points
32
downloads

Publisher

verified publisherbechattaoui.dev

Weekly Downloads

Enhanced local notifications with rich media, actions, and scheduling for Flutter

Repository (GitHub)

Documentation

API reference

License

MIT (license)

Dependencies

flutter, flutter_web_plugins

More

Packages that depend on flutter_local_notification_plus

Packages that implement flutter_local_notification_plus