custom_notification_scheduler 0.0.5 copy "custom_notification_scheduler: ^0.0.5" to clipboard
custom_notification_scheduler: ^0.0.5 copied to clipboard

A Flutter plugin for scheduling notifications with custom sounds and FCM support. on Android and iOS.

example/lib/main.dart

import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'dart:async';
import 'package:custom_notification_scheduler/custom_notification_scheduler.dart';
import 'package:permission_handler/permission_handler.dart';

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

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

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: NotificationExample(),
    );
  }
}

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

  @override
  _NotificationExampleState createState() => _NotificationExampleState();
}

class _NotificationExampleState extends State<NotificationExample> {
  String? _fcmToken; // To store and display the FCM token

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

  Future<void> _initializePlugin() async {
    try {
      await CustomNotificationScheduler.initialize();
      await _requestPermissions();
      await CustomNotificationScheduler.setNotificationSound('asset:custom_sound');
      _fcmToken = await CustomNotificationScheduler.getFcmToken();
      if (_fcmToken != null && mounted) {
        if (kDebugMode) {
          print('FCM Token: $_fcmToken');
        }
        ScaffoldMessenger.of(context).showSnackBar(
          SnackBar(content: Text('FCM Token: $_fcmToken')),
        );
      }
    } catch (e) {
      if (mounted) {
        ScaffoldMessenger.of(context).showSnackBar(
          SnackBar(content: Text('Failed to initialize plugin: $e')),
        );
      }
    }
  }

  Future<void> _requestPermissions() async {
    var status = await Permission.notification.status;
    if (!status.isGranted) {
      status = await Permission.notification.request();
      if (!status.isGranted && mounted) {
        ScaffoldMessenger.of(context).showSnackBar(
          SnackBar(content: Text('Notification permission is required to proceed.')),
        );
      }
    }
  }

  Future<void> _scheduleNotification() async {
    var status = await Permission.notification.status;
    if (status.isGranted) {
      final scheduledTime = DateTime.now().add(Duration(seconds: 5));
      if (kDebugMode) {
        print('Scheduling notification for: $scheduledTime');
      }
      try {
        await CustomNotificationScheduler.scheduleNotification(
          title: "Test",
          body: "This is a test notification",
          scheduledTime: scheduledTime,
          payload: {"test": "data"},
          repeatInterval: null,
        );
        if (mounted) {
          ScaffoldMessenger.of(context).showSnackBar(
            SnackBar(content: Text('Notification scheduled for ${scheduledTime.toIso8601String()}!')),
          );
        }
      } catch (e) {
        if (mounted) {
          ScaffoldMessenger.of(context).showSnackBar(
            SnackBar(content: Text('Failed to schedule notification: $e')),
          );
        }
      }
    } else {
      if (mounted) {
        ScaffoldMessenger.of(context).showSnackBar(
          SnackBar(content: Text('Notification permission denied')),
        );
        await openAppSettings();
      }
    }
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(title: Text('Notification Example')),
      body: Builder(
        builder: (context) => Center(
          child: Column(
            mainAxisAlignment: MainAxisAlignment.center,
            children: [
              Text('FCM Token: ${_fcmToken ?? "Loading..."}'),
              SizedBox(height: 20),
              ElevatedButton(
                onPressed: _scheduleNotification,
                child: Text('Schedule Notification'),
              ),
              ElevatedButton(
                onPressed: () async {
                  await CustomNotificationScheduler.cancelAllNotifications();
                  if (mounted) {
                    ScaffoldMessenger.of(context).showSnackBar(
                      SnackBar(content: Text('All notifications canceled!')),
                    );
                  }
                },
                child: Text('Cancel All'),
              ),
            ],
          ),
        ),
      ),
    );
  }
}
1
likes
140
points
45
downloads

Publisher

unverified uploader

Weekly Downloads

A Flutter plugin for scheduling notifications with custom sounds and FCM support. on Android and iOS.

Repository (GitHub)
View/report issues

Documentation

API reference

License

MIT (license)

Dependencies

flutter, flutter_local_notifications, flutter_local_notifications_platform_interface, get, plugin_platform_interface, timezone

More

Packages that depend on custom_notification_scheduler

Packages that implement custom_notification_scheduler