notifications_utils 0.0.1 copy "notifications_utils: ^0.0.1" to clipboard
notifications_utils: ^0.0.1 copied to clipboard

\ A Flutter package to get delivered notifications from the notification center on Android and iOS and cancel them.

example/lib/main.dart

import 'dart:math';

import 'package:english_words/english_words.dart';
import 'package:flutter/material.dart';
import 'package:flutter_local_notifications/flutter_local_notifications.dart';
import 'dart:async';

import 'package:notifications_utils/notifications_utils.dart';

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

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

  @override
  State<MyApp> createState() => _MyAppState();
}

class _MyAppState extends State<MyApp> {
  var _notifications = <DeliveredNotification>[];
  final _notificationsPlugin = FlutterLocalNotificationsPlugin();

  @override
  void initState() {
    super.initState();
    _notificationsPlugin.initialize(const InitializationSettings(iOS: DarwinInitializationSettings()));
    _getNotifications();
  }

  Future<void> _getNotifications() async {
    try {
      final notifications = await NotificationsUtils().getDeliveredNotifications();
      _notifications = notifications.whereType<DeliveredNotification>().toList();
      if (mounted) setState(() {});
    } catch (e, st) {
      debugPrintStack(stackTrace: st, label: e.toString());
    }
  }

  void _removeNotification(NotificationId notificationId) {
    NotificationsUtils().removeDeliveredNotifications([notificationId]);
    _getNotifications();
  }

  Future<void> _showNotification() async {
    await _notificationsPlugin.show(
      Random().nextInt(10000000),
      generateWordPairs().take(3).join(' '),
      generateWordPairs().take(3).join(' '),
      NotificationDetails(
        iOS: DarwinNotificationDetails(
          subtitle: generateWordPairs().take(3).join(' '),
          threadIdentifier: generateWordPairs().take(1).join(' '),
        ),
      ),
      payload: generateWordPairs().take(3).join(' '),
    );
    _getNotifications();
  }

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(
          title: const Text('Plugin example app'),
        ),
        floatingActionButton: FloatingActionButton(
          onPressed: () => _showNotification(),
          child: const Icon(Icons.add),
        ),
        body: ListView(
          children: [
            if (_notifications.isEmpty)
              const ListTile(
                title: Text('No delivered notifications'),
              ),
            for (final notification in _notifications)
              ListTile(
                onTap: () => _removeNotification(notification.id),
                leading: Text('Id:\n${notification.id.id}'),
                trailing: Text('Thread id:\n${notification.threadIdentifier}'),
                title: Text('Title: ${notification.title}. Subtitle: ${notification.subtitle}'),
                subtitle: Text('Body: ${notification.body}\nPayload: ${notification.payload}'),
              ),
          ],
        ),
      ),
    );
  }
}

extension on NotificationId {
  String get id => androidId?.toString() ?? iosId ?? 'null';
}
6
likes
0
points
165
downloads

Publisher

unverified uploader

Weekly Downloads

\ A Flutter package to get delivered notifications from the notification center on Android and iOS and cancel them.

Repository (GitHub)
View/report issues

License

unknown (license)

Dependencies

flutter

More

Packages that depend on notifications_utils