推送配置,测试

This commit is contained in:
2025-12-31 17:22:13 +08:00
parent 6629c8047f
commit 295b71c819
15 changed files with 557 additions and 76 deletions

View File

@@ -1,3 +1,6 @@
import 'dart:io';
import 'package:aliyun_push_flutter/aliyun_push_flutter.dart';
import 'package:flutter_native_splash/flutter_native_splash.dart';
import 'package:getx_scaffold/getx_scaffold.dart';
import 'package:ln_jq_app/pages/b_page/base_widgets/view.dart';
@@ -12,14 +15,115 @@ class HomeController extends GetxController with BaseControllerMixin {
HomeController();
final _aliyunPush = AliyunPushFlutter();
@override
void onInit() {
super.onInit();
initAliyunPush();
addPushCallback();
FlutterNativeSplash.remove();
}
void addPushCallback() {
_aliyunPush.addMessageReceiver(
onNotification: _onNotification,
onNotificationOpened: _onNotificationOpened,
onNotificationRemoved: _onNotificationRemoved,
onMessage: _onMessage,
onAndroidNotificationReceivedInApp: _onAndroidNotificationReceivedInApp,
onAndroidNotificationClickedWithNoAction: _onAndroidNotificationClickedWithNoAction,
onIOSChannelOpened: _onIOSChannelOpened,
onIOSRegisterDeviceTokenSuccess: _onIOSRegisterDeviceTokenSuccess,
onIOSRegisterDeviceTokenFailed: _onIOSRegisterDeviceTokenFailed,
);
}
Future<void> _onAndroidNotificationClickedWithNoAction(
Map<dynamic, dynamic> message,
) async {
Logger.d('onAndroidNotificationClickedWithNoAction ====> $message');
}
Future<void> _onAndroidNotificationReceivedInApp(Map<dynamic, dynamic> message) async {
Logger.d('onAndroidNotificationReceivedInApp ====> $message');
}
Future<void> _onMessage(Map<dynamic, dynamic> message) async {
Logger.d('onMessage ====> $message');
}
Future<void> _onNotification(Map<dynamic, dynamic> message) async {
Logger.d('onNotification ====> $message');
}
Future<void> _onNotificationOpened(Map<dynamic, dynamic> message) async {
Logger.d('onNotificationOpened ====> $message');
}
Future<void> _onNotificationRemoved(Map<dynamic, dynamic> message) async {
Logger.d('onNotificationRemoved ====> $message');
}
Future<void> _onIOSChannelOpened(Map<dynamic, dynamic> message) async {
Logger.d('onIOSChannelOpened ====> $message');
}
Future<void> _onIOSRegisterDeviceTokenSuccess(Map<dynamic, dynamic> message) async {
Logger.d('onIOSRegisterDeviceTokenSuccess ====> $message');
}
Future<void> _onIOSRegisterDeviceTokenFailed(Map<dynamic, dynamic> message) async {
Logger.d('onIOSRegisterDeviceTokenFailed====> $message');
}
Future<void> initAliyunPush() async {
String appKey;
String appSecret;
if (Platform.isIOS) {
appKey = '335642649';
appSecret = '173bc08bd5df422da20c8e3ffbf0521b';
} else {
appKey = "335642645";
appSecret = "39628204345a4240b5b645b68a5896c7";
}
var result = await _aliyunPush.initPush(appKey: appKey, appSecret: appSecret);
var code = result['code'];
if (code == kAliyunPushSuccessCode) {
Logger.d('初始化推送成功');
if(Platform.isIOS){
var result = await _aliyunPush.showIOSNoticeWhenForeground(true);
var code = result['code'];
if (code == kAliyunPushSuccessCode) {
Logger.d('设置前台显示通知成功');
} else {
Logger.d('设置前台显示通知失败');
}
}else if(Platform.isAndroid){
await _aliyunPush.setNotificationInGroup(true);
var result = await _aliyunPush.createAndroidChannel(
"xll_push_android", '新消息通知', 2, '收到新消息的通知类别');
var code = result['code'];
if (code == kAliyunPushSuccessCode) {
Logger.d('创建通道成功');
} else {
var errorCode = result['code'];
var errorMsg = result['errorMsg'];
Logger.d('创建通道失败, $errorCode - $errorMsg');
}
}
} else {
var errorMsg = result['errorMsg'];
Logger.d('初始化推送失败: $code - $errorMsg');
}
}
// 根据登录状态和登录渠道返回不同的首页
Widget getHomePage() {
requestPermission();
//登录状态跳转
if (StorageService.to.isLoggedIn) {
// 如果已登录,再判断是哪个渠道
@@ -35,4 +139,25 @@ class HomeController extends GetxController with BaseControllerMixin {
return LoginPage();
}
}
void requestPermission() async {
PermissionStatus status = await Permission.notification.status;
if (status.isGranted) {
Logger.d("通知权限已开启");
return;
}
if (status.isDenied) {
// 建议此处增加一个应用内的 Rationale (解释说明) 弹窗
status = await Permission.notification.request();
}
if (status.isGranted) {
// 授权成功
Logger.d('通知已开启');
} else if (status.isPermanentlyDenied) {
Logger.d('通知权限已被拒绝,请到系统设置中开启');
} else if (status.isDenied) {
Logger.d('请授予通知权限,以便接收加氢站通知');
}
}
}

View File

@@ -1,3 +1,4 @@
import 'package:aliyun_push_flutter/aliyun_push_flutter.dart';
import 'package:flutter/material.dart';
import 'package:getx_scaffold/getx_scaffold.dart';
import 'package:ln_jq_app/common/login_util.dart';
@@ -21,8 +22,10 @@ class _LoginPageState extends State<LoginPage> with SingleTickerProviderStateMix
bool cLogin = true;
bool _obscureText = true;
// 用于管理“记住密码”的复选框状态
bool _rememberPassword = true;
// 用于确保凭证只在首次加载时回填一次
bool _credentialsLoaded = false;
@@ -198,20 +201,24 @@ class _LoginPageState extends State<LoginPage> with SingleTickerProviderStateMix
phone: phone,
);
//注册推送别名
addAlias(phone);
//登录后查询已绑定车辆信息
var carInfo = await HttpService.to.get(
"appointment/driver/getTruckInfoByDriver?phone=$phone"
"appointment/driver/getTruckInfoByDriver?phone=$phone",
);
if (carInfo != null) {
var carInforesult = BaseModel.fromJson(carInfo.data);
if (carInforesult.data != null) {
final vehicle = VehicleInfo.fromJson(carInforesult.data as Map<String, dynamic>);
final vehicle = VehicleInfo.fromJson(
carInforesult.data as Map<String, dynamic>,
);
//保存使用
await StorageService.to.saveVehicleInfo(vehicle);
}
}
//页面操作
dismissLoading();
showToast('登录成功,欢迎您');
@@ -269,9 +276,7 @@ class _LoginPageState extends State<LoginPage> with SingleTickerProviderStateMix
hintText: '请输入密码',
border: OutlineInputBorder(),
suffixIcon: IconButton(
icon: Icon(
_obscureText ? Icons.visibility_off : Icons.visibility,
),
icon: Icon(_obscureText ? Icons.visibility_off : Icons.visibility),
onPressed: () {
setState(() {
_obscureText = !_obscureText;
@@ -304,7 +309,7 @@ class _LoginPageState extends State<LoginPage> with SingleTickerProviderStateMix
GestureDetector(
onTap: () => setState(() => _rememberPassword = !_rememberPassword),
child: const Text('记住密码', style: TextStyle(color: Colors.grey)),
)
),
],
),
SizedBox(height: 20), // 调整间距
@@ -317,7 +322,7 @@ class _LoginPageState extends State<LoginPage> with SingleTickerProviderStateMix
onPressed: () async {
String account = controller.stationIdController.text;
String password = controller.passwordController.text;
if (account.isEmpty || password.isEmpty) {
showToast("请输入账号和密码");
return;
@@ -353,13 +358,18 @@ class _LoginPageState extends State<LoginPage> with SingleTickerProviderStateMix
String token = result.data['token'] ?? '';
String userId = result.data['userId'] ?? '';
String mobile = result.data['mobile'] ?? '';
await StorageService.to.saveLoginInfo(
token: token,
userId: userId,
phone: mobile,
channel: "station",
);
//注册推送别名
addAlias(mobile);
// 根据复选框状态保存或清除密码 ---
if (_rememberPassword) {
await StorageService.to.saveStationCredentials(account, password);
@@ -384,6 +394,20 @@ class _LoginPageState extends State<LoginPage> with SingleTickerProviderStateMix
);
}
final _aliyunPush = AliyunPushFlutter();
void addAlias(String alias) async {
var result = await _aliyunPush.addAlias(alias);
var code = result['code'];
if (code == kAliyunPushSuccessCode) {
Logger.d('添加别名$alias成功');
} else {
var errorCode = result['code'];
var errorMsg = result['errorMsg'];
Logger.d('添加别名$alias失败: $errorCode - $errorMsg');
}
}
@override
Widget build(BuildContext context) {
return GetBuilder<LoginController>(