推送配置,测试

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('请授予通知权限,以便接收加氢站通知');
}
}
}