You cannot select more than 25 topics
			Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
		
		
		
		
		
			
		
			
				
	
	
		
			116 lines
		
	
	
		
			5.2 KiB
		
	
	
	
		
			Dart
		
	
			
		
		
	
	
			116 lines
		
	
	
		
			5.2 KiB
		
	
	
	
		
			Dart
		
	
import 'dart:io';
 | 
						|
 | 
						|
import 'package:flutter/foundation.dart';
 | 
						|
import 'package:flutter/material.dart';
 | 
						|
import 'package:flutter_local_notifications/flutter_local_notifications.dart';
 | 
						|
import 'package:permission_handler/permission_handler.dart';
 | 
						|
import 'package:timezone/timezone.dart' as tz;
 | 
						|
 | 
						|
class NotificationManger {
 | 
						|
  // private constructor to avoid create object
 | 
						|
  NotificationManger._();
 | 
						|
 | 
						|
  static late FlutterLocalNotificationsPlugin localNotificationsPlugin;
 | 
						|
 | 
						|
  /// initialisation setting for all platform
 | 
						|
  /// onNotificationPressed action when notification pressed to open tap
 | 
						|
  /// onIOSNotificationPressed action when notification pressed
 | 
						|
  /// to open tap in iOS versions older than 10
 | 
						|
  static Future<void> initialisation(Function(NotificationResponse) onNotificationPressed, DidReceiveLocalNotificationCallback onIOSNotificationPressed) async {
 | 
						|
    final FlutterLocalNotificationsPlugin flutterLocalNotificationsPlugin = FlutterLocalNotificationsPlugin();
 | 
						|
    // initialise the plugin. app_icon needs to be a added as a drawable resource to the Android head project
 | 
						|
    const AndroidInitializationSettings initializationSettingsAndroid = AndroidInitializationSettings('@drawable/ic_stat_name');
 | 
						|
 | 
						|
    final DarwinInitializationSettings initializationSettingsDarwin = DarwinInitializationSettings(onDidReceiveLocalNotification: onIOSNotificationPressed);
 | 
						|
 | 
						|
    final InitializationSettings initializationSettings = InitializationSettings(android: initializationSettingsAndroid, iOS: initializationSettingsDarwin, macOS: initializationSettingsDarwin);
 | 
						|
    localNotificationsPlugin = flutterLocalNotificationsPlugin;
 | 
						|
 | 
						|
    if (Platform.isIOS) {
 | 
						|
      await localNotificationsPlugin.resolvePlatformSpecificImplementation<IOSFlutterLocalNotificationsPlugin>()?.requestPermissions(alert: true, badge: true, sound: true);
 | 
						|
    } else if (Platform.isAndroid) {
 | 
						|
      final AndroidFlutterLocalNotificationsPlugin? androidImplementation = localNotificationsPlugin.resolvePlatformSpecificImplementation<AndroidFlutterLocalNotificationsPlugin>();
 | 
						|
      final bool granted = await androidImplementation?.requestNotificationsPermission() ?? false;
 | 
						|
      print('permission value is $granted');
 | 
						|
      if (!granted) {
 | 
						|
        if (kDebugMode) {
 | 
						|
          print("-------------------- Permission Granted ------------------------");
 | 
						|
          print(granted);
 | 
						|
        }
 | 
						|
        await Permission.notification.request();
 | 
						|
      }
 | 
						|
    }
 | 
						|
 | 
						|
    await localNotificationsPlugin.initialize(initializationSettings, onDidReceiveNotificationResponse: onNotificationPressed);
 | 
						|
  } // push new notification
 | 
						|
  static const  AndroidNotificationDetails androidNotificationDetails = AndroidNotificationDetails(
 | 
						|
    'com.hmg.atoms',
 | 
						|
    'ATOMS',
 | 
						|
    channelDescription: 'Push notification service for ATOMS',
 | 
						|
    importance: Importance.max,
 | 
						|
    priority: Priority.max,
 | 
						|
    icon: "@drawable/ic_stat_name",
 | 
						|
    playSound: true,
 | 
						|
    channelShowBadge: true,
 | 
						|
    visibility: NotificationVisibility.public,
 | 
						|
    enableVibration: true,
 | 
						|
    groupKey: 'com.hmg.atoms',
 | 
						|
  );
 | 
						|
 | 
						|
  static const DarwinNotificationDetails iosNotificationDetails = DarwinNotificationDetails(
 | 
						|
    categoryIdentifier: "atoms",
 | 
						|
  );
 | 
						|
 | 
						|
  static Future<void> showNotification({required BuildContext context, required String title, required String subtext, required int hashcode, String? payload}) async {
 | 
						|
    // const AndroidNotificationDetails androidChannel = AndroidNotificationDetails(
 | 
						|
    //   'com.hmg.atoms',
 | 
						|
    //   'ATOMS',
 | 
						|
    //   channelDescription: 'Push notification service for ATOMS',
 | 
						|
    //   importance: Importance.max,
 | 
						|
    //   priority: Priority.max,
 | 
						|
    //   icon: "@drawable/ic_stat_name",
 | 
						|
    //   playSound: true,
 | 
						|
    //   channelShowBadge: true,
 | 
						|
    //   visibility: NotificationVisibility.public,
 | 
						|
    //   enableVibration: true,
 | 
						|
    //   groupKey: 'com.hmg.atoms',
 | 
						|
    // );
 | 
						|
 | 
						|
 | 
						|
    // const DarwinNotificationDetails iosNotificationDetails = DarwinNotificationDetails(
 | 
						|
    //   categoryIdentifier: "atoms",
 | 
						|
    // );
 | 
						|
 | 
						|
    const NotificationDetails platformChannel = NotificationDetails(android: androidNotificationDetails, iOS: iosNotificationDetails, macOS: iosNotificationDetails);
 | 
						|
 | 
						|
    await localNotificationsPlugin.show(hashcode, title, subtext, platformChannel, payload: payload);
 | 
						|
  }
 | 
						|
 | 
						|
  static Future scheduleNotification(
 | 
						|
      {int?id,
 | 
						|
        String? title,
 | 
						|
        String? body,
 | 
						|
        String? payLoad,
 | 
						|
        required DateTime scheduledNotificationDateTime}) async {
 | 
						|
 | 
						|
    print('time i got is ${scheduledNotificationDateTime}');
 | 
						|
    return localNotificationsPlugin.zonedSchedule(
 | 
						|
        id??0,
 | 
						|
        title,
 | 
						|
        body,
 | 
						|
        // tz.TZDateTime.now(tz.local).add(const Duration(seconds: 5)),
 | 
						|
        tz.TZDateTime.from(
 | 
						|
          scheduledNotificationDateTime,
 | 
						|
          tz.local,
 | 
						|
        ),
 | 
						|
         const NotificationDetails(android: androidNotificationDetails, iOS: iosNotificationDetails, macOS: iosNotificationDetails),
 | 
						|
        androidScheduleMode: AndroidScheduleMode.exactAllowWhileIdle,
 | 
						|
        uiLocalNotificationDateInterpretation:
 | 
						|
        UILocalNotificationDateInterpretation.absoluteTime);
 | 
						|
  }
 | 
						|
  static Future<void> cancelNotificationById(int notificationId) async {
 | 
						|
    await localNotificationsPlugin.cancel(notificationId);
 | 
						|
    print("Notification with ID $notificationId has been canceled.");
 | 
						|
  }
 | 
						|
}
 |