Skip to content

Commit

Permalink
flutter native timezone get location and save to the local storage ge…
Browse files Browse the repository at this point in the history
…tStorage
  • Loading branch information
ZainNaqvi committed Jul 31, 2022
1 parent 19dbfa6 commit 17c240a
Show file tree
Hide file tree
Showing 5 changed files with 71 additions and 19 deletions.
56 changes: 39 additions & 17 deletions lib/services/notification_services.dart
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
import 'package:example_todo_sqflite/models/task_model.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter_local_notifications/flutter_local_notifications.dart';
import 'package:flutter_native_timezone/flutter_native_timezone.dart';
import 'package:get/get.dart';
import 'package:timezone/data/latest_all.dart' as tz;
import 'package:timezone/timezone.dart' as tz;

class NotificationServices {

FlutterLocalNotificationsPlugin flutterLocalNotificationsPlugin =
FlutterLocalNotificationsPlugin(); //

initializeNotification() async {

_configureLocalTimeZone();
//tz.initializeTimeZones();
final IOSInitializationSettings initializationSettingsIOS =
IOSInitializationSettings(
Expand All @@ -33,7 +34,7 @@ class NotificationServices {
}

displayNotification({required String title, required String body}) async {
print("doing test");
print("Diplay Notificatoin...");
// ignore: prefer_const_constructors
var androidPlatformChannelSpecifics = AndroidNotificationDetails(
'your channel id', 'your channel name',
Expand All @@ -51,25 +52,46 @@ class NotificationServices {
);
}

Future<void> scheduledNotification() async {
tz.initializeTimeZones();
tz.setLocalLocation(tz.getLocation('America/Detroit'));
Future<void> scheduledNotification(int hours, int min, UserTask task) async {
int seconds = min;

print("Scheduled Notificatin is trigger");
await flutterLocalNotificationsPlugin.zonedSchedule(
0,
'scheduled title',
'scheduled body',
tz.TZDateTime.now(tz.local).add(const Duration(seconds: 5)),
const NotificationDetails(
android: AndroidNotificationDetails(
'your channel id', 'your channel name',
channelDescription: 'your channel description')),
androidAllowWhileIdle: true,
uiLocalNotificationDateInterpretation:
UILocalNotificationDateInterpretation.absoluteTime);
0,
'scheduled title',
'scheduled body',
_currentTime(hours, min),
// tz.TZDateTime.now(tz.local).add(Duration(seconds: 1)),
const NotificationDetails(
android: AndroidNotificationDetails(
'your channel id', 'your channel name',
channelDescription: 'your channel description')),
androidAllowWhileIdle: true,
uiLocalNotificationDateInterpretation:
UILocalNotificationDateInterpretation.absoluteTime,
matchDateTimeComponents: DateTimeComponents.time,
);
print("Scheduled Notificatin is trigger ending ");
}

tz.TZDateTime _currentTime(int hour, int min) {
final tz.TZDateTime now = tz.TZDateTime.now(tz.local);
tz.TZDateTime scheduledDate =
tz.TZDateTime(tz.local, now.year, now.month, now.day, hour, min);

if (scheduledDate.isBefore(now)) {
scheduledDate = scheduledDate.add(const Duration(days: 1));
}
return scheduledDate;
}

Future<void> _configureLocalTimeZone() async {
tz.initializeTimeZones();
final String currentTimeZone =
await FlutterNativeTimezone.getLocalTimezone();
tz.setLocalLocation(tz.getLocation(currentTimeZone));
}

void requestIOSPermissions() {
flutterLocalNotificationsPlugin
.resolvePlatformSpecificImplementation<
Expand Down
12 changes: 10 additions & 2 deletions lib/ui/home_page.dart
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import 'dart:developer';


import 'package:date_picker_timeline/date_picker_timeline.dart';
import 'package:example_todo_sqflite/controllers/add_task_controller.dart';
Expand Down Expand Up @@ -72,6 +72,14 @@ class _HomePageState extends State<HomePage> {
UserTask task = _taskController.taskList[index];

if (task.repeat == 'Daily') {
DateTime date = DateFormat.jm().parse(task.startTime.toString());

var myTime = DateFormat('HH:mm').format(date);
notifyHelper.scheduledNotification(
int.parse(myTime.split(":")[0]),
int.parse(myTime.split(":")[1]),
task,
);
return AnimationConfiguration.staggeredList(
position: index,
child: SlideAnimation(
Expand Down Expand Up @@ -313,7 +321,7 @@ class _HomePageState extends State<HomePage> {
: "Activated Dark Theme",
);

await notifyHelper.scheduledNotification();
// await notifyHelper.scheduledNotification();
},
icon: Icon(
Get.isDarkMode ? Icons.wb_sunny_rounded : 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 @@ -6,11 +6,13 @@ import FlutterMacOS
import Foundation

import flutter_local_notifications
import flutter_native_timezone
import path_provider_macos
import sqflite

func RegisterGeneratedPlugins(registry: FlutterPluginRegistry) {
FlutterLocalNotificationsPlugin.register(with: registry.registrar(forPlugin: "FlutterLocalNotificationsPlugin"))
FlutterNativeTimezonePlugin.register(with: registry.registrar(forPlugin: "FlutterNativeTimezonePlugin"))
PathProviderPlugin.register(with: registry.registrar(forPlugin: "PathProviderPlugin"))
SqflitePlugin.register(with: registry.registrar(forPlugin: "SqflitePlugin"))
}
19 changes: 19 additions & 0 deletions pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,13 @@ packages:
url: "https://pub.dartlang.org"
source: hosted
version: "5.0.0"
flutter_native_timezone:
dependency: "direct main"
description:
name: flutter_native_timezone
url: "https://pub.dartlang.org"
source: hosted
version: "2.0.0"
flutter_screenutil:
dependency: "direct main"
description:
Expand All @@ -151,6 +158,11 @@ packages:
description: flutter
source: sdk
version: "0.0.0"
flutter_web_plugins:
dependency: transitive
description: flutter
source: sdk
version: "0.0.0"
get:
dependency: "direct main"
description:
Expand Down Expand Up @@ -193,6 +205,13 @@ packages:
url: "https://pub.dartlang.org"
source: hosted
version: "0.17.0"
js:
dependency: transitive
description:
name: js
url: "https://pub.dartlang.org"
source: hosted
version: "0.6.4"
lints:
dependency: transitive
description:
Expand Down
1 change: 1 addition & 0 deletions pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ dependencies:
flutter_screenutil: ^5.5.3+2
date_picker_timeline: ^1.2.3
flutter_staggered_animations: ^1.0.0
flutter_native_timezone: ^2.0.0

dev_dependencies:
flutter_test:
Expand Down

0 comments on commit 17c240a

Please sign in to comment.