/* * @Author: XianKaiQun * @LastEditors: wuwei * @Date: 2020-10-22 09:54:21 * @LastEditTime: 2025-03-13 14:54:47 */ import 'dart:io'; import 'package:flutter/material.dart'; // import 'package:jpush_flutter/jpush_flutter.dart'; // import 'package:permission_handler/permission_handler.dart'; import 'package:wisdom_cli/wisdom_cli.dart'; // export 'package:jpush_flutter/jpush_flutter.dart' show LocalNotification; typedef Future EventHandler(Map event); ///极光推送 /// ///main函数中调用[WJpushUtil.setup],先注册极光且设置回调函数。 /// ///然后再调用其他方法 /// ///eg: ///在登录前后,登出前后调用相应方法操作[tags] /// ///登出后[stopPush]停止推送 /// ///登录后[resumePush]恢复推送 /// class WJpushUtil { WJpushUtil._(); static WJpushUtil? _instance; static WJpushUtil? get instance { _instance ??= WJpushUtil._(); return _instance; } bool _inited = false; late final _jpush; ///注册 ///[appKey]IOS appKey /// ///[channel]channel /// ///[production]production /// ///[debug] 是否打印 /// ///[sound] iOS10+ only 是否触发声音 /// ///[alert] iOS10+ only 是否前台展示 /// ///[badge] iOS10+ only 是否设置应用角标 badge /// ///[notificationSettingsIOS]Ios配置 /// ///[onReceiveNotification]接收通知回调方法。 /// ///[onOpenNotification]点击通知回调方法。 /// ///[onReceiveMessage]接收自定义消息回调方法。 /// void setup({ required String appKey, String? channel, bool production: false, bool debug: true, bool sound = true, bool alert = true, bool badge = true, EventHandler? onReceiveNotification, EventHandler? onOpenNotification, EventHandler? onReceiveMessage, }) { // print('---------------------------------------------------------------------push in jpush class------------------------------'); // if (_inited) { // debugPrint('重复注册JpushUtil,此次注册无效,请忽略'); // return; // } // bool _isReadPrivacy = WisStorageUtil.get(key: 'isReadPrivacy') ?? false; // print(_isReadPrivacy); // print('---------------------------------------------------------------------isReadPrivacy--------------------------------'); // // 检查用户是否同意隐私政策 // if (!_isReadPrivacy) { // _jpush.setAuth(enable: false); // debugPrint('---------------------------------------------------------------------用户未同意隐私政策,极光推送初始化已跳过--------------------------------------------------------------------'); // return; // } // _jpush = JPush(); // _inited = true; // _jpush.addEventHandler( // onReceiveNotification: onReceiveNotification, // onOpenNotification: onOpenNotification, // onReceiveMessage: onReceiveMessage, // ); // _jpush.setup( // appKey: appKey, // channel: channel ?? 'theChannel', // production: production, // debug: false, // ); // // 允许极光收集 // _jpush.setAuth(enable: true); // ///申请推送权限,注意这个方法只会向用户弹出一次推送权限请求, // ///该方法还配置IOS角标、是否有声音的工作等 // ///如果用户不同意,之后只能用户到设置页面里面勾选相应权限, // ///配置IOS角标、是否有声音的工作 // if (Platform.isIOS) { // _jpush.applyPushAuthority( // NotificationSettingsIOS(sound: true, alert: true, badge: true), // ); // } ///Android权限申请 // if (Platform.isAndroid){ // WPermissionUtil.request( // service: Permission.notification, // deniedText: '无法获取您的通知权限,是否前往设置?', // guidance: true, // ); // } } _check() { assert(_inited, '请先在main函数中调用WJpushUtil.setup初始化极光推送'); } ///获取 registrationId,这个 JPush 运行通过 registrationId 来进行推送. Future get getRegistrationID { _check(); return _jpush.getRegistrationID(); } ///停止推送功能,调用该方法将不会接收到通知。 Future stopPush() { _check(); return _jpush.stopPush(); } ///调用 stopPush 后,可以通过 resumePush 方法恢复推送。 Future resumePush() { _check(); return _jpush.resumePush(); } ///设置别名,极光后台可以通过别名来推送,一个 App 应用只有一个别名,一般用来存储用户 id。 Future> setAlias(String alias) { _check(); return _jpush.setAlias(alias); } ///可以通过 deleteAlias 方法来删除已经设置的 alias。 Future> deleteAlias() { _check(); return _jpush.deleteAlias(); } ///在原来的 Tags 列表上添加指定 tags。 Future> addTags(List tags) { _check(); return _jpush.addTags(tags); } ///在原来的 Tags 列表上删除指定 tags。 Future> deleteTags(List tags) { _check(); return _jpush.deleteTags(tags); } ///重置 tags。 Future> setTags(List tags) { _check(); return _jpush.setTags(tags); } ///清空所有 tags Future> cleanTags() { _check(); return _jpush.cleanTags(); } ///获取当前 tags 列表。 Future> get getAllTags { _check(); return _jpush.getAllTags(); } ///指定触发时间,添加本地推送通知。 // Future sendLocalNotification(LocalNotification notification) { // _check(); // return _jpush.sendLocalNotification(notification); // } ///清除通知栏上所有通知。 Future clearAllNotifications() { _check(); return _jpush.clearAllNotifications(); } ///清空通知栏上某个通知 void clearNotification(int notificationId) { _check(); return _jpush.clearNotification(notificationId: notificationId); } ///设置应用 Badge(小红点) ///IOS/华为Android Future setBadge(int badge) { _check(); return _jpush.setBadge(badge); } /// iOS Only /// 点击推送启动应用的时候原生会将该 notification 缓存起来,该方法用于获取缓存 notification /// 注意:notification 可能是 remoteNotification 和 localNotification,两种推送字段不一样。 /// 如果不是通过点击推送启动应用,比如点击应用 icon 直接启动应用,notification 会返回 @{}。 /// @param {Function} callback = (Object) => {} /// Future> getLaunchAppNotification() async { _check(); return _jpush.getLaunchAppNotification(); } }