Flutter notifications
Published: 2023-12-07 08:32:18

"Normal notifications are working, but scheduled notifications are not coming."


import 'package:flutter/material.dart';
import 'notification_helper.dart';
import 'package:timezone/timezone.dart' as tz;
import 'dart:async';
import 'package:timezone/data/latest.dart' as tz;



Future<void> main() async {
  WidgetsFlutterBinding.ensureInitialized();
  await NotificationHelper.initialize();
  tz.initializeTimeZones(); // Initialize the timezone database
  runApp(const MyApp());
}

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

  @override
  Widget build(BuildContext context) {
    return const MaterialApp(
      title: 'Flutter Demo',
      home: MyHomePage(),
    );
  }
}

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

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text('Ana Sayfa'),
      ),
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
            TextButton(
              onPressed: () {
                // AnÄ±nda bildirim gÃ¶ster
                NotificationHelper.showNotification(
                  title: 'AnÄ±nda Bildirim',
                  body: 'Bu bir anÄ±nda bildirimdir.',
                  payload: 'anindaBildirim',
                );
              },
              child: const Text('AnÄ±nda Bildirim'),
            ),
            const SizedBox(height: 20),
            TextButton(
              onPressed: () {
                // 30 saniye sonra bildirim gÃ¶ster
                DateTime scheduledTime = DateTime.now().add(const Duration(seconds: 1));
                NotificationHelper.scheduleNotification(
                  scheduledDateTime: scheduledTime,
                  title: 'ZamanlanmÄ±ÅŸ Bildirim',
                  body: 'Bu bildirim 30 saniye sonra gÃ¶sterilecek.',
                  payload: 'zamanlanmisBildirim',
                );
              },
              child: const Text('30 Saniye Sonra Bildirim'),
            ),
          ],
        ),
      ),
    );
  }
}

//--------notification_helper.dart----------//
import 'dart:async';
import 'package:flutter_local_notifications/flutter_local_notifications.dart';
import 'package:timezone/timezone.dart' as tz;

class NotificationHelper {
  static final _notifications = FlutterLocalNotificationsPlugin();

  static Future initialize() async {
    const androidInitialize = AndroidInitializationSettings('@mipmap/ic_launcher'); // 'mipmap/ic_launcher' yerine 'ic_launcher' kullanÄ±n
    const initializationsSettings = InitializationSettings(android: androidInitialize);
    await _notifications.initialize(initializationsSettings);
  }
  static Future _notificationDetails() async => const NotificationDetails(
    android: AndroidNotificationDetails(
      "GunSayaci",
      "day_counterr_1",
      importance: Importance.max,
    ),
  );
  static Future showNotification({
    int id = 0,
    required String title,
    required String body,
    required String payload, // bildirime extra veri eklemek istersek
  }) async =>
      _notifications.show(id, title, body, await _notificationDetails(),
          payload: payload);
  static Future scheduleNotification({
    int id = 1,
    String? title,
    String? body,
    String? payload,
    required DateTime scheduledDateTime,
  }) async =>
      _notifications.zonedSchedule(
          id,
          title,
          body,
          tz.TZDateTime.from(scheduledDateTime, tz.local),
          await _notificationDetails(),
          androidScheduleMode: AndroidScheduleMode.alarmClock,
          uiLocalNotificationDateInterpretation:
          UILocalNotificationDateInterpretation.absoluteTime);
  static Future unScheduleAllNotifications() async =>
      await _notifications.cancelAll();
}