修复上架问题:应用内消息推送按钮无法正常使用

This commit is contained in:
Daisy 2024-03-14 10:04:33 +08:00
parent b60a50a08e
commit aa421496a5
3 changed files with 61 additions and 2 deletions

View File

@ -1,7 +1,11 @@
import 'dart:io';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_local_notifications/flutter_local_notifications.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';
import 'package:permission_handler/permission_handler.dart';
import 'package:star_lock/flavors.dart';
import 'package:star_lock/mine/mineSet/mineSet/mineSet_logic.dart';
import '../../../appRouters.dart';
@ -18,7 +22,7 @@ class MineSetPage extends StatefulWidget {
State<MineSetPage> createState() => _MineSetPageState();
}
class _MineSetPageState extends State<MineSetPage> {
class _MineSetPageState extends State<MineSetPage> with WidgetsBindingObserver {
final logic = Get.put(MineSetLogic());
final state = Get.find<MineSetLogic>().state;
@ -26,11 +30,25 @@ class _MineSetPageState extends State<MineSetPage> {
void initState() {
super.initState();
WidgetsBinding.instance.addObserver(this);
logic.userSettingsInfoRequest();
}
@override
void didChangeAppLifecycleState(AppLifecycleState state) {
super.didChangeAppLifecycleState(state);
if (state == AppLifecycleState.resumed) {
//
//
_checkNotificationPermission();
}
}
@override
Widget build(BuildContext context) {
_checkNotificationPermission();
return Scaffold(
backgroundColor: AppColors.mainBackgroundColor,
appBar: TitleAppBar(
@ -371,8 +389,45 @@ class _MineSetPageState extends State<MineSetPage> {
thumbColor: CupertinoColors.white,
value: state.isPushNotification.value,
onChanged: (value) {
state.isPushNotification.value = !state.isPushNotification.value;
// state.isPushNotification.value = !state.isPushNotification.value;
openAppSettings();
},
);
}
Future<void> _checkNotificationPermission() async {
bool notificationEnabled = false;
if (Platform.isAndroid) {
notificationEnabled = await state.flutterLocalNotificationsPlugin
.resolvePlatformSpecificImplementation<
AndroidFlutterLocalNotificationsPlugin>()
?.areNotificationsEnabled() ??
false;
} else if (Platform.isIOS) {
notificationEnabled = await state.flutterLocalNotificationsPlugin
.resolvePlatformSpecificImplementation<
IOSFlutterLocalNotificationsPlugin>()
?.requestPermissions(
alert: true,
badge: true,
sound: true,
) ??
false;
}
if (notificationEnabled) {
print('Notifications are enabled');
state.isPushNotification.value = true;
} else {
print('Notifications are disabled');
state.isPushNotification.value = false;
}
}
@override
void dispose() {
WidgetsBinding.instance.removeObserver(this);
super.dispose();
}
}

View File

@ -1,3 +1,4 @@
import 'package:flutter_local_notifications/flutter_local_notifications.dart';
import 'package:get/get.dart';
import 'package:star_lock/mine/mineSet/mineSet/userSettingInfoEntity.dart';
@ -13,4 +14,6 @@ class MineSetState {
var hideExpiredAccessFlag = 2.obs; //
var currentLanguage = "".obs; //
late FlutterLocalNotificationsPlugin flutterLocalNotificationsPlugin =
FlutterLocalNotificationsPlugin();
}

View File

@ -141,6 +141,7 @@ dependencies:
flutter_slidable: ^3.0.1
audio_service: ^0.18.12
app_settings: ^5.1.1
flutter_local_notifications: ^17.0.0
system_settings: ^2.0.0
dev_dependencies: