Skip to content

Commit

Permalink
Flutter local Notification is added to the project
Browse files Browse the repository at this point in the history
  • Loading branch information
ZainNaqvi committed Jul 29, 2022
1 parent 84c21a4 commit 93041f4
Show file tree
Hide file tree
Showing 5 changed files with 159 additions and 1 deletion.
75 changes: 75 additions & 0 deletions lib/services/notification_services.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
import 'package:flutter/cupertino.dart';
import 'package:flutter_local_notifications/flutter_local_notifications.dart';
import 'package:get/get.dart';

class NotificationServices {
FlutterLocalNotificationsPlugin flutterLocalNotificationsPlugin =
FlutterLocalNotificationsPlugin(); //

initializeNotification() async {
//tz.initializeTimeZones();
final IOSInitializationSettings initializationSettingsIOS =
IOSInitializationSettings(
requestSoundPermission: false,
requestBadgePermission: false,
requestAlertPermission: false,
onDidReceiveLocalNotification: onDidReceiveLocalNotification);

final AndroidInitializationSettings androidInitializationSettingsIOS =
AndroidInitializationSettings('appicon');

final InitializationSettings initializationSettings =
InitializationSettings(
iOS: initializationSettingsIOS,
android: androidInitializationSettingsIOS,
);
await flutterLocalNotificationsPlugin.initialize(initializationSettings,
onSelectNotification: selectNotification);
}

displayNotification({required String title, required String body}) async {
print("doing test");
// ignore: prefer_const_constructors
var androidPlatformChannelSpecifics = AndroidNotificationDetails(
'your channel id', 'your channel name',
importance: Importance.max, priority: Priority.high);
var iOSPlatformChannelSpecifics = IOSNotificationDetails();
var platformChannelSpecifics = NotificationDetails(
android: androidPlatformChannelSpecifics,
iOS: iOSPlatformChannelSpecifics);
await flutterLocalNotificationsPlugin.show(
0,
title,
body,
platformChannelSpecifics,
payload: 'Default_Sound',
);
}

void requestIOSPermissions() {
flutterLocalNotificationsPlugin
.resolvePlatformSpecificImplementation<
IOSFlutterLocalNotificationsPlugin>()
?.requestPermissions(
alert: true,
badge: true,
sound: true,
);
}

Future selectNotification(String? payload) async {
if (payload != null) {
print('notification payload: $payload');
} else {
print("Notification Done");
}
Get.to(() => Container());
}

Future onDidReceiveLocalNotification(
int? id, String? title, String? body, String? payload) async {
// display a dialog with the notification details, tap ok to go to another page

Get.dialog(Text("Welcome to the Flutter."));
}
}
26 changes: 25 additions & 1 deletion lib/ui/home_page.dart
Original file line number Diff line number Diff line change
@@ -1,9 +1,27 @@
import 'package:example_todo_sqflite/services/theme_services.dart';
import 'package:flutter/material.dart';
import 'package:get/get.dart';

class HomePage extends StatelessWidget {
import '../services/notification_services.dart';

class HomePage extends StatefulWidget {
const HomePage({Key? key}) : super(key: key);

@override
State<HomePage> createState() => _HomePageState();
}

class _HomePageState extends State<HomePage> {
late NotificationServices notifyHelper;
@override
void initState() {
notifyHelper = NotificationServices();
notifyHelper.initializeNotification();
notifyHelper.requestIOSPermissions();
// TODO: implement initState
super.initState();
}

@override
Widget build(BuildContext context) {
return Scaffold(
Expand All @@ -17,6 +35,12 @@ class HomePage extends StatelessWidget {
leading: IconButton(
onPressed: () {
ThemeServices().updateTheme();
notifyHelper.displayNotification(
title: "title",
body: Get.isDarkMode
? "Activated Dark Theme"
: "Activated Light Theme.",
);
},
icon: Icon(Icons.nightlight_round),
),
Expand Down
2 changes: 2 additions & 0 deletions macos/Flutter/GeneratedPluginRegistrant.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,12 @@
import FlutterMacOS
import Foundation

import flutter_local_notifications
import path_provider_macos
import sqflite

func RegisterGeneratedPlugins(registry: FlutterPluginRegistry) {
FlutterLocalNotificationsPlugin.register(with: registry.registrar(forPlugin: "FlutterLocalNotificationsPlugin"))
PathProviderPlugin.register(with: registry.registrar(forPlugin: "PathProviderPlugin"))
SqflitePlugin.register(with: registry.registrar(forPlugin: "SqflitePlugin"))
}
56 changes: 56 additions & 0 deletions pubspec.lock
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
# Generated by pub
# See https://dart.dev/tools/pub/glossary#lockfile
packages:
args:
dependency: transitive
description:
name: args
url: "https://pub.dartlang.org"
source: hosted
version: "2.3.1"
async:
dependency: transitive
description:
Expand Down Expand Up @@ -50,6 +57,13 @@ packages:
url: "https://pub.dartlang.org"
source: hosted
version: "1.0.5"
dbus:
dependency: transitive
description:
name: dbus
url: "https://pub.dartlang.org"
source: hosted
version: "0.7.7"
fake_async:
dependency: transitive
description:
Expand Down Expand Up @@ -83,6 +97,27 @@ packages:
url: "https://pub.dartlang.org"
source: hosted
version: "2.0.1"
flutter_local_notifications:
dependency: "direct main"
description:
name: flutter_local_notifications
url: "https://pub.dartlang.org"
source: hosted
version: "9.7.0"
flutter_local_notifications_linux:
dependency: transitive
description:
name: flutter_local_notifications_linux
url: "https://pub.dartlang.org"
source: hosted
version: "0.5.0+1"
flutter_local_notifications_platform_interface:
dependency: transitive
description:
name: flutter_local_notifications_platform_interface
url: "https://pub.dartlang.org"
source: hosted
version: "5.0.0"
flutter_test:
dependency: "direct dev"
description: flutter
Expand Down Expand Up @@ -186,6 +221,13 @@ packages:
url: "https://pub.dartlang.org"
source: hosted
version: "2.1.0"
petitparser:
dependency: transitive
description:
name: petitparser
url: "https://pub.dartlang.org"
source: hosted
version: "5.0.0"
platform:
dependency: transitive
description:
Expand Down Expand Up @@ -275,6 +317,13 @@ packages:
url: "https://pub.dartlang.org"
source: hosted
version: "0.4.9"
timezone:
dependency: transitive
description:
name: timezone
url: "https://pub.dartlang.org"
source: hosted
version: "0.8.0"
vector_math:
dependency: transitive
description:
Expand All @@ -296,6 +345,13 @@ packages:
url: "https://pub.dartlang.org"
source: hosted
version: "0.2.0+1"
xml:
dependency: transitive
description:
name: xml
url: "https://pub.dartlang.org"
source: hosted
version: "6.1.0"
sdks:
dart: ">=2.17.6 <3.0.0"
flutter: ">=3.0.0"
1 change: 1 addition & 0 deletions pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ dependencies:
sqflite: ^2.0.3
get:
get_storage:
flutter_local_notifications: ^9.7.0

dev_dependencies:
flutter_test:
Expand Down

0 comments on commit 93041f4

Please sign in to comment.