Skip to content

Commit

Permalink
fix notification bug
Browse files Browse the repository at this point in the history
  • Loading branch information
RadRasyad committed Sep 29, 2022
1 parent abc5752 commit b8f9cd5
Show file tree
Hide file tree
Showing 9 changed files with 72 additions and 76 deletions.
11 changes: 0 additions & 11 deletions lib/common/navigation.dart

This file was deleted.

31 changes: 26 additions & 5 deletions lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ FlutterLocalNotificationsPlugin();

Future<void> main() async {
WidgetsFlutterBinding.ensureInitialized();

final NotificationHelper notificationHelper = NotificationHelper();
final BackgroundService service = BackgroundService();
service.initializeIsolate();
Expand All @@ -30,26 +31,34 @@ Future<void> main() async {
runApp(const MyApp());
}

class MyApp extends StatelessWidget {
class MyApp extends StatefulWidget {
const MyApp({super.key});

@override
State<MyApp> createState() => _MyAppState();

}

class _MyAppState extends State<MyApp> {
final NotificationHelper _notificationHelper = NotificationHelper();

@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'IndoRestaurant',
theme: ThemeData(
textTheme: textTheme,
colorScheme:
ThemeData().colorScheme.copyWith(primary: const Color(0xFFE07465)),
ThemeData().colorScheme.copyWith(primary: const Color(0xFFE07465)),
),
initialRoute: SplashScreenPage.routeName,
routes: {
SplashScreenPage.routeName: (context) => const SplashScreenPage(),
HomePage.routeName: (context) => const HomePage(),
DetailPage.routeName: (context) => DetailPage(
id:
ModalRoute.of(context)?.settings.arguments as String
),
id:
ModalRoute.of(context)?.settings.arguments as String
),
SearchPage.routeName: (context) => const SearchPage(),
ReviewPage.routeName: (context) => ReviewPage(
id: ModalRoute.of(context)?.settings.arguments as String
Expand All @@ -60,4 +69,16 @@ class MyApp extends StatelessWidget {
);
}

@override
void initState() {
super.initState();
_notificationHelper
.configureSelectNotificationSubject(context ,DetailPage.routeName);
}

@override
void dispose() {
super.dispose();
selectNotificationSubject.close();
}
}
4 changes: 2 additions & 2 deletions lib/provider/scheduling_provider.dart
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ class SchedulingProvider extends ChangeNotifier {
notifyListeners();
return await AndroidAlarmManager.periodic(
const Duration(hours: 24),
2,
1,
BackgroundService.callback,
startAt: DateTimeHelper.format(),
exact: true,
Expand All @@ -25,7 +25,7 @@ class SchedulingProvider extends ChangeNotifier {
} else {
print('Scheduling Restaurant Canceled');
notifyListeners();
return await AndroidAlarmManager.cancel(2);
return await AndroidAlarmManager.cancel(1);
}
}
}
16 changes: 0 additions & 16 deletions lib/ui/home_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,6 @@ class HomePage extends StatefulWidget {

class _HomePageState extends State<HomePage> {

final NotificationHelper _notificationHelper = NotificationHelper();

@override
Widget build(BuildContext context) {
return ChangeNotifierProvider<RestaurantProvider>(
Expand All @@ -30,19 +28,5 @@ class _HomePageState extends State<HomePage> {
);
}

@override
void initState() {
super.initState();
_notificationHelper
.configureSelectNotificationSubject(DetailPage.routeName);
}

@override
void dispose() {
super.dispose();
selectNotificationSubject.close();
}


}

26 changes: 13 additions & 13 deletions lib/ui/setting_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -21,19 +21,6 @@ class SettingPage extends StatefulWidget {
class _SettingPageState extends State<SettingPage> {
final NotificationHelper _notificationHelper = NotificationHelper();

@override
void initState() {
super.initState();
_notificationHelper
.configureSelectNotificationSubject(DetailPage.routeName);
}

@override
void dispose() {
selectNotificationSubject.close();
super.dispose();
}

@override
Widget build(BuildContext context) {
return MultiProvider(
Expand All @@ -49,4 +36,17 @@ class _SettingPageState extends State<SettingPage> {
child: const RestaurantSettingPage(),
);
}

@override
void initState() {
super.initState();
_notificationHelper
.configureSelectNotificationSubject(context ,DetailPage.routeName);
}

@override
void dispose() {
super.dispose();
selectNotificationSubject.close();
}
}
6 changes: 2 additions & 4 deletions lib/utils/datetime_helper.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,16 @@ import 'package:intl/intl.dart';

class DateTimeHelper {
static DateTime format() {
// Date and Time Format

final now = DateTime.now();
final dateFormat = DateFormat('y/M/d');
const timeSpecific = "10:25:00";
const timeSpecific = "11:00:00";
final completeFormat = DateFormat('y/M/d H:m:s');

// Today Format
final todayDate = dateFormat.format(now);
final todayDateAndTime = "$todayDate $timeSpecific";
var resultToday = completeFormat.parseStrict(todayDateAndTime);

// Tomorrow Format
var formatted = resultToday.add(const Duration(days: 1));
final tomorrowDate = dateFormat.format(formatted);
final tomorrowDateAndTime = "$tomorrowDate $timeSpecific";
Expand Down
38 changes: 21 additions & 17 deletions lib/utils/notification_helper.dart
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@

import 'dart:convert';
import 'package:flutter/cupertino.dart';
import 'dart:math';
import 'package:flutter_local_notifications/flutter_local_notifications.dart';
import 'package:indorestaurant/common/navigation.dart';
import 'package:indorestaurant/data/model/restaurant.dart';
import 'package:rxdart/subjects.dart';

Expand All @@ -21,25 +21,26 @@ class NotificationHelper {
Future<void> initNotifications(
FlutterLocalNotificationsPlugin flutterLocalNotificationsPlugin) async {
var initializationSettingsAndroid =
const AndroidInitializationSettings('app_icon');
const AndroidInitializationSettings('app_icon');

var initializationSettings = InitializationSettings(
android: initializationSettingsAndroid);
var initializationSettings =
InitializationSettings(android: initializationSettingsAndroid);

await flutterLocalNotificationsPlugin.initialize(initializationSettings,
onSelectNotification: (String? payload) async {

selectNotificationSubject.add(payload ?? 'empty payload');
});
onDidReceiveNotificationResponse: (NotificationResponse? payload) async {
var data = payload?.payload;
if (payload != null) {
print('notification payload: $data');
} selectNotificationSubject.add(data ?? 'empty payload');
});
}

Future<void> showNotification(
FlutterLocalNotificationsPlugin flutterLocalNotificationsPlugin,
RestaurantResult restaurantResult) async {
var channelId = "2";
var channelName = "channel_02";
var channelId = "1";
var channelName = "channel_01";
var channelDescription = "indorestaurant channel";
_random = Random().nextInt(restaurantResult.restaurants.length);

var androidPlatformChannelSpecifics = AndroidNotificationDetails(
channelId, channelName,
Expand All @@ -52,20 +53,23 @@ class NotificationHelper {
var platformChannelSpecifics = NotificationDetails(
android: androidPlatformChannelSpecifics);

_random = Random().nextInt(restaurantResult.restaurants.length);
var _restaurant = restaurantResult.restaurants[_random];

var titleNotification = "<b>Today Recommendation</b>";
var titleRestaurant = restaurantResult.restaurants[_random].name;
var titleRestaurant = _restaurant.name;

await flutterLocalNotificationsPlugin.show(
0, titleNotification, titleRestaurant, platformChannelSpecifics,
payload: json.encode(restaurantResult.toJson()));
payload: jsonEncode(_restaurant.toJson()));
}

void configureSelectNotificationSubject(String route) {
void configureSelectNotificationSubject(BuildContext context, String route) {
selectNotificationSubject.stream.listen(
(String payload) async {
var data = RestaurantResult.fromJson(json.decode(payload));
var restaurant = data.restaurants[_random].id;
Navigation.intentWithData(route, restaurant);
var data = Restaurant.fromJson(jsonDecode(payload));
var restaurant = data.id;
Navigator.pushNamed(context, route, arguments: restaurant);
},
);
}
Expand Down
10 changes: 5 additions & 5 deletions pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ packages:
name: android_alarm_manager_plus
url: "https://pub.dartlang.org"
source: hosted
version: "2.0.7"
version: "2.0.7+1"
archive:
dependency: transitive
description:
Expand Down Expand Up @@ -145,21 +145,21 @@ packages:
name: flutter_local_notifications
url: "https://pub.dartlang.org"
source: hosted
version: "9.9.1"
version: "11.0.1"
flutter_local_notifications_linux:
dependency: transitive
description:
name: flutter_local_notifications_linux
url: "https://pub.dartlang.org"
source: hosted
version: "0.5.1"
version: "1.0.0"
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"
version: "6.0.0"
flutter_test:
dependency: "direct dev"
description: flutter
Expand Down Expand Up @@ -489,7 +489,7 @@ packages:
name: timezone
url: "https://pub.dartlang.org"
source: hosted
version: "0.8.0"
version: "0.9.0"
typed_data:
dependency: transitive
description:
Expand Down
6 changes: 3 additions & 3 deletions pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,8 @@ dependencies:
provider: ^6.0.3
cupertino_icons: ^1.0.2
intl: ^0.17.0
android_alarm_manager_plus: ^2.0.3
flutter_local_notifications: ^9.7.0
android_alarm_manager_plus: ^2.0.7+1
flutter_local_notifications: ^11.0.1
rxdart: ^0.27.5
shared_preferences: ^2.0.15
sqflite: ^2.1.0
Expand All @@ -55,7 +55,7 @@ flutter_icons:
android: "launcher_icon"
image_path: "assets/img/ic_restaurant.png"
ios: false
min_sdk_android: 21
min_sdk_android: 26

# For information on the generic Dart part of this file, see the
# following page: https://dart.dev/tools/pub/pubspec
Expand Down

0 comments on commit b8f9cd5

Please sign in to comment.