From 9dd6cffe0c55a334971e472c372306a6e94bda87 Mon Sep 17 00:00:00 2001 From: LittleSheep Date: Thu, 3 Jul 2025 00:27:30 +0800 Subject: [PATCH] :bug: Trying to fix push notification --- lib/screens/about.dart | 80 ++++++++++++++++++++++++++++++++++++++-- lib/services/notify.dart | 7 +++- 2 files changed, 82 insertions(+), 5 deletions(-) diff --git a/lib/screens/about.dart b/lib/screens/about.dart index edcf40f..b68ae85 100644 --- a/lib/screens/about.dart +++ b/lib/screens/about.dart @@ -1,24 +1,33 @@ +import 'package:device_info_plus/device_info_plus.dart'; import 'package:flutter/material.dart'; import 'package:flutter/services.dart'; +import 'package:hooks_riverpod/hooks_riverpod.dart'; +import 'package:island/pods/network.dart'; +import 'package:island/services/notify.dart'; +import 'package:island/services/udid.native.dart'; +import 'package:island/widgets/alert.dart'; +import 'package:material_symbols_icons/symbols.dart'; import 'package:package_info_plus/package_info_plus.dart'; import 'package:styled_widget/styled_widget.dart'; import 'package:url_launcher/url_launcher.dart'; import 'package:easy_localization/easy_localization.dart'; -class AboutScreen extends StatefulWidget { +class AboutScreen extends ConsumerStatefulWidget { const AboutScreen({super.key}); @override - State createState() => _AboutScreenState(); + ConsumerState createState() => _AboutScreenState(); } -class _AboutScreenState extends State { +class _AboutScreenState extends ConsumerState { PackageInfo _packageInfo = PackageInfo( appName: 'Solian', packageName: 'dev.solsynth.solian', version: '1.0.0', buildNumber: '1', ); + BaseDeviceInfo? _deviceInfo; + String? _deviceUdid; bool _isLoading = true; String? _errorMessage; @@ -26,6 +35,7 @@ class _AboutScreenState extends State { void initState() { super.initState(); _initPackageInfo(); + _initDeviceInfo(); } Future _initPackageInfo() async { @@ -49,6 +59,25 @@ class _AboutScreenState extends State { } } + Future _initDeviceInfo() async { + try { + final deviceInfoPlugin = DeviceInfoPlugin(); + _deviceInfo = await deviceInfoPlugin.deviceInfo; + _deviceUdid = await getUdid(); + if (mounted) { + setState(() {}); + } + } catch (e) { + if (mounted) { + setState(() { + _errorMessage = 'aboutScreenFailedToLoadDeviceInfo'.tr( + args: [e.toString()], + ); + }); + } + } + } + Future _launchURL(String url) async { final uri = Uri.parse(url); if (await canLaunchUrl(uri)) { @@ -127,6 +156,47 @@ class _AboutScreenState extends State { ], ), + if (_deviceInfo != null) const SizedBox(height: 16), + + if (_deviceInfo != null) + _buildSection( + context, + title: 'Device Information', + children: [ + _buildInfoItem( + context, + icon: Symbols.label, + label: 'Device Name', + value: _deviceInfo?.data['name'], + ), + _buildInfoItem( + context, + icon: Symbols.fingerprint, + label: 'Device Identifier', + value: _deviceUdid ?? 'N/A', + copyable: true, + ), + const Divider(height: 1), + _buildListTile( + context, + icon: Symbols.notifications_active, + title: 'Reactivate Push Notifications', + onTap: () async { + showLoadingModal(context); + try { + await subscribePushNotification( + ref.watch(apiClientProvider), + ); + } catch (err) { + showErrorAlert(err); + } finally { + if (context.mounted) hideLoadingModal(context); + } + }, + ), + ], + ), + const SizedBox(height: 16), // Links Card @@ -247,6 +317,7 @@ class _AboutScreenState extends State { required IconData icon, required String label, required String value, + bool copyable = false, }) { return Padding( padding: const EdgeInsets.symmetric(horizontal: 16, vertical: 12), @@ -263,11 +334,12 @@ class _AboutScreenState extends State { SelectableText( value, style: Theme.of(context).textTheme.bodyMedium, + maxLines: copyable ? 1 : null, ), ], ), ), - if (value.startsWith('http') || value.contains('@')) + if (value.startsWith('http') || value.contains('@') || copyable) IconButton( icon: const Icon(Icons.copy, size: 16), onPressed: () { diff --git a/lib/services/notify.dart b/lib/services/notify.dart index 25f6ab0..e06295c 100644 --- a/lib/services/notify.dart +++ b/lib/services/notify.dart @@ -63,7 +63,10 @@ StreamSubscription setupNotificationListener( }); } -Future subscribePushNotification(Dio apiClient) async { +Future subscribePushNotification( + Dio apiClient, { + bool detailedErrors = false, +}) async { await FirebaseMessaging.instance.requestPermission( provisional: true, alert: true, @@ -97,6 +100,8 @@ Future subscribePushNotification(Dio apiClient) async { deviceToken, !kIsWeb && (Platform.isIOS || Platform.isMacOS) ? 0 : 1, ); + } else if (detailedErrors) { + throw Exception("Failed to get device token for push notifications."); } }