Open zsrour1991 opened 2 hours ago
The issue template is required, not optional;
Your Environment
flutter info
, flutter doctor
):To Reproduce Steps to reproduce the behavior:
Debug logs
$ adb logcat
Additional context Add any other context about the problem here.
import 'dart:async'; import 'dart:convert'; import 'dart:io'; import 'package:ess_application/appservices_lib.dart'; import 'package:ess_core/esscore_lib.dart'; import 'package:flutter_local_notifications/flutter_local_notifications.dart'; import 'package:get_it/get_it.dart'; import 'package:permission_handler/permission_handler.dart'; import 'package:shared_preferences/shared_preferences.dart'; import 'package:background_fetch/background_fetch.dart';
class BackGroundHelper { late FlutterLocalNotificationsPlugin flutterLocalNotificationsPlugin; late Timer _timer;
BackGroundHelper() { initializeNotifications(); initBackgroundFetch(); //registration(); startTimer(); // Start the timer here } static late NotificationService notificationService = locator(); // Declare NotificationService
static GetIt sl = GetIt.instance;
Future initializeNotifications() async {
print("initializeNotifications");
flutterLocalNotificationsPlugin = FlutterLocalNotificationsPlugin();
}
Future _createNotificationChannel() async {
const AndroidNotificationChannel channel = AndroidNotificationChannel(
'your_channel_id', // Unique ID for the channel
'your_channel_name', // User-visible name for the channel
description: 'your_channel_description', // Description for the channel
importance: Importance.max, // Importance level for notifications
playSound: true, // Play sound for notifications
);
await requestPermissions();
flutterLocalNotificationsPlugin
.resolvePlatformSpecificImplementation<
AndroidFlutterLocalNotificationsPlugin>()
?.createNotificationChannel(channel);
}
Future showNotification() async {
print("Showing notification...");
var androidPlatformChannelSpecifics = AndroidNotificationDetails(
'your_channel_id', // Ensure this matches the channel ID used in _createNotificationChannel
'your_channel_name',
channelDescription: 'your_channel_description',
importance: Importance.max,
priority: Priority.high,
playSound: true, // Ensure this is set if you want sound
);
var platformChannelSpecifics = NotificationDetails(
android: androidPlatformChannelSpecifics,
iOS: DarwinNotificationDetails(),
);
}
static Future requestPermissions() async {
if (Platform.isAndroid) {
var status = await Permission.notification.status;
if (!status.isGranted) {
await Permission.notification.request();
}
} else if (Platform.isIOS) {
var status = await Permission.notification.isGranted;
if (!status) {
await Permission.notification.request();
}
}
}
@pragma('vm:entry-point') void backgroundFetchHeadlessTask(HeadlessTask task) async { print("Headless Task Triggered: ${task.taskId}"); try { await BackGroundHelper().showNotification(); } catch (e) { print("Error in headless task: $e"); } BackgroundFetch.finish(task.taskId); }
void initBackgroundFetch() { print("initBackgroundFetch"); BackgroundFetch.configure( BackgroundFetchConfig( minimumFetchInterval: 15, stopOnTerminate: false, enableHeadless: true, startOnBoot: true, ), (String taskId) async { print("[BackgroundFetch] Event received: $taskId"); await showNotification(); BackgroundFetch.finish(taskId); }, ).then((int status) { print("[BackgroundFetch] configure success: $status"); }).catchError((e) { print("[BackgroundFetch] configure ERROR: $e"); }); }
void startTimer() { _timer = Timer.periodic(Duration(seconds: 15), (timer) { showNotification(); }); }
void stopTimer() { _timer.cancel(); }
void ini() { BackgroundFetch.registerHeadlessTask(backgroundFetchHeadlessTask); } }