Skip to content

Commit 7361c86

Browse files
committed
create a Local Notification module
1 parent 1e79b1f commit 7361c86

10 files changed

+189
-1
lines changed

README.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,14 @@ This Docs given Flutter easy implement Flutter Project
99

1010
## Screenshots
1111

12-
![App Screenshot](https://raw.githubusercontent.com/RajTechnologiesPvtLtd/flutter-moonlight/master/screenshot.png)
12+
<img src="screenshot.png" alt="Flutter Moonlight Framework" width="200"/>
1313

1414

1515
## Features
1616

1717
- Core Routes
1818
- MVC Architecture
19+
- Local Notification
1920

2021

2122
## Examples

lib/config/app_routes.dart

+2
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ class App extends RouteManager {
1414
static const String home = '${App.name}/';
1515
static const String loadMore = '${App.name}/load-more';
1616
static const String sqlCrud = '${App.name}/sql-crud';
17+
static const String localNotification = '${App.name}/local-notification';
1718
static const String example = '${App.name}/examples';
1819
static const String setting = '${App.name}/setting';
1920
// Examples
@@ -34,6 +35,7 @@ class App extends RouteManager {
3435
addRoute(App.setting, (context) => const SettingController());
3536
addRoute(App.loadMore, (context) => const LoadMoreController());
3637
addRoute(App.sqlCrud, (context) => const SqlCRUDView());
38+
addRoute(App.localNotification, (context) => const LocalNotificationView());
3739
addRoute(App.example, (context) => const ExampleController());
3840
// Examples
3941
addRoute(App.exampleAuth, (context) => const AuthView());
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
import 'package:flutter_local_notifications/flutter_local_notifications.dart';
2+
3+
class LocalNotificationService {
4+
final FlutterLocalNotificationsPlugin flutterLocalNotificationsPlugin =
5+
FlutterLocalNotificationsPlugin();
6+
7+
final AndroidInitializationSettings androidInitializationSettings =
8+
const AndroidInitializationSettings('@mipmap/ic_launcher');
9+
10+
void initializeNotification() async {
11+
InitializationSettings initializationSettings =
12+
InitializationSettings(android: androidInitializationSettings);
13+
14+
await flutterLocalNotificationsPlugin.initialize(initializationSettings);
15+
}
16+
17+
void showNotification(String title, String body) async {
18+
AndroidNotificationDetails androidNotificationDetails =
19+
const AndroidNotificationDetails(
20+
'channelId',
21+
'channelName',
22+
importance: Importance.max,
23+
priority: Priority.high,
24+
);
25+
26+
NotificationDetails notificationDetails = NotificationDetails(
27+
android: androidNotificationDetails,
28+
);
29+
30+
await flutterLocalNotificationsPlugin.show(
31+
0, title, body, notificationDetails);
32+
}
33+
34+
void scheduleNotification(String title, String body) async {
35+
AndroidNotificationDetails androidNotificationDetails =
36+
const AndroidNotificationDetails(
37+
'channelId',
38+
'channelName',
39+
importance: Importance.max,
40+
priority: Priority.high,
41+
);
42+
43+
NotificationDetails notificationDetails = NotificationDetails(
44+
android: androidNotificationDetails,
45+
);
46+
47+
await flutterLocalNotificationsPlugin.periodicallyShow(
48+
0,
49+
title,
50+
body,
51+
RepeatInterval.everyMinute,
52+
notificationDetails,
53+
);
54+
}
55+
56+
void stopScheduleNotification(id) async {
57+
await flutterLocalNotificationsPlugin.cancel(id);
58+
}
59+
60+
void stopAllScheduleNotification() async {
61+
await flutterLocalNotificationsPlugin.cancelAll();
62+
}
63+
64+
65+
}

lib/services/services.dart

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export 'local_notification_service.dart';
+61
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
import 'package:flutter/material.dart';
2+
import 'package:moonlight/services/services.dart';
3+
import '../widgets/widget.dart';
4+
5+
class LocalNotificationView extends StatefulWidget {
6+
const LocalNotificationView({Key? key}) : super(key: key);
7+
8+
@override
9+
State<LocalNotificationView> createState() => _LocalNotificationViewState();
10+
}
11+
12+
class _LocalNotificationViewState extends State<LocalNotificationView> {
13+
LocalNotificationService localNotificationService =
14+
LocalNotificationService();
15+
16+
@override
17+
void initState() {
18+
super.initState();
19+
localNotificationService.initializeNotification();
20+
}
21+
22+
@override
23+
Widget build(BuildContext context) {
24+
return Scaffold(
25+
drawer: const MoonLightDrawer("Local Notification"),
26+
appBar: AppBar(title: const Text("Local Notification")),
27+
bottomNavigationBar: BannerAdWidget(),
28+
body: Center(
29+
child: Column(
30+
mainAxisAlignment: MainAxisAlignment.center,
31+
children: [
32+
const Text("Local Notification"),
33+
const SizedBox(height: 2),
34+
ElevatedButton(
35+
child: const Text("send Notification"),
36+
onPressed: () {
37+
localNotificationService.showNotification(
38+
"Normal Notification", "This is a Body");
39+
},
40+
),
41+
const SizedBox(width: 2),
42+
ElevatedButton(
43+
child: const Text("Schedule Notification"),
44+
onPressed: () {
45+
localNotificationService.scheduleNotification(
46+
"Schedule Notification", "This is a Body");
47+
},
48+
),
49+
const SizedBox(width: 2),
50+
ElevatedButton(
51+
child: const Text("Stop All Notification"),
52+
onPressed: () {
53+
localNotificationService.stopAllScheduleNotification();
54+
},
55+
)
56+
],
57+
),
58+
),
59+
);
60+
}
61+
}

lib/views/views.dart

+1
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,4 @@ export 'setting_view.dart';
33
export 'load_more_view.dart';
44
export 'sql_crud_view.dart';
55
export 'example_view.dart';
6+
export 'local_notification_view.dart';

lib/widgets/drawer.dart

+7
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,13 @@ class MoonLightDrawer extends StatelessWidget {
5757
Navigator.of(context).popAndPushNamed(App.sqlCrud);
5858
},
5959
),
60+
DrawerTile(
61+
title: "Local Notification",
62+
icon: Icons.list,
63+
onTap: () {
64+
Navigator.of(context).popAndPushNamed(App.localNotification);
65+
},
66+
),
6067
DrawerTile(
6168
title: "Settings",
6269
icon: Icons.settings,

pubspec.lock

+49
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,27 @@ packages:
139139
url: "https://pub.dartlang.org"
140140
source: hosted
141141
version: "2.0.1"
142+
flutter_local_notifications:
143+
dependency: "direct main"
144+
description:
145+
name: flutter_local_notifications
146+
url: "https://pub.dartlang.org"
147+
source: hosted
148+
version: "12.0.4"
149+
flutter_local_notifications_linux:
150+
dependency: transitive
151+
description:
152+
name: flutter_local_notifications_linux
153+
url: "https://pub.dartlang.org"
154+
source: hosted
155+
version: "2.0.0"
156+
flutter_local_notifications_platform_interface:
157+
dependency: transitive
158+
description:
159+
name: flutter_local_notifications_platform_interface
160+
url: "https://pub.dartlang.org"
161+
source: hosted
162+
version: "6.0.0"
142163
flutter_test:
143164
dependency: "direct dev"
144165
description: flutter
@@ -233,13 +254,27 @@ packages:
233254
url: "https://pub.dartlang.org"
234255
source: hosted
235256
version: "5.0.0"
257+
platform:
258+
dependency: transitive
259+
description:
260+
name: platform
261+
url: "https://pub.dartlang.org"
262+
source: hosted
263+
version: "3.1.0"
236264
plugin_platform_interface:
237265
dependency: transitive
238266
description:
239267
name: plugin_platform_interface
240268
url: "https://pub.dartlang.org"
241269
source: hosted
242270
version: "2.1.3"
271+
process:
272+
dependency: transitive
273+
description:
274+
name: process
275+
url: "https://pub.dartlang.org"
276+
source: hosted
277+
version: "4.2.4"
243278
share_plus:
244279
dependency: "direct main"
245280
description:
@@ -350,6 +385,13 @@ packages:
350385
url: "https://pub.dartlang.org"
351386
source: hosted
352387
version: "0.4.12"
388+
timezone:
389+
dependency: transitive
390+
description:
391+
name: timezone
392+
url: "https://pub.dartlang.org"
393+
source: hosted
394+
version: "0.9.0"
353395
typed_data:
354396
dependency: transitive
355397
description:
@@ -427,6 +469,13 @@ packages:
427469
url: "https://pub.dartlang.org"
428470
source: hosted
429471
version: "0.3.3"
472+
xdg_directories:
473+
dependency: transitive
474+
description:
475+
name: xdg_directories
476+
url: "https://pub.dartlang.org"
477+
source: hosted
478+
version: "0.2.0+2"
430479
xml:
431480
dependency: transitive
432481
description:

pubspec.yaml

+1
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ dependencies:
4343
google_mobile_ads: ^2.0.1
4444
sqflite: ^2.1.0+1
4545
connectivity_plus: ^2.3.9
46+
flutter_local_notifications: ^12.0.4
4647

4748
dev_dependencies:
4849
flutter_test:

screenshot.png

114 KB
Loading

0 commit comments

Comments
 (0)