Skip to content

Commit

Permalink
✨ Add request for schedule notifications for Android 13+
Browse files Browse the repository at this point in the history
  • Loading branch information
iqfareez committed Dec 25, 2023
1 parent 8e5e795 commit 2a6d33c
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 0 deletions.
18 changes: 18 additions & 0 deletions lib/notificationUtil/notification_scheduler.dart
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,26 @@ import '../views/settings/notification_page_setting.dart';
import 'notifications_helper.dart';

class MyNotifScheduler {
/// Check if app can schedule notification on Android 13+ (S+)
static Future<bool> _canScheduleNotification() async {
FlutterLocalNotificationsPlugin flutterLocalNotificationsPlugin =
FlutterLocalNotificationsPlugin();
final androidNotif =
flutterLocalNotificationsPlugin.resolvePlatformSpecificImplementation<
AndroidFlutterLocalNotificationsPlugin>();
// underlying implementation: https://github.com/MaikuB/flutter_local_notifications/blob/ca71c96ba2a245175b44471e2e41e4958d480876/flutter_local_notifications/android/src/main/java/com/dexterous/flutterlocalnotifications/FlutterLocalNotificationsPlugin.java#L2119
final res = await androidNotif?.canScheduleExactNotifications();
return res ?? false;
}

/// Schedule notification for each prayer time.
static void schedulePrayNotification(
AppLocalizations appLocalizationsInstance, List<Prayers> times) async {
if (!await _canScheduleNotification()) {
print(
'Notifications are not scheduled. Schedule notification permission is not granted huhu');
return;
}
await FlutterLocalNotificationsPlugin().cancelAll(); //reset all
var currentDateTime = DateTime.now();

Expand Down
19 changes: 19 additions & 0 deletions lib/views/app_body.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import 'package:flutter/material.dart';
import 'package:flutter_gen/gen_l10n/app_localizations.dart';
import 'package:flutter_local_notifications/flutter_local_notifications.dart';
import 'package:flutter_spinkit/flutter_spinkit.dart';
import 'package:font_awesome_flutter/font_awesome_flutter.dart';
import 'package:get_storage/get_storage.dart';
Expand Down Expand Up @@ -34,6 +35,7 @@ class _AppBodyState extends State<AppBody> {

_checkForUpdate();
_showUpdateNotes();
_requestScheduleNotificationPermission();
}

void _checkForUpdate() async {
Expand Down Expand Up @@ -62,6 +64,23 @@ class _AppBodyState extends State<AppBody> {
DateTime.now().toString()); // write something to the version key
}

/// Request schedule notification permission. The permission already requested from the onboarding page,
/// but for users that have their system upgraded, the permission will be requested here.
void _requestScheduleNotificationPermission() async {
debugPrint('Requesting notification permission');
FlutterLocalNotificationsPlugin flutterLocalNotificationsPlugin =
FlutterLocalNotificationsPlugin();
final androidNotif =
flutterLocalNotificationsPlugin.resolvePlatformSpecificImplementation<
AndroidFlutterLocalNotificationsPlugin>();
final permissionGranted =
await androidNotif?.canScheduleExactNotifications() ?? false;

if (!permissionGranted) {
androidNotif?.requestNotificationsPermission();
}
}

@override
Widget build(BuildContext context) {
return Consumer<LocationProvider>(
Expand Down

0 comments on commit 2a6d33c

Please sign in to comment.