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 ( ) {
var initializationSettingsAndroid = new AndroidInitializationSettings ( ' app_icon ' ) ;
var initializationSettingsIOS = IOSInitializationSettings ( onDidReceiveLocalNotification: null ) ;
var initializationSettings = InitializationSettings ( initializationSettingsAndroid , initializationSettingsIOS ) ;
flutterLocalNotificationsPlugin . initialize ( initializationSettings , onSelectNotification: _onNotificationClick ) ;
}
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 ' , ' HMG ' , importance: Importance . Max , priority: Priority . High , ticker: ' ticker ' , vibrationPattern: _vibrationPattern ( ) ) ;
var iOSPlatformChannelSpecifics = IOSNotificationDetails ( ) ;
var platformChannelSpecifics = NotificationDetails ( androidPlatformChannelSpecifics , iOSPlatformChannelSpecifics ) ;
await flutterLocalNotificationsPlugin . show ( _randomNumber ( ) , title , subtitle , platformChannelSpecifics , payload: payload ) ;
} ) ;
}
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 ' , ' 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 = IOSNotificationDetails ( sound: ' slow_spring_board.aiff ' ) ;
///change it to be as ionic
// var platformChannelSpecifics = NotificationDetails(
// androidPlatformChannelSpecifics, 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 ' , ' repeatDailyAtTime description ' ) ;
var iOSPlatformChannelSpecifics = IOSNotificationDetails ( ) ;
// 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 ' , ' show weekly description ' ) ;
var iOSPlatformChannelSpecifics = IOSNotificationDetails ( ) ;
// 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 ( ) ;
}
}