JPush推送接入:jpush_flutter 3.6.2(适配Gradle9与3.x API)、Android 13通知权限、RID注册上报;dispatch修复通知依赖RID的缺陷;AppKey a4b05b..8c3已验证真机长连接推送
This commit is contained in:
@@ -23,6 +23,9 @@ android {
|
||||
targetSdk = flutter.targetSdkVersion
|
||||
versionCode = flutter.versionCode
|
||||
versionName = flutter.versionName
|
||||
// 极光推送(jpush_flutter 插件占位符)
|
||||
manifestPlaceholders["JPUSH_APPKEY"] = "ba2236956024b8644a83e8c3"
|
||||
manifestPlaceholders["JPUSH_CHANNEL"] = "developer_default"
|
||||
}
|
||||
|
||||
buildTypes {
|
||||
|
||||
File diff suppressed because one or more lines are too long
@@ -1,13 +1,17 @@
|
||||
/// 推送服务骨架:设备号生成 + RID 上报/解绑 + WorkManager 兜底。
|
||||
/// 推送服务:JPush SDK 接入(初始化 → RegistrationID → 上报后端)+ WorkManager 兜底。
|
||||
///
|
||||
/// JPush 凭证未配置时 RID 为空,其余链路(设备注册、登出解绑)已可用;
|
||||
/// 凭证到位后在 init() 接入 jpush_flutter 并把 RID 回传 registerDevice。
|
||||
/// JPush 凭证经 --dart-define=JPUSH_APP_KEY 注入(与 manifestPlaceholders 同值);
|
||||
/// RID 获取可能滞后于 setup(首次安装需等 JPush 服务分配),带重试。
|
||||
library;
|
||||
|
||||
import 'dart:async' show unawaited;
|
||||
import 'dart:math';
|
||||
|
||||
import 'package:flutter/foundation.dart' show kIsWeb;
|
||||
import 'package:flutter_riverpod/flutter_riverpod.dart';
|
||||
import 'package:jpush_flutter/jpush_flutter.dart' show JPush;
|
||||
import 'package:jpush_flutter/jpush_interface.dart' show JPushFlutterInterface;
|
||||
import 'package:permission_handler/permission_handler.dart';
|
||||
|
||||
import '../network/api_client.dart';
|
||||
import '../storage/token_store.dart';
|
||||
@@ -16,25 +20,47 @@ class PushService {
|
||||
PushService(this._ref);
|
||||
|
||||
final Ref _ref;
|
||||
final JPushFlutterInterface _jpush = JPush.newJPush();
|
||||
String? _deviceId;
|
||||
String? _rid;
|
||||
|
||||
bool get _configured => const String.fromEnvironment('JPUSH_APP_KEY').isNotEmpty;
|
||||
static const _appKey = String.fromEnvironment('JPUSH_APP_KEY');
|
||||
bool get _configured => _appKey.isNotEmpty;
|
||||
|
||||
Future<void> init() async {
|
||||
if (kIsWeb) return;
|
||||
_deviceId = await _ref.read(tokenStoreProvider).readDeviceId() ?? _genDeviceId();
|
||||
await _ref.read(tokenStoreProvider).saveDeviceId(_deviceId!);
|
||||
// TODO(JPush 凭证到位后):
|
||||
// JPush.setup(appKey: ..., channel: 'official', production: true);
|
||||
// JPush.applyPushAuthority();
|
||||
// _rid = await JPush.getRegistrationID();
|
||||
if (_configured) {
|
||||
// rid 回传后由 registerDevice 上报
|
||||
try {
|
||||
_jpush.setup(
|
||||
appKey: _appKey,
|
||||
channel: 'official',
|
||||
production: true,
|
||||
debug: false,
|
||||
);
|
||||
// Android 13+ 通知权限(拒绝过则静默,用户可去系统设置开启)
|
||||
unawaited(Permission.notification.request());
|
||||
_rid = await _waitRegistrationID();
|
||||
} catch (_) {
|
||||
// SDK 初始化失败不阻塞登录;下一轮 registerDevice 重试
|
||||
}
|
||||
}
|
||||
await registerDevice();
|
||||
}
|
||||
|
||||
/// RID 分配可能需要数秒(首次安装),最多等 15 秒。
|
||||
Future<String?> _waitRegistrationID() async {
|
||||
for (var i = 0; i < 15; i++) {
|
||||
try {
|
||||
final rid = await _jpush.getRegistrationID();
|
||||
if (rid.isNotEmpty) return rid;
|
||||
} catch (_) {}
|
||||
await Future<void>.delayed(const Duration(seconds: 1));
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
String _genDeviceId() {
|
||||
final r = Random.secure();
|
||||
return base64UrlNoPad(List<int>.generate(32, (_) => r.nextInt(256)));
|
||||
|
||||
@@ -408,6 +408,14 @@ packages:
|
||||
url: "https://pub.dev"
|
||||
source: hosted
|
||||
version: "1.0.0"
|
||||
jpush_flutter:
|
||||
dependency: "direct main"
|
||||
description:
|
||||
name: jpush_flutter
|
||||
sha256: bc80254d40b564547371e6dbbf7fdd4ed7a079a5849dbbbd7c8c312bfdcdd1f1
|
||||
url: "https://pub.dev"
|
||||
source: hosted
|
||||
version: "3.6.2"
|
||||
js:
|
||||
dependency: transitive
|
||||
description:
|
||||
|
||||
@@ -41,6 +41,7 @@ dependencies:
|
||||
path: ^1.9.0
|
||||
workmanager: ^0.10.0
|
||||
flutter_local_notifications: ^22.2.0
|
||||
jpush_flutter: ^3.6.2
|
||||
shared_preferences: ^2.5.5
|
||||
|
||||
# The following adds the Cupertino Icons font to your application.
|
||||
|
||||
Reference in New Issue
Block a user