Skip to content

Commit

Permalink
app crash when notification permission denied fixed, issue #201
Browse files Browse the repository at this point in the history
  • Loading branch information
ShashankDeepak committed Jun 12, 2024
1 parent 44d485a commit d44a8cd
Show file tree
Hide file tree
Showing 14 changed files with 119 additions and 72 deletions.
Binary file added .gradle/8.1.1/checksums/checksums.lock
Binary file not shown.
Binary file not shown.
Empty file.
Binary file not shown.
Binary file added .gradle/8.1.1/fileChanges/last-build.bin
Binary file not shown.
Binary file added .gradle/8.1.1/fileHashes/fileHashes.lock
Binary file not shown.
Empty file added .gradle/8.1.1/gc.properties
Empty file.
Binary file modified .gradle/buildOutputCleanup/buildOutputCleanup.lock
Binary file not shown.
4 changes: 2 additions & 2 deletions .gradle/buildOutputCleanup/cache.properties
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
#Sat Jun 08 20:35:59 IST 2024
gradle.version=7.5.1
#Wed Jun 12 11:38:18 IST 2024
gradle.version=8.1.1
Binary file removed .gradle/buildOutputCleanup/outputFiles.bin
Binary file not shown.
8 changes: 3 additions & 5 deletions lib/home_page.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import 'dart:ui';
import 'package:flutter_screenutil/flutter_screenutil.dart';

import 'package:flutter/material.dart';
import 'package:font_awesome_flutter/font_awesome_flutter.dart';
import 'package:opso/opso_timeline.dart';
Expand All @@ -17,7 +16,7 @@ import 'package:opso/widgets/book_mark_screen.dart';
import 'package:adaptive_theme/adaptive_theme.dart';
import 'package:opso/widgets/faq.dart';
import 'dart:math' as math;

import 'package:awesome_notifications/awesome_notifications.dart';
import 'about.dart';

class HomePage extends StatefulWidget {
Expand All @@ -30,7 +29,7 @@ class HomePage extends StatefulWidget {
class _HomePageState extends State<HomePage> {
@override
void initState() {
showNotification();
// showNotification();
super.initState();
_getInitialThemeMode();
}
Expand Down Expand Up @@ -250,8 +249,7 @@ class _HomePageState extends State<HomePage> {
Navigator.push(
context,
MaterialPageRoute(
builder: (context) =>
FAQPage(),
builder: (context) => FAQPage(),
),
);
},
Expand Down
11 changes: 5 additions & 6 deletions lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import 'package:opso/programs%20screen/social_winter_of_code.dart';
import 'package:opso/services/notificationService.dart';
import 'home_page.dart';
import 'package:adaptive_theme/adaptive_theme.dart';
import 'splash_screen.dart';
import 'splash_screen.dart';

void main() async {
WidgetsFlutterBinding.ensureInitialized();
Expand All @@ -35,7 +35,7 @@ class OpSoApp extends StatelessWidget {
dark: ThemeData.dark(),
initial: AdaptiveThemeMode.system,
builder: (theme, darkTheme) => MaterialApp(
initialRoute: '/splash_screen',
initialRoute: '/splash_screen',
routes: {
"/progarm_page": (context) => const HomePage(),
"/girl_script_summer_of_code": (context) => const GSSOCScreen(),
Expand All @@ -50,7 +50,7 @@ class OpSoApp extends StatelessWidget {
const MajorLeagueHackingFellowship(),
"/linux_foundation": (context) => const LinuxFoundation(),
"/landing_page": (context) => const LandingPage(),
'/splash_screen': (context) => SplashScreen(),
'/splash_screen': (context) => SplashScreen(),
},
title: 'OpSo',
debugShowCheckedModeBanner: false,
Expand All @@ -60,10 +60,9 @@ class OpSoApp extends StatelessWidget {
// primarySwatch: Colors.blue,
// visualDensity: VisualDensity.adaptivePlatformDensity,
// ),
home: const HomePage(),

// home: const HomePage(),
),
);
});
}
}
}
128 changes: 89 additions & 39 deletions lib/services/notificationService.dart
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,10 @@ class NotificationService {
debug: true,
);

bool isAllowed = await AwesomeNotifications().isNotificationAllowed();
if (!isAllowed) {
await AwesomeNotifications().requestPermissionToSendNotifications();
}
// bool isAllowed = await AwesomeNotifications().isNotificationAllowed();
// if (!isAllowed) {
// await AwesomeNotifications().requestPermissionToSendNotifications();
// }

await AwesomeNotifications().setListeners(
onActionReceivedMethod: onActionReceivedMethod,
Expand Down Expand Up @@ -70,7 +70,7 @@ class NotificationService {
}
}

static Future<void> showNotification(
static Future<bool> showNotification(
{required final String title,
required final String body,
final String? summary,
Expand All @@ -83,29 +83,61 @@ class NotificationService {
final bool scheduled = false,
final int? interval}) async {
assert(!scheduled || (scheduled && interval != null));
await AwesomeNotifications().createNotification(
content: NotificationContent(
id: -1,
channelKey: 'high_importance_channel',
title: title,
body: body,
actionType: actionType,
notificationLayout: notificationLayout,
summary: summary,
category: category,
payload: payload,
bigPicture: bigPicture,
),
actionButtons: actionButtons,
schedule: scheduled
? NotificationInterval(
interval: interval,
timeZone:
await AwesomeNotifications().getLocalTimeZoneIdentifier(),
preciseAlarm: true,
)
: null,
);
bool allowed =
await AwesomeNotifications().requestPermissionToSendNotifications();
if (allowed) {
await AwesomeNotifications().createNotification(
content: NotificationContent(
id: -1,
channelKey: 'high_importance_channel',
title: title,
body: body,
actionType: actionType,
notificationLayout: notificationLayout,
summary: summary,
category: category,
payload: payload,
bigPicture: bigPicture,
),
actionButtons: actionButtons,
schedule: scheduled
? NotificationInterval(
interval: interval,
timeZone:
await AwesomeNotifications().getLocalTimeZoneIdentifier(),
preciseAlarm: true,
)
: null,
);
} else {
allowed = await AwesomeNotifications().isNotificationAllowed();
if (allowed) {
await AwesomeNotifications().createNotification(
content: NotificationContent(
id: -1,
channelKey: 'high_importance_channel',
title: title,
body: body,
actionType: actionType,
notificationLayout: notificationLayout,
summary: summary,
category: category,
payload: payload,
bigPicture: bigPicture,
),
actionButtons: actionButtons,
schedule: scheduled
? NotificationInterval(
interval: interval,
timeZone:
await AwesomeNotifications().getLocalTimeZoneIdentifier(),
preciseAlarm: true,
)
: null,
);
}
}
return allowed;
}

static Future<void> scheduleNotificationsForEvent(
Expand All @@ -132,17 +164,35 @@ class NotificationService {
);
}

static Future<void> _scheduleNotification(
static Future<bool> _scheduleNotification(
String description, String body, DateTime dateTime) async {
await AwesomeNotifications().createNotification(
content: NotificationContent(
id: DateTime.now().millisecondsSinceEpoch.remainder(100000),
channelKey: 'high_importance_channel',
title: description,
body: body,
notificationLayout: NotificationLayout.Default,
),
schedule: NotificationCalendar.fromDate(date: dateTime),
);
if (await AwesomeNotifications().isNotificationAllowed()) {
await AwesomeNotifications().createNotification(
content: NotificationContent(
id: DateTime.now().millisecondsSinceEpoch.remainder(100000),
channelKey: 'high_importance_channel',
title: description,
body: body,
notificationLayout: NotificationLayout.Default,
),
schedule: NotificationCalendar.fromDate(date: dateTime),
);
return true;
} else {
bool allowed = await AwesomeNotifications().isNotificationAllowed();
if (allowed) {
await AwesomeNotifications().createNotification(
content: NotificationContent(
id: DateTime.now().millisecondsSinceEpoch.remainder(100000),
channelKey: 'high_importance_channel',
title: description,
body: body,
notificationLayout: NotificationLayout.Default,
),
schedule: NotificationCalendar.fromDate(date: dateTime),
);
}
return allowed;
}
}
}
40 changes: 20 additions & 20 deletions pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -260,26 +260,26 @@ packages:
dependency: transitive
description:
name: leak_tracker
sha256: "7f0df31977cb2c0b88585095d168e689669a2cc9b97c309665e3386f3e9d341a"
sha256: "78eb209deea09858f5269f5a5b02be4049535f568c07b275096836f01ea323fa"
url: "https://pub.dev"
source: hosted
version: "10.0.4"
version: "10.0.0"
leak_tracker_flutter_testing:
dependency: transitive
description:
name: leak_tracker_flutter_testing
sha256: "06e98f569d004c1315b991ded39924b21af84cf14cc94791b8aea337d25b57f8"
sha256: b46c5e37c19120a8a01918cfaf293547f47269f7cb4b0058f21531c2465d6ef0
url: "https://pub.dev"
source: hosted
version: "3.0.3"
version: "2.0.1"
leak_tracker_testing:
dependency: transitive
description:
name: leak_tracker_testing
sha256: "6ba465d5d76e67ddf503e1161d1f4a6bc42306f9d66ca1e8f079a47290fb06d3"
sha256: a597f72a664dbd293f3bfc51f9ba69816f84dcd403cdac7066cb3f6003f3ab47
url: "https://pub.dev"
source: hosted
version: "3.0.1"
version: "2.0.1"
lints:
dependency: transitive
description:
Expand Down Expand Up @@ -308,10 +308,10 @@ packages:
dependency: transitive
description:
name: meta
sha256: "7687075e408b093f36e6bbf6c91878cc0d4cd10f409506f7bc996f68220b9136"
sha256: d584fa6707a52763a52446f02cc621b077888fb63b93bbcb1143a7be5a0c0c04
url: "https://pub.dev"
source: hosted
version: "1.12.0"
version: "1.11.0"
multi_select_flutter:
dependency: "direct main"
description:
Expand Down Expand Up @@ -396,10 +396,10 @@ packages:
dependency: transitive
description:
name: shared_preferences_android
sha256: "93d0ec9dd902d85f326068e6a899487d1f65ffcd5798721a95330b26c8131577"
sha256: "1ee8bf911094a1b592de7ab29add6f826a7331fb854273d55918693d5364a1f2"
url: "https://pub.dev"
source: hosted
version: "2.2.3"
version: "2.2.2"
shared_preferences_foundation:
dependency: transitive
description:
Expand Down Expand Up @@ -489,10 +489,10 @@ packages:
dependency: transitive
description:
name: test_api
sha256: "9955ae474176f7ac8ee4e989dadfb411a58c30415bcfb648fa04b2b8a03afa7f"
sha256: "5c2f730018264d276c20e4f1503fd1308dfbbae39ec8ee63c5236311ac06954b"
url: "https://pub.dev"
source: hosted
version: "0.7.0"
version: "0.6.1"
timeline_tile:
dependency: "direct main"
description:
Expand Down Expand Up @@ -521,10 +521,10 @@ packages:
dependency: transitive
description:
name: url_launcher_android
sha256: ceb2625f0c24ade6ef6778d1de0b2e44f2db71fded235eb52295247feba8c5cf
sha256: "17cd5e205ea615e2c6ea7a77323a11712dffa0720a8a90540db57a01347f9ad9"
url: "https://pub.dev"
source: hosted
version: "6.3.3"
version: "6.3.2"
url_launcher_ios:
dependency: transitive
description:
Expand Down Expand Up @@ -609,10 +609,10 @@ packages:
dependency: transitive
description:
name: vm_service
sha256: "3923c89304b715fb1eb6423f017651664a03bf5f4b29983627c4da791f74a4ec"
sha256: b3d56ff4341b8f182b96aceb2fa20e3dcb336b9f867bc0eafc0de10f1048e957
url: "https://pub.dev"
source: hosted
version: "14.2.1"
version: "13.0.0"
web:
dependency: transitive
description:
Expand All @@ -625,10 +625,10 @@ packages:
dependency: transitive
description:
name: win32
sha256: a79dbe579cb51ecd6d30b17e0cae4e0ea15e2c0e66f69ad4198f22a6789e94f4
sha256: "0eaf06e3446824099858367950a813472af675116bf63f008a4c2a75ae13e9cb"
url: "https://pub.dev"
source: hosted
version: "5.5.1"
version: "5.5.0"
xdg_directories:
dependency: transitive
description:
Expand All @@ -654,5 +654,5 @@ packages:
source: hosted
version: "3.1.2"
sdks:
dart: ">=3.4.0 <4.0.0"
flutter: ">=3.22.0"
dart: ">=3.3.3 <4.0.0"
flutter: ">=3.19.0"

0 comments on commit d44a8cd

Please sign in to comment.