Show error dialog box when error notification tapped (#2246)

This commit is contained in:
Imran Remtulla
2025-04-18 16:05:16 -04:00
parent 10634e8ed2
commit 6af1748a78
2 changed files with 59 additions and 8 deletions

View File

@ -109,11 +109,13 @@ void main() async {
); );
SystemChrome.setEnabledSystemUIMode(SystemUiMode.edgeToEdge); SystemChrome.setEnabledSystemUIMode(SystemUiMode.edgeToEdge);
} }
final np = NotificationsProvider();
await np.initialize();
runApp(MultiProvider( runApp(MultiProvider(
providers: [ providers: [
ChangeNotifierProvider(create: (context) => AppsProvider()), ChangeNotifierProvider(create: (context) => AppsProvider()),
ChangeNotifierProvider(create: (context) => SettingsProvider()), ChangeNotifierProvider(create: (context) => SettingsProvider()),
Provider(create: (context) => NotificationsProvider()), Provider(create: (context) => np),
Provider(create: (context) => LogsProvider()) Provider(create: (context) => LogsProvider())
], ],
child: EasyLocalization( child: EasyLocalization(
@ -168,6 +170,7 @@ class _ObtainiumState extends State<Obtainium> {
SettingsProvider settingsProvider = context.watch<SettingsProvider>(); SettingsProvider settingsProvider = context.watch<SettingsProvider>();
AppsProvider appsProvider = context.read<AppsProvider>(); AppsProvider appsProvider = context.read<AppsProvider>();
LogsProvider logs = context.read<LogsProvider>(); LogsProvider logs = context.read<LogsProvider>();
NotificationsProvider notifs = context.read<NotificationsProvider>();
if (settingsProvider.prefs == null) { if (settingsProvider.prefs == null) {
settingsProvider.initializeSettings(); settingsProvider.initializeSettings();
@ -211,6 +214,10 @@ class _ObtainiumState extends State<Obtainium> {
} }
} }
WidgetsBinding.instance.addPostFrameCallback((_) {
notifs.checkLaunchByNotif();
});
return DynamicColorBuilder( return DynamicColorBuilder(
builder: (ColorScheme? lightDynamic, ColorScheme? darkDynamic) { builder: (ColorScheme? lightDynamic, ColorScheme? darkDynamic) {
// Decide on a colour/brightness scheme based on OS and user settings // Decide on a colour/brightness scheme based on OS and user settings

View File

@ -2,7 +2,9 @@
// Contains a set of pre-defined ObtainiumNotification objects that should be used throughout the app // Contains a set of pre-defined ObtainiumNotification objects that should be used throughout the app
import 'package:easy_localization/easy_localization.dart'; import 'package:easy_localization/easy_localization.dart';
import 'package:flutter/material.dart';
import 'package:flutter_local_notifications/flutter_local_notifications.dart'; import 'package:flutter_local_notifications/flutter_local_notifications.dart';
import 'package:obtainium/main.dart';
import 'package:obtainium/providers/settings_provider.dart'; import 'package:obtainium/providers/settings_provider.dart';
import 'package:obtainium/providers/source_provider.dart'; import 'package:obtainium/providers/source_provider.dart';
@ -16,10 +18,11 @@ class ObtainiumNotification {
Importance importance; Importance importance;
int? progPercent; int? progPercent;
bool onlyAlertOnce; bool onlyAlertOnce;
String? payload;
ObtainiumNotification(this.id, this.title, this.message, this.channelCode, ObtainiumNotification(this.id, this.title, this.message, this.channelCode,
this.channelName, this.channelDescription, this.importance, this.channelName, this.channelDescription, this.importance,
{this.onlyAlertOnce = false, this.progPercent}); {this.onlyAlertOnce = false, this.progPercent, this.payload});
} }
class UpdateNotification extends ObtainiumNotification { class UpdateNotification extends ObtainiumNotification {
@ -88,7 +91,8 @@ class ErrorCheckingUpdatesNotification extends ObtainiumNotification {
'BG_UPDATE_CHECK_ERROR', 'BG_UPDATE_CHECK_ERROR',
tr('errorCheckingUpdatesNotifChannel'), tr('errorCheckingUpdatesNotifChannel'),
tr('errorCheckingUpdatesNotifDescription'), tr('errorCheckingUpdatesNotifDescription'),
Importance.high); Importance.high,
payload: "${tr('errorCheckingUpdates')}\n$error");
} }
class AppsRemovedNotification extends ObtainiumNotification { class AppsRemovedNotification extends ObtainiumNotification {
@ -173,11 +177,48 @@ class NotificationsProvider {
}; };
Future<void> initialize() async { Future<void> initialize() async {
isInitialized = await notifications.initialize(const InitializationSettings( isInitialized = await notifications.initialize(
android: AndroidInitializationSettings('ic_notification'))) ?? const InitializationSettings(
android: AndroidInitializationSettings('ic_notification')),
onDidReceiveNotificationResponse: (NotificationResponse response) {
_showNotificationPayload(response.payload);
},
) ??
false; false;
} }
checkLaunchByNotif() async {
final NotificationAppLaunchDetails? launchDetails =
await notifications.getNotificationAppLaunchDetails();
if (launchDetails?.didNotificationLaunchApp ?? false) {
_showNotificationPayload(launchDetails!.notificationResponse?.payload,
doublePop: true);
}
}
_showNotificationPayload(String? payload, {bool doublePop = false}) {
var title = (payload ?? '\n\n').split('\n').first;
var content = (payload ?? '\n\n').split('\n').sublist(1).join('\n');
globalNavigatorKey.currentState?.push(
PageRouteBuilder(
pageBuilder: (context, _, __) => AlertDialog(
title: Text(title),
content: Text(content),
actions: [
TextButton(
onPressed: () {
Navigator.of(context).pop(null);
if (doublePop) {
Navigator.of(context).pop(null);
}
},
child: Text(tr('ok'))),
],
),
),
);
}
Future<void> cancel(int id) async { Future<void> cancel(int id) async {
if (!isInitialized) { if (!isInitialized) {
await initialize(); await initialize();
@ -195,7 +236,8 @@ class NotificationsProvider {
Importance importance, Importance importance,
{bool cancelExisting = false, {bool cancelExisting = false,
int? progPercent, int? progPercent,
bool onlyAlertOnce = false}) async { bool onlyAlertOnce = false,
String? payload}) async {
if (cancelExisting) { if (cancelExisting) {
await cancel(id); await cancel(id);
} }
@ -216,7 +258,8 @@ class NotificationsProvider {
maxProgress: 100, maxProgress: 100,
showProgress: progPercent != null, showProgress: progPercent != null,
onlyAlertOnce: onlyAlertOnce, onlyAlertOnce: onlyAlertOnce,
indeterminate: progPercent != null && progPercent < 0))); indeterminate: progPercent != null && progPercent < 0)),
payload: payload);
} }
Future<void> notify(ObtainiumNotification notif, Future<void> notify(ObtainiumNotification notif,
@ -225,5 +268,6 @@ class NotificationsProvider {
notif.channelName, notif.channelDescription, notif.importance, notif.channelName, notif.channelDescription, notif.importance,
cancelExisting: cancelExisting, cancelExisting: cancelExisting,
onlyAlertOnce: notif.onlyAlertOnce, onlyAlertOnce: notif.onlyAlertOnce,
progPercent: notif.progPercent); progPercent: notif.progPercent,
payload: notif.payload);
} }