这是indexloc提供的服务,不要输入任何密码
Skip to content

feat: use UnifiedPush connector 3 #585

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Mar 29, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions android/app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ android {

defaultConfig {
applicationId "com.poppingmoon.aria"
minSdkVersion 21
minSdkVersion 23
multiDexEnabled true
targetSdkVersion flutter.targetSdkVersion
versionCode flutterVersionCode.toInteger()
Expand Down Expand Up @@ -72,7 +72,7 @@ flutter {

dependencies {
coreLibraryDesugaring 'com.android.tools:desugar_jdk_libs:2.1.5'
implementation 'org.unifiedpush.android:foss-embedded-fcm-distributor:1.0.0'
implementation 'org.unifiedpush.android:embedded-fcm-distributor:3.0.0'
}

ext.abiCodes = ["x86_64": 1, "armeabi-v7a": 2, "arm64-v8a": 3]
Expand Down
10 changes: 0 additions & 10 deletions android/app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -65,16 +65,6 @@
<action android:name="android.intent.action.MEDIA_BUTTON" />
</intent-filter>
</receiver>
<receiver
android:enabled="true"
android:name=".EmbeddedDistributor"
android:exported="false">
<intent-filter>
<action android:name="org.unifiedpush.android.distributor.feature.BYTES_MESSAGE"/>
<action android:name="org.unifiedpush.android.distributor.REGISTER"/>
<action android:name="org.unifiedpush.android.distributor.UNREGISTER"/>
</intent-filter>
</receiver>
<!-- Don't delete the meta-data below.
This is used by the Flutter tool to generate GeneratedPluginRegistrant.java -->
<meta-data
Expand Down

This file was deleted.

3 changes: 0 additions & 3 deletions lib/constant/fcm_token_prefix.dart

This file was deleted.

53 changes: 34 additions & 19 deletions lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ class Aria extends HookConsumerWidget {
.read(unifiedPushEndpointNotifierProvider(instance).notifier)
.updateEndpoint(endpoint),
onRegistrationFailed:
(instance) =>
(reason, instance) =>
ref
.read(
unifiedPushEndpointNotifierProvider(instance).notifier,
Expand All @@ -171,28 +171,43 @@ class Aria extends HookConsumerWidget {
.remove(),
onMessage: (message, instance) async {
Account account = Account.fromString(instance);
final keySet = await ref.read(
webPushKeySetNotifierNotifierProvider(account).future,
);

final PushNotification notification;
if (keySet != null) {
final decrypted = await WebPush().decrypt(keySet, message);
if (message.decrypted) {
final webPushMessage =
jsonDecode(utf8.decode(decrypted)) as Map<String, dynamic>;
jsonDecode(utf8.decode(message.content))
as Map<String, dynamic>;
notification = PushNotification.fromJson(webPushMessage);
} else {
final fcmMessage =
jsonDecode(utf8.decode(message)) as Map<String, dynamic>;
fcmMessage['body'] = jsonDecode(fcmMessage['body'] as String);
notification = PushNotification.fromJson(fcmMessage);
account =
ref
.read(userIdsNotifierProvider)
.entries
.firstWhereOrNull((e) => e.value == notification.userId)
?.key ??
account;
final keySet = await ref.read(
webPushKeySetNotifierNotifierProvider(account).future,
);
if (keySet != null) {
final decrypted = await WebPush().decrypt(
keySet,
message.content,
);
final webPushMessage =
jsonDecode(utf8.decode(decrypted)) as Map<String, dynamic>;
notification = PushNotification.fromJson(webPushMessage);
} else {
final fcmMessage =
jsonDecode(utf8.decode(message.content))
as Map<String, dynamic>;
if (fcmMessage['body'] case final String body) {
fcmMessage['body'] = jsonDecode(body);
}
if (fcmMessage['dateTime'] case final String dateTime) {
fcmMessage['dateTime'] = int.tryParse(dateTime);
}
notification = PushNotification.fromJson(fcmMessage);
account =
ref
.read(userIdsNotifierProvider)
.entries
.firstWhereOrNull((e) => e.value == notification.userId)
?.key ??
account;
}
}

final currentLocale = LocaleSettings.currentLocale;
Expand Down
52 changes: 30 additions & 22 deletions lib/provider/push_subscription_notifier_provider.dart
Original file line number Diff line number Diff line change
Expand Up @@ -22,36 +22,42 @@ class PushSubscriptionNotifier extends _$PushSubscriptionNotifier {

String get _key => '$account/push-subscription';

String get _isProxyKey => '$account/push-subscription-is-proxy';

Future<void> subscribe({
required String id,
String? fcmToken,
String? apnsToken,
required WebPushKeySet keySet,
WebPushKeySet? keySet,
required SwRegisterResponse response,
}) async {
final endpoint = response.endpoint;
if (endpoint.startsWith(misskeyWebPushProxyUrl)) {
final jwk = await (await keySet.privateKey.privKey).exportJsonWebKey();
await ref
.read(dioProvider)
.post<Map<String, dynamic>>(
'$misskeyWebPushProxyUrl/subscriptions',
data: {
'id': id,
if (fcmToken != null) 'fcmToken': fcmToken,
if (apnsToken != null) 'apnsToken': apnsToken,
'auth': keySet.publicKey.auth,
'publicKey': keySet.publicKey.p256dh,
'privateKey': jwk['d'],
'vapidKey': response.key,
},
);
} else {
await ref
.read(webPushKeySetNotifierNotifierProvider(account).notifier)
.save(keySet);
final isProxy = endpoint.startsWith(misskeyWebPushProxyUrl);
if (keySet != null) {
if (isProxy) {
final jwk = await (await keySet.privateKey.privKey).exportJsonWebKey();
await ref
.read(dioProvider)
.post<Map<String, dynamic>>(
'$misskeyWebPushProxyUrl/subscriptions',
data: {
'id': id,
if (fcmToken != null) 'fcmToken': fcmToken,
if (apnsToken != null) 'apnsToken': apnsToken,
'auth': keySet.publicKey.auth,
'publicKey': keySet.publicKey.p256dh,
'privateKey': jwk['d'],
'vapidKey': response.key,
},
);
} else {
await ref
.read(webPushKeySetNotifierNotifierProvider(account).notifier)
.save(keySet);
}
}
await ref.read(sharedPreferencesProvider).setString(_key, endpoint);
await ref.read(sharedPreferencesProvider).setBool(_isProxyKey, isProxy);
state = endpoint;
}

Expand All @@ -61,10 +67,11 @@ class PushSubscriptionNotifier extends _$PushSubscriptionNotifier {
if (defaultTargetPlatform == TargetPlatform.android) {
await UnifiedPush.unregister(account.toString());
}
final isProxy = ref.read(sharedPreferencesProvider).getBool(_isProxyKey);
final keySet = await ref.read(
webPushKeySetNotifierNotifierProvider(account).future,
);
if (keySet == null) {
if (isProxy ?? keySet == null) {
await ref.read(dioProvider).delete<void>(endpoint);
}
await ref
Expand All @@ -77,6 +84,7 @@ class PushSubscriptionNotifier extends _$PushSubscriptionNotifier {
.delete();
}
await ref.read(sharedPreferencesProvider).remove(_key);
await ref.read(sharedPreferencesProvider).remove(_isProxyKey);
state = null;
}
}
2 changes: 1 addition & 1 deletion lib/provider/push_subscription_notifier_provider.g.dart

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 3 additions & 2 deletions lib/provider/unified_push_endpoint_notifier_provider.dart
Original file line number Diff line number Diff line change
@@ -1,17 +1,18 @@
import 'package:riverpod_annotation/riverpod_annotation.dart';
import 'package:unifiedpush/unifiedpush.dart';

part 'unified_push_endpoint_notifier_provider.g.dart';

@Riverpod(keepAlive: true)
class UnifiedPushEndpointNotifier extends _$UnifiedPushEndpointNotifier {
@override
String? build(String instance) {
PushEndpoint? build(String instance) {
return null;
}

// https://pub.dev/packages/riverpod_lint#avoid_public_notifier_properties
// ignore: use_setters_to_change_properties
void updateEndpoint(String endpoint) {
void updateEndpoint(PushEndpoint endpoint) {
state = endpoint;
}

Expand Down
20 changes: 11 additions & 9 deletions lib/provider/unified_push_endpoint_notifier_provider.g.dart

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 7 additions & 0 deletions lib/router/router.dart
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ import '../view/page/settings/tab_settings_page.dart';
import '../view/page/settings/tabs_page.dart';
import '../view/page/settings/theme_manage_page.dart';
import '../view/page/settings/theme_page.dart';
import '../view/page/settings/timeline_buttons_page.dart';
import '../view/page/share_page.dart';
import '../view/page/splash_page.dart';
import '../view/page/tag/tag_page.dart';
Expand Down Expand Up @@ -260,6 +261,12 @@ GoRouter router(Ref ref) {
GoRoute(
path: 'appearance',
builder: (_, _) => const AppearancePage(),
routes: [
GoRoute(
path: 'buttons',
builder: (_, _) => const TimelineButtonsPage(),
),
],
),
GoRoute(path: 'behavior', builder: (_, _) => const BehaviorPage()),
GoRoute(
Expand Down
2 changes: 1 addition & 1 deletion lib/router/router.g.dart

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading
Loading