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

The easiest way to handle Firebase Cloud Messaging + Local Notifications + Timezone-aware scheduling in Flutter (Android & iOS)

example/lib/main.dart

import 'package:flutter/material.dart';
import 'package:firebase_core/firebase_core.dart';
import 'package:firebase_easy_notify/firebase_easy_notify.dart';

void main() async {
  WidgetsFlutterBinding.ensureInitialized();
  
  // Initialize Firebase
  await Firebase.initializeApp();
  
  // Initialize EasyNotify
  await EasyNotify.init(
    androidIcon: '@mipmap/ic_launcher',
    requestPermissionOnInit: true,
    onNotificationTap: (NotificationResponse response) {
      debugPrint('Local notification tapped: ${response.payload}');
    },
    onMessageOpened: (RemoteMessage message) {
      debugPrint('FCM notification opened: ${message.data}');
    },
  );
  
  runApp(const MyApp());
}

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

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'EasyNotify Demo',
      theme: ThemeData(
        colorScheme: ColorScheme.fromSeed(seedColor: Colors.deepPurple),
        useMaterial3: true,
      ),
      home: const HomePage(),
    );
  }
}

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

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

class _HomePageState extends State<HomePage> {
  String? _fcmToken;
  final List<String> _logs = [];

  @override
  void initState() {
    super.initState();
    _setupListeners();
    _getToken();
  }

  void _setupListeners() {
    // Listen to foreground messages
    EasyNotify.onMessage.listen((RemoteMessage message) {
      _addLog('Foreground message: ${message.notification?.title}');
    });

    // Listen to notification taps
    EasyNotify.onTap.listen((NotificationResponse response) {
      _addLog('Notification tapped: ${response.payload}');
    });

    // Listen to FCM notification taps
    EasyNotify.onMessageOpened.listen((RemoteMessage message) {
      _addLog('FCM notification opened: ${message.data}');
    });
  }

  Future<void> _getToken() async {
    final token = await EasyNotify.getToken();
    setState(() {
      _fcmToken = token;
    });
    _addLog('FCM Token: $token');
  }

  void _addLog(String log) {
    setState(() {
      _logs.insert(0, '${DateTime.now().toString().substring(11, 19)} - $log');
    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        backgroundColor: Theme.of(context).colorScheme.inversePrimary,
        title: const Text('EasyNotify Demo'),
      ),
      body: Column(
        children: [
          // Token display
          Container(
            padding: const EdgeInsets.all(16),
            color: Colors.grey[200],
            child: Column(
              crossAxisAlignment: CrossAxisAlignment.start,
              children: [
                const Text(
                  'FCM Token:',
                  style: TextStyle(fontWeight: FontWeight.bold),
                ),
                const SizedBox(height: 4),
                Text(
                  _fcmToken ?? 'Loading...',
                  style: const TextStyle(fontSize: 12),
                ),
              ],
            ),
          ),

          // Action buttons
          Expanded(
            child: ListView(
              padding: const EdgeInsets.all(16),
              children: [
                const Text(
                  'Actions',
                  style: TextStyle(fontSize: 18, fontWeight: FontWeight.bold),
                ),
                const SizedBox(height: 16),

                // Show immediate notification
                ElevatedButton(
                  onPressed: () async {
                    await EasyNotify.show(
                      title: 'Test Notification',
                      body: 'This is a test notification from EasyNotify!',
                      payload: 'test_payload',
                    );
                    _addLog('Immediate notification shown');
                  },
                  child: const Text('Show Immediate Notification'),
                ),

                const SizedBox(height: 8),

                // Schedule weekly notification
                ElevatedButton(
                  onPressed: () async {
                    await EasyNotify.scheduleWeekly(
                      id: 1,
                      day: DateTime.monday,
                      hour: 22,
                      minute: 0,
                      title: 'Workout Time!',
                      body: 'Let\'s keep the streak 💪',
                    );
                    _addLog('Weekly notification scheduled for Monday 22:00');
                  },
                  child: const Text('Schedule Weekly (Monday 22:00)'),
                ),

                const SizedBox(height: 8),

                // Schedule batch notifications
                ElevatedButton(
                  onPressed: () async {
                    await EasyNotify.scheduleBatch([
                      const ScheduleModel(
                        days: [DateTime.monday, DateTime.wednesday, DateTime.friday],
                        times: [
                          TimeOfDay(hour: 10, minute: 0),
                          TimeOfDay(hour: 18, minute: 0),
                        ],
                        title: 'Reminder',
                        body: 'Time to check in!',
                      ),
                    ]);
                    _addLog('Batch notifications scheduled (Mon/Wed/Fri at 10:00 & 18:00)');
                  },
                  child: const Text('Schedule Batch (Mon/Wed/Fri)'),
                ),

                const SizedBox(height: 8),

                // Subscribe to topic
                ElevatedButton(
                  onPressed: () async {
                    await EasyNotify.subscribe('all_users');
                    _addLog('Subscribed to topic: all_users');
                  },
                  child: const Text('Subscribe to "all_users"'),
                ),

                const SizedBox(height: 8),

                // Set user type
                ElevatedButton(
                  onPressed: () async {
                    await EasyNotify.setUserType(UserType.premium);
                    _addLog('User type set to: Premium');
                  },
                  child: const Text('Set User Type: Premium'),
                ),

                const SizedBox(height: 8),

                // Get pending notifications
                ElevatedButton(
                  onPressed: () async {
                    final pending = await EasyNotify.getPendingNotifications();
                    _addLog('Pending notifications: ${pending.length}');
                    for (final notification in pending) {
                      _addLog('  - ID: ${notification.id}, Title: ${notification.title}');
                    }
                  },
                  child: const Text('Get Pending Notifications'),
                ),

                const SizedBox(height: 8),

                // Cancel all notifications
                ElevatedButton(
                  onPressed: () async {
                    await EasyNotify.cancelAll();
                    _addLog('All notifications cancelled');
                  },
                  style: ElevatedButton.styleFrom(
                    backgroundColor: Colors.red,
                    foregroundColor: Colors.white,
                  ),
                  child: const Text('Cancel All Notifications'),
                ),

                const SizedBox(height: 24),

                // Logs section
                const Text(
                  'Logs',
                  style: TextStyle(fontSize: 18, fontWeight: FontWeight.bold),
                ),
                const SizedBox(height: 8),
                Container(
                  height: 200,
                  decoration: BoxDecoration(
                    border: Border.all(color: Colors.grey),
                    borderRadius: BorderRadius.circular(8),
                  ),
                  child: _logs.isEmpty
                      ? const Center(child: Text('No logs yet'))
                      : ListView.builder(
                          itemCount: _logs.length,
                          itemBuilder: (context, index) {
                            return Padding(
                              padding: const EdgeInsets.symmetric(
                                horizontal: 8,
                                vertical: 4,
                              ),
                              child: Text(
                                _logs[index],
                                style: const TextStyle(fontSize: 12),
                              ),
                            );
                          },
                        ),
                ),
              ],
            ),
          ),
        ],
      ),
    );
  }
}
0
likes
150
points
81
downloads

Documentation

Documentation
API reference

Publisher

unverified uploader

Weekly Downloads

The easiest way to handle Firebase Cloud Messaging + Local Notifications + Timezone-aware scheduling in Flutter (Android & iOS)

Repository (GitHub)
View/report issues

License

MIT (license)

Dependencies

firebase_messaging, flutter, flutter_local_notifications, flutter_timezone, timezone

More

Packages that depend on firebase_easy_notify