@ -28,109 +28,158 @@ class LocalNotification {
}
}
_initialize ( ) {
var initializationSettingsAndroid = new AndroidInitializationSettings ( ' app_icon ' ) ;
var initializationSettingsIOS = IOSInitializationSettings ( onDidReceiveLocalNotification: null ) ;
var initializationSettings = InitializationSettings ( android: initializationSettingsAndroid , iOS: initializationSettingsIOS ) ;
flutterLocalNotificationsPlugin . initialize ( initializationSettings , onSelectNotification: _onNotificationClick ) ;
}
var _random = new Random ( ) ;
_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 ;
/ / } , }
/ /
/ / ,
/ /
/ / ) ;
}
_randomNumber ( { int from = 100000 } ) {
return _random . nextInt ( from ) ;
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 } ' ) ;
}
}
_vibrationPattern ( ) {
var vibrationPattern = Int64List ( 4 ) ;
vibrationPattern [ 0 ] = 0 ;
vibrationPattern [ 1 ] = 1000 ;
vibrationPattern [ 2 ] = 5000 ;
vibrationPattern [ 3 ] = 2000 ;
var _random = new Random ( ) ;
return vibrationPattern ;
}
_randomNumber ( { int from = 100000 } ) {
return _random . nextInt ( from ) ;
}
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 = IOSNotificationDetails ( ) ;
var platformChannelSpecifics = NotificationDetails ( android: androidPlatformChannelSpecifics , iOS: iOSPlatformChannelSpecifics ) ;
await flutterLocalNotificationsPlugin . show ( _randomNumber ( ) , title , subtitle , platformChannelSpecifics , payload: payload ) . catchError ( ( err ) {
print ( err ) ;
} ) ;
} ) ;
}
_vibrationPattern ( ) {
var vibrationPattern = Int64List ( 4 ) ;
vibrationPattern [ 0 ] = 0 ;
vibrationPattern [ 1 ] = 1000 ;
vibrationPattern [ 2 ] = 5000 ;
vibrationPattern [ 3 ] = 2000 ;
return vibrationPattern ;
}
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 = IOSNotificationDetails ( sound: ' slow_spring_board.aiff ' ) ;
/ / / change it to be as ionic
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 . schedule ( 0 , title , description , scheduledNotificationDateTime , platformChannelSpecifics ) ;
}
await flutterLocalNotificationsPlugin . show ( _randomNumber ( ) , title , subtitle , platformChannelSpecifics , payload: payload ) . catchError ( ( err ) {
print ( err ) ;
} ) ;
} ) ;
}
/ / / 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 = 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 ) ;
}
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 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 = 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 ) ;
}
/ / / 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 ) ;
}
String _toTwoDigitString ( int value ) {
return value . toString ( ) . padLeft ( 2 , ' 0 ' ) ;
}
/ / / 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 ) ;
}
Future cancelNotification ( ) async {
await flutterLocalNotificationsPlugin . cancel ( 0 ) ;
}
String _toTwoDigitString ( int value ) {
return value . toString ( ) . padLeft ( 2 , ' 0 ' ) ;
}
Future cancelAllNotifications ( ) async {
await flutterLocalNotificationsPlugin . cancelAll ( ) ;
}
Future cancelNotification ( ) async {
await flutterLocalNotificationsPlugin . cancel ( 0 ) ;
}
Future cancelAllNotifications ( ) async {
await flutterLocalNotificationsPlugin . cancelAll ( ) ;
} }