notibee 1.1.0 copy "notibee: ^1.1.0" to clipboard
notibee: ^1.1.0 copied to clipboard

NotiBee — One package. All notifications. Handle Local, Push (FCM), and In-App Notifications in Flutter.

example/lib/main.dart

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

void main() async {
  WidgetsFlutterBinding.ensureInitialized();
  runApp(const MyApp());
}

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

  @override
  Widget build(BuildContext context) {
    return const MaterialApp(home: HomePage());
  }
}

class HomePage extends StatefulWidget {
  const HomePage({super.key});

  @override
  State<HomePage> createState() => _HomePageState();
}

class _HomePageState extends State<HomePage> {
  String _lastAction = 'None';
  bool _usePush = false;
  String? _fcmToken;

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

  Future<void> _initializeNotifications() async {
    await NotiBee.init(
      appName: 'NotiBee Example',
      defaultIcon: NotifyIcon.app,
      defaultSound: NotifySound.system,
      usePushNotifications: _usePush,
      useInAppMessaging: _usePush,
    );

    if (_usePush) {
      final token = await NotiBee.getFCMToken();
      setState(() {
        _fcmToken = token;
      });
      debugPrint("FCM Token: $_fcmToken");
    }

    NotiBee.onAction((actionId) {
      setState(() {
        _lastAction = actionId;
      });
      debugPrint('Action clicked: $actionId');
    });
  }

  Future<void> _reInit() async {
    await _initializeNotifications();
    debugPrint('Re-initialized with Push: $_usePush');
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(title: const Text('NotiBee Example')),
      body: Center(
        child: SingleChildScrollView(
          padding: const EdgeInsets.all(20),
          child: Column(
            mainAxisAlignment: MainAxisAlignment.center,
            children: [
              Text(
                'Last Action: $_lastAction',
                style: const TextStyle(fontWeight: FontWeight.bold),
              ),
              const SizedBox(height: 10),
              SwitchListTile(
                title: const Text("Enable Push Notifications (FCM)"),
                subtitle: const Text("Toggle this to initialize FCM"),
                value: _usePush,
                onChanged: (val) {
                  setState(() {
                    _usePush = val;
                  });
                  _reInit();
                },
              ),
              if (_fcmToken != null)
                SelectableText(
                  "FCM Token: $_fcmToken",
                  style: const TextStyle(fontSize: 10, color: Colors.grey),
                ),
              const Divider(),
              ElevatedButton(
                onPressed: () {
                  NotiBee.triggerInAppEvent('test_event');
                  ScaffoldMessenger.of(context).showSnackBar(
                    const SnackBar(
                      content: Text('Triggered event: test_event'),
                    ),
                  );
                },
                child: const Text('Trigger In-App "test_event"'),
              ),
              const SizedBox(height: 10),
              const SizedBox(height: 10),
              ElevatedButton(
                onPressed: () {
                  debugPrint('Calling NotiBee.show...');
                  NotiBee.show(
                    title: 'Hello!',
                    body: 'This is a simple local notification.',
                  );
                  debugPrint('NotiBee.show called.');
                },
                child: const Text('Show Simple Notification'),
              ),
              const SizedBox(height: 10),
              ElevatedButton(
                onPressed: () {
                  NotiBee.show(
                    title: 'Custom Icon & Sound',
                    body: 'This notification uses a custom icon and sound.',
                    // Note: Ensure 'chat_icon' exists in drawable and 'bell_sound' in raw/Runner
                    icon: NotifyIcon.custom('bell'),
                    sound: NotifySound.custom('notification'),
                  );
                },
                child: const Text('Custom Icon & Sound'),
              ),
              const SizedBox(height: 10),
              ElevatedButton(
                onPressed: () async {
                  final id = await NotiBee.schedule(
                    title: 'Scheduled',
                    body: 'This notification appeared after 5 seconds.',
                    after: const Duration(seconds: 5),
                  );
                  ScaffoldMessenger.of(
                    context,
                  ).showSnackBar(SnackBar(content: Text('Scheduled ID: $id')));
                  setState(() {
                    _lastAction = 'Scheduled ID: $id';
                  });
                },
                child: const Text('Schedule for 5s'),
              ),
              const SizedBox(height: 10),
              ElevatedButton(
                onPressed: () {
                  if (_lastAction.startsWith('Scheduled ID: ')) {
                    final id = int.tryParse(_lastAction.split(': ').last);
                    if (id != null) {
                      NotiBee.cancel(id);
                      ScaffoldMessenger.of(context).showSnackBar(
                        const SnackBar(content: Text('Cancelled notification')),
                      );
                    }
                  } else {
                    ScaffoldMessenger.of(context).showSnackBar(
                      const SnackBar(
                        content: Text('No ID found in Last Action'),
                      ),
                    );
                  }
                },
                child: const Text('Cancel Scheduled Notification'),
              ),
              const SizedBox(height: 10),
              ElevatedButton(
                onPressed: () {
                  NotiBee.repeat(
                    title: 'Repeating',
                    body:
                        'This notification repeats every minute (min interval).',
                    // Note: Android minimum repeat interval is often 15 mins for exact, but periodic works differently.
                    // For demo, using hourly as per enum or daily. Hourly is safest min.
                    interval: NotifyInterval.hourly,
                  );
                  ScaffoldMessenger.of(context).showSnackBar(
                    const SnackBar(content: Text('Repeating hourly set')),
                  );
                },
                child: const Text('Repeat Hourly'),
              ),
              const SizedBox(height: 10),
              ElevatedButton(
                onPressed: () {
                  NotiBee.show(
                    title: 'Action Required',
                    body: 'Please make a choice.',
                    actions: [
                      NotifyAction(id: 'accept', label: 'Accept'),
                      NotifyAction(id: 'decline', label: 'Decline'),
                    ],
                  );
                },
                child: const Text('Show with Actions'),
              ),
            ],
          ),
        ),
      ),
    );
  }
}
2
likes
160
points
15
downloads

Publisher

unverified uploader

Weekly Downloads

NotiBee — One package. All notifications. Handle Local, Push (FCM), and In-App Notifications in Flutter.

Repository (GitHub)

Documentation

API reference

License

MIT (license)

Dependencies

firebase_core, firebase_in_app_messaging, firebase_messaging, flutter

More

Packages that depend on notibee

Packages that implement notibee