协议通知后注册推送
This commit is contained in:
@@ -29,8 +29,14 @@ class HomeController extends GetxController with BaseControllerMixin {
|
||||
@override
|
||||
void onInit() {
|
||||
super.onInit();
|
||||
initAliyunPush();
|
||||
addPushCallback();
|
||||
|
||||
// 检查是否同意过隐私政策,只有同意后才初始化推送
|
||||
if (StorageService.to.isPrivacyAgreed) {
|
||||
requestPermission();
|
||||
initAliyunPush();
|
||||
addPushCallback();
|
||||
}
|
||||
|
||||
FlutterNativeSplash.remove();
|
||||
log('page-init');
|
||||
|
||||
@@ -152,7 +158,6 @@ class HomeController extends GetxController with BaseControllerMixin {
|
||||
|
||||
// 根据登录状态和登录渠道返回不同的首页
|
||||
Widget getHomePage() {
|
||||
requestPermission();
|
||||
if (StorageService.to.isLoggedIn) {
|
||||
if (StorageService.to.loginChannel == LoginChannel.station) {
|
||||
return B_BaseWidgetsPage();
|
||||
|
||||
@@ -6,6 +6,7 @@ import 'package:flutter/material.dart';
|
||||
import 'package:flutter/services.dart';
|
||||
import 'package:get/get.dart';
|
||||
import 'package:getx_scaffold/getx_scaffold.dart';
|
||||
import 'package:permission_handler/permission_handler.dart';
|
||||
import 'package:ln_jq_app/common/login_util.dart';
|
||||
import 'package:ln_jq_app/common/model/base_model.dart';
|
||||
import 'package:ln_jq_app/common/model/vehicle_info.dart';
|
||||
@@ -17,6 +18,8 @@ import 'package:ln_jq_app/pages/login/controller.dart';
|
||||
import 'package:ln_jq_app/pages/url_host/view.dart';
|
||||
import 'package:ln_jq_app/storage_service.dart';
|
||||
|
||||
import '../c_page/message/view.dart';
|
||||
|
||||
class LoginPage extends StatefulWidget {
|
||||
const LoginPage({super.key});
|
||||
|
||||
@@ -30,6 +33,7 @@ class _LoginPageState extends State<LoginPage> with SingleTickerProviderStateMix
|
||||
bool _obscureText = true;
|
||||
bool _rememberPassword = true;
|
||||
bool _credentialsLoaded = false;
|
||||
bool isPushInitialized = false;
|
||||
|
||||
@override
|
||||
void initState() {
|
||||
@@ -388,13 +392,28 @@ class _LoginPageState extends State<LoginPage> with SingleTickerProviderStateMix
|
||||
content: _buildDialogContent(),
|
||||
confirmText: '同意',
|
||||
cancelText: '拒绝',
|
||||
onConfirm: () {
|
||||
onConfirm: () async {
|
||||
_isAgreed = true;
|
||||
controller.updateUi();
|
||||
|
||||
// 保存隐私政策同意状态
|
||||
await StorageService.to.savePrivacyAgreed(true);
|
||||
|
||||
// 申请通知权限
|
||||
await _requestNotificationPermission();
|
||||
|
||||
// 初始化阿里云推送
|
||||
await _initPushService();
|
||||
},
|
||||
);
|
||||
return;
|
||||
}
|
||||
|
||||
// 如果已经同意过,但推送还没初始化,则初始化
|
||||
if (!isPushInitialized) {
|
||||
await _initPushService();
|
||||
}
|
||||
|
||||
_tabController.index == 0
|
||||
? _handleDriverLogin(controller)
|
||||
: _handleStationLogin(controller);
|
||||
@@ -536,6 +555,62 @@ class _LoginPageState extends State<LoginPage> with SingleTickerProviderStateMix
|
||||
addAlias(identifier);
|
||||
}
|
||||
|
||||
// 申请通知权限
|
||||
Future<void> _requestNotificationPermission() async {
|
||||
final PermissionStatus status = await Permission.notification.request();
|
||||
if (status.isGranted) {
|
||||
Logger.d('通知权限已授予');
|
||||
} else if (status.isPermanentlyDenied) {
|
||||
Logger.d('通知权限被永久拒绝');
|
||||
}
|
||||
}
|
||||
|
||||
// 初始化推送服务
|
||||
Future<void> _initPushService() async {
|
||||
try {
|
||||
final _aliyunPush = AliyunPushFlutter();
|
||||
|
||||
// 初始化推送
|
||||
final String appKey = Platform.isIOS ? AppTheme.ios_key : AppTheme.android_key;
|
||||
final String appSecret = Platform.isIOS
|
||||
? AppTheme.ios_appsecret
|
||||
: AppTheme.android_appsecret;
|
||||
|
||||
final result = await _aliyunPush.initPush(appKey: appKey, appSecret: appSecret);
|
||||
if (result['code'] != kAliyunPushSuccessCode) {
|
||||
Logger.d('推送初始化失败: ${result['errorMsg']}');
|
||||
return;
|
||||
}
|
||||
|
||||
// 配置平台特定设置
|
||||
if (Platform.isIOS) {
|
||||
await _aliyunPush.showIOSNoticeWhenForeground(true);
|
||||
} else if (Platform.isAndroid) {
|
||||
await _aliyunPush.setNotificationInGroup(true);
|
||||
await _aliyunPush.createAndroidChannel(
|
||||
"xll_push_android",
|
||||
'新消息通知',
|
||||
4,
|
||||
'用于接收加氢站实时状态提醒',
|
||||
);
|
||||
}
|
||||
|
||||
// 添加推送回调
|
||||
_aliyunPush.addMessageReceiver(
|
||||
onNotificationOpened: _onNotificationOpened,
|
||||
);
|
||||
|
||||
isPushInitialized = true;
|
||||
Logger.d('推送服务初始化成功');
|
||||
} catch (e) {
|
||||
Logger.d('推送服务初始化异常: $e');
|
||||
}
|
||||
}
|
||||
|
||||
Future<void> _onNotificationOpened(Map<dynamic, dynamic> message) async {
|
||||
await Get.to(() => const MessagePage());
|
||||
}
|
||||
|
||||
final _aliyunPush = AliyunPushFlutter();
|
||||
|
||||
void addAlias(String alias) async {
|
||||
|
||||
Reference in New Issue
Block a user