import ' dart:math ' ;
import ' dart:typed_data ' ;
import ' dart:ui ' ;
import ' package:flutter/cupertino.dart ' ;
import ' package:flutter_local_notifications/flutter_local_notifications.dart ' ;
final FlutterLocalNotificationsPlugin flutterLocalNotificationsPlugin = FlutterLocalNotificationsPlugin ( ) ;
class LocalNotification {
Function ( String payload ) _onNotificationClick ;
static LocalNotification _instance ;
static LocalNotification getInstance ( ) {
return _instance ;
}
static init ( { Function ( String payload ) onNotificationClick } ) {
if ( _instance = = null ) {
_instance = LocalNotification ( ) ;
_instance . _onNotificationClick = onNotificationClick ;
_instance . _initialize ( ) ;
} else {
// assert(false,(){
// //TODO fix it
// "LocalNotification Already Initialized";
// });
}
}
_initialize ( ) async {
try {
var initializationSettingsAndroid = new AndroidInitializationSettings ( ' app_icon ' ) ;
// var initializationSettingsIOS = DarwinInitializationSettings(onDidReceiveLocalNotification: null);
// var initializationSettings = InitializationSettings(android: initializationSettingsAndroid, iOS: initializationSettingsIOS);
// await flutterLocalNotificationsPlugin.initialize(
// initializationSettings,
// onDidReceiveNotificationResponse: (NotificationResponse notificationResponse) {
// switch (notificationResponse.notificationResponseType) {
// case NotificationResponseType.selectedNotification:
// // selectNotificationStream.add(notificationResponse.payload);
// break;
// case NotificationResponseType.selectedNotificationAction:
// // if (notificationResponse.actionId == navigationActionId) {
// // selectNotificationStream.add(notificationResponse.payload);
// // }
// break;
// }
// },
// onDidReceiveBackgroundNotificationResponse: notificationTapBackground,
// );
} catch ( ex ) { }
// flutterLocalNotificationsPlugin.initialize(initializationSettings, onDidReceiveNotificationResponse: (NotificationResponse notificationResponse)
// {
// switch (notificationResponse.notificationResponseType) {
// case NotificationResponseType.selectedNotification:
// // selectNotificationStream.add(notificationResponse.payload);
// break;
// case NotificationResponseType.selectedNotificationAction:
// // if (notificationResponse.actionId == navigationActionId) {
// // selectNotificationStream.add(notificationResponse.payload);
// }
// // break;
// },}
//
// ,
//
// );
}
// void notificationTapBackground(NotificationResponse notificationResponse) {
// // ignore: avoid_print
// print('notification(${notificationResponse.id}) action tapped: '
// '${notificationResponse.actionId} with'
// ' payload: ${notificationResponse.payload}');
// if (notificationResponse.input?.isNotEmpty ?? false) {
// // ignore: avoid_print
// print('notification action tapped with input: ${notificationResponse.input}');
// }
// }
var _random = new Random ( ) ;
_randomNumber ( { int from = 100000 } ) {
return _random . nextInt ( from ) ;
}
_vibrationPattern ( ) {
var vibrationPattern = Int64List ( 4 ) ;
vibrationPattern [ 0 ] = 0 ;
vibrationPattern [ 1 ] = 1000 ;
vibrationPattern [ 2 ] = 5000 ;
vibrationPattern [ 3 ] = 2000 ;
return vibrationPattern ;
}
Future showNow ( { @ required String title , @ required String subtitle , String payload } ) {
// Future.delayed(Duration(seconds: 1)).then((result) async {
// var androidPlatformChannelSpecifics = AndroidNotificationDetails('com.hmg.local_notification', 'HMG',
// channelDescription: 'HMG', importance: Importance.max, priority: Priority.high, ticker: 'ticker', vibrationPattern: _vibrationPattern());
// var iOSPlatformChannelSpecifics = DarwinNotificationDetails();
// var platformChannelSpecifics = NotificationDetails(android: androidPlatformChannelSpecifics, iOS: iOSPlatformChannelSpecifics);
// await flutterLocalNotificationsPlugin.show(_randomNumber(), title, subtitle, platformChannelSpecifics, payload: payload).catchError((err) {
// print(err);
// });
// });
}
Future scheduleNotification ( { @ required DateTime scheduledNotificationDateTime , @ required String title , @ required String description } ) async {
///vibrationPattern
var vibrationPattern = Int64List ( 4 ) ;
vibrationPattern [ 0 ] = 0 ;
vibrationPattern [ 1 ] = 1000 ;
vibrationPattern [ 2 ] = 5000 ;
vibrationPattern [ 3 ] = 2000 ;
// var androidPlatformChannelSpecifics = AndroidNotificationDetails('active-prescriptions', 'ActivePrescriptions',
// channelDescription: 'ActivePrescriptionsDescription',
// // icon: 'secondary_icon',
// sound: RawResourceAndroidNotificationSound('slow_spring_board'),
//
// ///change it to be as ionic
// // largeIcon: DrawableResourceAndroidBitmap('sample_large_icon'),///change it to be as ionic
// vibrationPattern: vibrationPattern,
// enableLights: true,
// color: const Color.fromARGB(255, 255, 0, 0),
// ledColor: const Color.fromARGB(255, 255, 0, 0),
// ledOnMs: 1000,
// ledOffMs: 500);
// var iOSPlatformChannelSpecifics = DarwinNotificationDetails(sound: 'slow_spring_board.aiff');
// /change it to be as ionic
// var platformChannelSpecifics = NotificationDetails(android: androidPlatformChannelSpecifics, iOS: iOSPlatformChannelSpecifics);
// await flutterLocalNotificationsPlugin.schedule(0, title, description, scheduledNotificationDateTime, platformChannelSpecifics);
}
///Repeat notification every day at approximately 10:00:00 am
Future showDailyAtTime ( ) async {
var time = Time ( 10 , 0 , 0 ) ;
// var androidPlatformChannelSpecifics = AndroidNotificationDetails('repeatDailyAtTime channel id', 'repeatDailyAtTime channel name', channelDescription: 'repeatDailyAtTime description');
// var iOSPlatformChannelSpecifics = DarwinNotificationDetails();
// var platformChannelSpecifics = NotificationDetails(
// androidPlatformChannelSpecifics, iOSPlatformChannelSpecifics);
// await flutterLocalNotificationsPlugin.showDailyAtTime(
// 0,
// 'show daily title',
// 'Daily notification shown at approximately ${_toTwoDigitString(time.hour)}:${_toTwoDigitString(time.minute)}:${_toTwoDigitString(time.second)}',
// time,
// platformChannelSpecifics);
}
///Repeat notification weekly on Monday at approximately 10:00:00 am
Future showWeeklyAtDayAndTime ( ) async {
var time = Time ( 10 , 0 , 0 ) ;
// var androidPlatformChannelSpecifics = AndroidNotificationDetails('show weekly channel id', 'show weekly channel name', channelDescription: 'show weekly description');
// var iOSPlatformChannelSpecifics = DarwinNotificationDetails();
// var platformChannelSpecifics = NotificationDetails(
// androidPlatformChannelSpecifics, iOSPlatformChannelSpecifics);
// await flutterLocalNotificationsPlugin.showWeeklyAtDayAndTime(
// 0,
// 'show weekly title',
// 'Weekly notification shown on Monday at approximately ${_toTwoDigitString(time.hour)}:${_toTwoDigitString(time.minute)}:${_toTwoDigitString(time.second)}',
// Day.Monday,
// time,
// platformChannelSpecifics);
}
String _toTwoDigitString ( int value ) {
return value . toString ( ) . padLeft ( 2 , ' 0 ' ) ;
}
Future cancelNotification ( ) async {
await flutterLocalNotificationsPlugin . cancel ( 0 ) ;
}
Future cancelAllNotifications ( ) async {
await flutterLocalNotificationsPlugin . cancelAll ( ) ;
}
}