Commit ed9ff109 authored by Indika NK's avatar Indika NK

added smart notificator with 2 functions , local and push notifications

parent a5f54e55
......@@ -36,6 +36,12 @@
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
<receiver android:name="com.dexterous.flutterlocalnotifications.ScheduledNotificationBootReceiver">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED"></action>
</intent-filter>
</receiver>
<receiver android:name="com.dexterous.flutterlocalnotifications.ScheduledNotificationReceiver" />
<!-- Don't delete the meta-data below.
This is used by the Flutter tool to generate GeneratedPluginRegistrant.java -->
<meta-data
......
import 'package:covidefender/pages/heart_rate/heart_rate_screen.dart';
import 'package:covidefender/pages/mask_detect/invoker.dart';
import 'package:covidefender/pages/notificator/ui/MainNotificationUI.dart';
import 'package:covidefender/pages/notificator/ui/notificator.dart';
import 'package:covidefender/pages/self_carer/sc_main_screen.dart';
import 'package:flutter/cupertino.dart';
......@@ -66,7 +67,7 @@ class I_guider extends StatelessWidget {
label: Text('',
style: TextStyle(fontWeight: FontWeight.bold)),
onPressed: () {
Navigator.push(context, MaterialPageRoute(builder: (context) => MessagingWidget() ));
Navigator.push(context, MaterialPageRoute(builder: (context) => MainNotificationUI() ));
},
shape: RoundedRectangleBorder(
......
import 'dart:ffi';
import 'package:flutter/material.dart';
import 'package:flutter_local_notifications/flutter_local_notifications.dart';
import 'package:oktoast/oktoast.dart';
import 'package:path_provider/path_provider.dart';
import 'dart:io' show File, Platform;
import 'package:http/http.dart' as http;
import 'package:rxdart/subjects.dart';
class NotificationPlugin {
//
FlutterLocalNotificationsPlugin flutterLocalNotificationsPlugin;
final BehaviorSubject<ReceivedNotification>
didReceivedLocalNotificationSubject =
BehaviorSubject<ReceivedNotification>();
var initializationSettings;
int awarenessLevel = 0;
NotificationPlugin._() {
init();
}
init() async {
flutterLocalNotificationsPlugin = FlutterLocalNotificationsPlugin();
if (Platform.isIOS) {
_requestIOSPermission();
}
initializePlatformSpecifics();
}
initializePlatformSpecifics() {
var initializationSettingsAndroid =
AndroidInitializationSettings('app_notf_icon');
var initializationSettingsIOS = IOSInitializationSettings(
requestAlertPermission: true,
requestBadgePermission: true,
requestSoundPermission: false,
onDidReceiveLocalNotification: (id, title, body, payload) async {
ReceivedNotification receivedNotification = ReceivedNotification(
id: id, title: title, body: body, payload: payload);
didReceivedLocalNotificationSubject.add(receivedNotification);
},
);
initializationSettings = InitializationSettings(
initializationSettingsAndroid, initializationSettingsIOS);
}
_requestIOSPermission() {
flutterLocalNotificationsPlugin
.resolvePlatformSpecificImplementation<
IOSFlutterLocalNotificationsPlugin>()
.requestPermissions(
alert: false,
badge: true,
sound: true,
);
}
setListenerForLowerVersions(Function onNotificationInLowerVersions) {
didReceivedLocalNotificationSubject.listen((receivedNotification) {
onNotificationInLowerVersions(receivedNotification);
});
}
setOnNotificationClick(Function onNotificationClick) async {
await flutterLocalNotificationsPlugin.initialize(initializationSettings,
onSelectNotification: (String payload) async {
onNotificationClick(payload);
});
}
Future<void> showNotification() async {
var androidChannelSpecifics = AndroidNotificationDetails(
'CHANNEL_ID',
'CHANNEL_NAME',
"CHANNEL_DESCRIPTION",
importance: Importance.Max,
priority: Priority.High,
playSound: true,
timeoutAfter: 5000,
styleInformation: DefaultStyleInformation(true, true),
);
var iosChannelSpecifics = IOSNotificationDetails();
var platformChannelSpecifics =
NotificationDetails(androidChannelSpecifics, iosChannelSpecifics);
await flutterLocalNotificationsPlugin.show(
0,
'Healthy Tips',
'Always Keep Your Hands Clean !', //null
platformChannelSpecifics,
payload: 'New Payload',
);
}
// Future<void> showDailyAtTime() async {
// var time = Time(21, 3, 0);
// var androidChannelSpecifics = AndroidNotificationDetails(
// 'CHANNEL_ID 4',
// 'CHANNEL_NAME 4',
// "CHANNEL_DESCRIPTION 4",
// importance: Importance.Max,
// priority: Priority.High,
// );
// var iosChannelSpecifics = IOSNotificationDetails();
// var platformChannelSpecifics =
// NotificationDetails(androidChannelSpecifics, iosChannelSpecifics);
// await flutterLocalNotificationsPlugin.showDailyAtTime(
// 0,
// 'Test Title at ${time.hour}:${time.minute}.${time.second}',
// 'Test Body', //null
// time,
// platformChannelSpecifics,
// payload: 'Test Payload',
// );
// }
// Future<void> showWeeklyAtDayTime() async {
// var time = Time(21, 5, 0);
// var androidChannelSpecifics = AndroidNotificationDetails(
// 'CHANNEL_ID 5',
// 'CHANNEL_NAME 5',
// "CHANNEL_DESCRIPTION 5",
// importance: Importance.Max,
// priority: Priority.High,
// );
// var iosChannelSpecifics = IOSNotificationDetails();
// var platformChannelSpecifics =
// NotificationDetails(androidChannelSpecifics, iosChannelSpecifics);
// await flutterLocalNotificationsPlugin.showWeeklyAtDayAndTime(
// 0,
// 'Test Title at ${time.hour}:${time.minute}.${time.second}',
// 'Test Body', //null
// Day.Saturday,
// time,
// platformChannelSpecifics,
// payload: 'Test Payload',
// );
// }
Future<void> repeatNotification() async {
RepeatInterval Freq;
if(awarenessLevel != null ){
print('::: awareness is at :'+awarenessLevel.toString());
if(awarenessLevel >= 0 && awarenessLevel < 2){
Freq = RepeatInterval.EveryMinute;
print('::: awareness is 0 - 2');
}else if(awarenessLevel >= 2 && awarenessLevel < 4){
Freq = RepeatInterval.Hourly;
print('::: awareness is 2 - 3 ');
}else if(awarenessLevel == 4){
Freq = RepeatInterval.Daily;
print('::: awareness is 4');
}else{
showToast(
"Awareness level is not compatible , please try again !",
duration: Duration(seconds: 2),
position: ToastPosition.bottom,
backgroundColor: Colors.black.withOpacity(0.8),
radius: 13.0,
textStyle: TextStyle(fontSize: 18.0),
);
}
}else{
showToast(
"Awareness level is not Availble !",
duration: Duration(seconds: 2),
position: ToastPosition.bottom,
backgroundColor: Colors.black.withOpacity(0.8),
radius: 13.0,
textStyle: TextStyle(fontSize: 18.0),
);
}
var androidChannelSpecifics = AndroidNotificationDetails(
'CHANNEL_ID 3',
'CHANNEL_NAME 3',
"CHANNEL_DESCRIPTION 3",
importance: Importance.Max,
priority: Priority.High,
styleInformation: DefaultStyleInformation(true, true),
);
var iosChannelSpecifics = IOSNotificationDetails();
var platformChannelSpecifics =
NotificationDetails(androidChannelSpecifics, iosChannelSpecifics);
await flutterLocalNotificationsPlugin.periodicallyShow(
0,
'Tips from COVIDEFENDER :D ',
'Always Keep Your Hands Clean !',
Freq,
platformChannelSpecifics,
payload: 'Test Payload',
);
}
// Future<void> scheduleNotification() async {
// var scheduleNotificationDateTime = DateTime.now().add(Duration(seconds: 5));
// var androidChannelSpecifics = AndroidNotificationDetails(
// 'CHANNEL_ID 1',
// 'CHANNEL_NAME 1',
// "CHANNEL_DESCRIPTION 1",
// icon: 'secondary_icon',
// sound: RawResourceAndroidNotificationSound('my_sound'),
// largeIcon: DrawableResourceAndroidBitmap('large_notf_icon'),
// enableLights: true,
// color: const Color.fromARGB(255, 255, 0, 0),
// ledColor: const Color.fromARGB(255, 255, 0, 0),
// ledOnMs: 1000,
// ledOffMs: 500,
// importance: Importance.Max,
// priority: Priority.High,
// playSound: true,
// timeoutAfter: 5000,
// styleInformation: DefaultStyleInformation(true, true),
// );
// var iosChannelSpecifics = IOSNotificationDetails(
// sound: 'my_sound.aiff',
// );
// var platformChannelSpecifics = NotificationDetails(
// androidChannelSpecifics,
// iosChannelSpecifics,
// );
// await flutterLocalNotificationsPlugin.schedule(
// 0,
// 'Test Title',
// 'Test Body',
// scheduleNotificationDateTime,
// platformChannelSpecifics,
// payload: 'Test Payload',
// );
// }
// Future<void> showNotificationWithAttachment() async {
// var attachmentPicturePath = await _downloadAndSaveFile(
// 'https://via.placeholder.com/800x200', 'attachment_img.jpg');
// var iOSPlatformSpecifics = IOSNotificationDetails(
// attachments: [IOSNotificationAttachment(attachmentPicturePath)],
// );
// var bigPictureStyleInformation = BigPictureStyleInformation(
// FilePathAndroidBitmap(attachmentPicturePath),
// contentTitle: '<b>Attached Image</b>',
// htmlFormatContentTitle: true,
// summaryText: 'Test Image',
// htmlFormatSummaryText: true,
// );
// var androidChannelSpecifics = AndroidNotificationDetails(
// 'CHANNEL ID 2',
// 'CHANNEL NAME 2',
// 'CHANNEL DESCRIPTION 2',
// importance: Importance.High,
// priority: Priority.High,
// styleInformation: bigPictureStyleInformation,
// );
// var notificationDetails =
// NotificationDetails(androidChannelSpecifics, iOSPlatformSpecifics);
// await flutterLocalNotificationsPlugin.show(
// 0,
// 'Title with attachment',
// 'Body with Attachment',
// notificationDetails,
// );
// }
// _downloadAndSaveFile(String url, String fileName) async {
// var directory = await getApplicationDocumentsDirectory();
// var filePath = '${directory.path}/$fileName';
// var response = await http.get(url);
// var file = File(filePath);
// await file.writeAsBytes(response.bodyBytes);
// return filePath;
// }
// Future<int> getPendingNotificationCount() async {
// List<PendingNotificationRequest> p =
// await flutterLocalNotificationsPlugin.pendingNotificationRequests();
// return p.length;
// }
// Future<void> cancelNotification() async {
// await flutterLocalNotificationsPlugin.cancel(0);
// }
// Future<void> cancelAllNotification() async {
// await flutterLocalNotificationsPlugin.cancelAll();
// }
}
NotificationPlugin notificationPlugin = NotificationPlugin._();
class ReceivedNotification {
final int id;
final String title;
final String body;
final String payload;
ReceivedNotification({
@required this.id,
@required this.title,
@required this.body,
@required this.payload,
});
}
\ No newline at end of file
import 'package:covidefender/pages/notificator/model/Notification_plugin.dart';
import 'package:flutter/material.dart';
import 'NotificationUI.dart';
class LocalNotificationScreen extends StatefulWidget {
@override
_LocalNotificationScreenState createState() =>
_LocalNotificationScreenState();
}
class _LocalNotificationScreenState extends State<LocalNotificationScreen> {
//
int count = 0;
@override
void initState() {
super.initState();
notificationPlugin
.setListenerForLowerVersions(onNotificationInLowerVersions);
notificationPlugin.setOnNotificationClick(onNotificationClick);
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
backgroundColor: Colors.green,
title: Text('Local Notifications'),
),
body: Center(
child: FlatButton(
onPressed: () async {
await notificationPlugin.showNotification();
// await notificationPlugin.scheduleNotification();
// await notificationPlugin.showNotificationWithAttachment();
await notificationPlugin.repeatNotification();
// await notificationPlugin.showDailyAtTime();
// await notificationPlugin.showWeeklyAtDayTime();
// count = await notificationPlugin.getPendingNotificationCount();
// print('Count $count');
// await notificationPlugin.cancelNotification();
// count = await notificationPlugin.getPendingNotificationCount();
// print('Count $count');
},
child: Text('Send Notification'),
),
),
);
}
onNotificationInLowerVersions(ReceivedNotification receivedNotification) {
print('Notification Received ${receivedNotification.id}');
}
onNotificationClick(String payload) {
print('Payload $payload');
Navigator.push(context, MaterialPageRoute(builder: (coontext) {
return NotificationScreen(
payload: payload,
);
}));
}
}
import 'package:flutter/material.dart';
import 'LocalNotificationUI.dart';
import 'notificator.dart';
class MainNotificationUI extends StatefulWidget {
//
final String payload;
MainNotificationUI({this.payload});
@override
_NotificationScreenState createState() => _NotificationScreenState();
}
class _NotificationScreenState extends State<MainNotificationUI> {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
backgroundColor: Colors.green,
title: Text('Smart Notifier'),
),
body: Center(
child: Column(
crossAxisAlignment: CrossAxisAlignment.stretch,
mainAxisAlignment: MainAxisAlignment.end,
children: [
Padding(
padding: const EdgeInsets.fromLTRB(8,8,8,8),
child: SizedBox(
child: ElevatedButton(
style: ElevatedButton.styleFrom(
primary: Colors.green[300],
elevation: 15,
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(12)
)
),
onPressed: (){
Navigator.push(context, MaterialPageRoute(builder: (context) => LocalNotificationScreen() ));
}, child: Text("Local Notifications")),
),
),
Padding(
padding: const EdgeInsets.fromLTRB(8,8,8,100),
child: SizedBox(
child: ElevatedButton(
style: ElevatedButton.styleFrom(
primary: Colors.green[300],
elevation: 15,
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(12)
)
),
onPressed: (){
Navigator.push(context, MaterialPageRoute(builder: (context) => MessagingWidget() ));
}, child: Text("Push Notifications")),
),
)
],
),
),
);
}
}
import 'package:flutter/material.dart';
class NotificationScreen extends StatefulWidget {
//
final String payload;
NotificationScreen({this.payload});
@override
_NotificationScreenState createState() => _NotificationScreenState();
}
class _NotificationScreenState extends State<NotificationScreen> {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('Notifications Screen'),
),
body: Center(
child: Text(widget.payload),
),
);
}
}
......@@ -51,6 +51,7 @@ class _MessagingWidgetState extends State<MessagingWidget> {
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
backgroundColor: Colors.green,
title: Text("Smart - Notificator "),
),
body: Container(
......@@ -64,7 +65,7 @@ class _MessagingWidgetState extends State<MessagingWidget> {
spreadRadius: 5)
],
gradient: LinearGradient(
colors: [Colors.blue[600], Colors.white],
colors: [Colors.green[600], Colors.white],
begin: Alignment.centerLeft,
end: Alignment.centerRight,
),
......
......@@ -36,7 +36,9 @@ dependencies:
google_fonts: ^1.1.2
flutter_sms: ^2.1.1
firebase_messaging: ^4.0.0+1
rxdart: ^0.24.1
path_provider: ^1.6.11
flutter_local_notifications: ^1.4.4+1
# The following adds the Cupertino Icons font to your application.
......@@ -44,11 +46,8 @@ dependencies:
cupertino_icons: ^1.0.2
http: ^0.12.2
#add dependencies for heart rate module
#add dependencies for heart rate module
camera:
git:
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment