Browse Source

feat(flutter): implement notification service

pull/851/head
colin 2 years ago
parent
commit
9123fb2e4f
  1. 3
      apps/flutter/core/lib/core.module.dart
  2. 13
      apps/flutter/core/lib/models/notifications.dart
  3. 1
      apps/flutter/core/lib/services/index.dart
  4. 5
      apps/flutter/core/lib/services/notification.send.service.dart
  5. 1
      apps/flutter/dev_app/android/app/build.gradle
  6. BIN
      apps/flutter/dev_app/android/app/src/main/res/mipmap-hdpi/logo.png
  7. BIN
      apps/flutter/dev_app/android/app/src/main/res/mipmap-mdpi/logo.png
  8. BIN
      apps/flutter/dev_app/android/app/src/main/res/mipmap-xhdpi/logo.png
  9. BIN
      apps/flutter/dev_app/android/app/src/main/res/mipmap-xxhdpi/logo.png
  10. BIN
      apps/flutter/dev_app/android/app/src/main/res/mipmap-xxxhdpi/logo.png
  11. 3
      apps/flutter/dev_app/lib/interceptors/api.append.header.interceptor.dart
  12. 15
      apps/flutter/dev_app/lib/main.module.dart
  13. 6
      apps/flutter/dev_app/lib/pages/public/center/view.dart
  14. 1
      apps/flutter/dev_app/lib/services/index.dart
  15. 62
      apps/flutter/dev_app/lib/services/notification.send.local.service.dart
  16. 64
      apps/flutter/dev_app/pubspec.lock
  17. 1
      apps/flutter/dev_app/pubspec.yaml
  18. BIN
      apps/flutter/dev_app/res/images/logo.png

3
apps/flutter/core/lib/core.module.dart

@ -1,14 +1,13 @@
import 'package:core/dependency/injector.dart';
import 'package:core/modularity/module.dart';
import 'package:core/services/index.dart';
import 'package:get/get.dart';
import 'proxy/index.dart';
class CoreModule extends Module {
@override
Future<void> configureServicesAsync() async {
await Get.putAsync<StorageService>(() async {
await injectAsync<StorageService>(() async {
if (await GetxStorageService.init()) {
return GetxStorageService();
}

13
apps/flutter/core/lib/models/notifications.dart

@ -0,0 +1,13 @@
class Notification {
Notification({
required this.id,
required this.title,
required this.body,
required this.payload,
});
final int id;
final String? title;
final String? body;
final String? payload;
}

1
apps/flutter/core/lib/services/index.dart

@ -1,6 +1,7 @@
export 'auth.service.dart';
export 'config.state.service.dart';
export 'localization.service.dart';
export 'notification.send.service.dart';
export 'rest.service.dart';
export 'storage.service.dart';
export 'session.service.dart';

5
apps/flutter/core/lib/services/notification.send.service.dart

@ -0,0 +1,5 @@
import 'package:core/services/service.base.dart';
abstract class NotificationSendService extends ServiceBase {
Future<void> send(String title, [String? body, String? payload]);
}

1
apps/flutter/dev_app/android/app/build.gradle

@ -69,4 +69,5 @@ flutter {
dependencies {
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
coreLibraryDesugaring 'com.android.tools:desugar_jdk_libs:1.1.5'
}

BIN
apps/flutter/dev_app/android/app/src/main/res/mipmap-hdpi/logo.png

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.4 KiB

BIN
apps/flutter/dev_app/android/app/src/main/res/mipmap-mdpi/logo.png

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.4 KiB

BIN
apps/flutter/dev_app/android/app/src/main/res/mipmap-xhdpi/logo.png

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.4 KiB

BIN
apps/flutter/dev_app/android/app/src/main/res/mipmap-xxhdpi/logo.png

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.4 KiB

BIN
apps/flutter/dev_app/android/app/src/main/res/mipmap-xxxhdpi/logo.png

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.4 KiB

3
apps/flutter/dev_app/lib/interceptors/api.append.header.interceptor.dart

@ -1,3 +1,4 @@
import 'package:core/config/index.dart';
import 'package:core/services/session.service.dart';
import 'package:core/utils/string.extensions.dart';
import 'package:dio/dio.dart';
@ -11,7 +12,7 @@ class AppendHeaderInterceptor extends Interceptor {
}
var tenant = SessionService.to.tenant;
if (tenant != null && tenant.isAvailable == true) {
options.headers['__tenant'] = tenant.id;
options.headers[Environment.current.tenantKey ?? '__tenant'] = tenant.id;
}
return handler.next(options);
}

15
apps/flutter/dev_app/lib/main.module.dart

@ -1,4 +1,5 @@
import 'package:dev_app/pages/index.dart';
import 'package:dev_app/services/index.dart';
import 'package:dev_app/utils/loading.dart';
import 'package:account/index.dart';
import 'package:components/index.dart';
@ -38,6 +39,18 @@ class MainModule extends Module {
...SystemRoute.routes,
];
@override
Future<void> configureServicesAsync() async {
await injectAsync<NotificationSendService>(() async {
var service = FlutterLocalNotificationsSendService();
await service.initAsync();
return service;
}, permanent: true);
await super.configureServicesAsync();
}
@override
void configureServices() {
inject<MainModule>(this);
@ -46,7 +59,7 @@ class MainModule extends Module {
tag: NotificationTokens.producer,
permanent: true);
lazyInject(() {
lazyInject<RestService>(() {
var dio = Dio(BaseOptions(
baseUrl: Environment.current.baseUrl,
));

6
apps/flutter/dev_app/lib/pages/public/center/view.dart

@ -1,3 +1,4 @@
import 'package:core/services/notification.send.service.dart';
import 'package:dev_app/pages/center/route.name.dart';
import 'package:account/pages/route.name.dart';
import 'package:flutter/material.dart';
@ -47,7 +48,10 @@ class CenterPage extends GetView<CenterController> {
//controller.redirectToRoute('/center/feedback');
},
onSettings: () => controller.redirectToRoute(CenterRoutes.settings),
onInfo: () => controller.redirectToRoute(CenterRoutes.info),
onInfo: () async {
var service = Get.find<NotificationSendService>();
await service.send('测试通知', '测试内容', '测试载体');
},
)),
],
);

1
apps/flutter/dev_app/lib/services/index.dart

@ -0,0 +1 @@
export 'notification.send.local.service.dart';

62
apps/flutter/dev_app/lib/services/notification.send.local.service.dart

@ -0,0 +1,62 @@
import 'dart:async';
import 'package:get/get.dart';
import 'package:rxdart/rxdart.dart' hide Notification;
import 'package:core/models/notifications.dart';
import 'package:core/services/notification.send.service.dart';
import 'package:flutter_local_notifications/flutter_local_notifications.dart';
class FlutterLocalNotificationsSendService extends NotificationSendService {
final RxInt nid = 0.obs;
final FlutterLocalNotificationsPlugin _flutterLocalNotificationsPlugin = FlutterLocalNotificationsPlugin();
final Subject<Notification> _notifications$ = BehaviorSubject<Notification>();
final Subject<String?> _selectedNotifications$ = BehaviorSubject<String?>();
Future<void> initAsync() async {
const initializationSettingsAndroid = AndroidInitializationSettings('@mipmap/logo');
const initializationSettingsLinux = LinuxInitializationSettings(
defaultActionName: 'Open notification',
);
var initializationSettingsDarwin = DarwinInitializationSettings(
onDidReceiveLocalNotification: (id, title, body, payload) {
_notifications$.add(Notification(id: id, title: title, body: body, payload: payload));
},
);
var initializationSettings = InitializationSettings(
android: initializationSettingsAndroid,
iOS: initializationSettingsDarwin,
macOS: initializationSettingsDarwin,
linux: initializationSettingsLinux,
);
await _flutterLocalNotificationsPlugin.initialize(
initializationSettings,
onDidReceiveNotificationResponse: (notificationResponse) {
switch (notificationResponse.notificationResponseType) {
case NotificationResponseType.selectedNotification:
_selectedNotifications$.add(notificationResponse.payload);
break;
case NotificationResponseType.selectedNotificationAction:
_selectedNotifications$.add(notificationResponse.payload);
break;
}
},
);
}
@override
Future<void> send(String title, [String? body, String? payload]) async {
nid.value += 1;
const androidDetails = AndroidNotificationDetails(
'abp-flutter',
'abp-flutter');
const details = NotificationDetails(
android: androidDetails,
);
await _flutterLocalNotificationsPlugin.show(
nid.value,
title,
body,
details,
payload: payload);
}
}

64
apps/flutter/dev_app/pubspec.lock

@ -8,6 +8,14 @@ packages:
relative: true
source: path
version: "0.0.1"
args:
dependency: transitive
description:
name: args
sha256: eef6c46b622e0494a36c5a12d10d77fb4e855501a91c1b9ef9339326e58f0596
url: "https://pub.flutter-io.cn"
source: hosted
version: "2.4.2"
async:
dependency: transitive
description:
@ -94,6 +102,14 @@ packages:
url: "https://pub.flutter-io.cn"
source: hosted
version: "1.0.0"
dbus:
dependency: transitive
description:
name: dbus
sha256: "6f07cba3f7b3448d42d015bfd3d53fe12e5b36da2423f23838efc1d5fb31a263"
url: "https://pub.flutter-io.cn"
source: hosted
version: "0.7.8"
dio:
dependency: "direct main"
description:
@ -171,6 +187,30 @@ packages:
url: "https://pub.flutter-io.cn"
source: hosted
version: "2.0.2"
flutter_local_notifications:
dependency: "direct main"
description:
name: flutter_local_notifications
sha256: aea96b3b78556c97607d9bee59fcec515bc3ec1b6e905f14d72906ae055033fa
url: "https://pub.flutter-io.cn"
source: hosted
version: "14.1.2"
flutter_local_notifications_linux:
dependency: transitive
description:
name: flutter_local_notifications_linux
sha256: "33f741ef47b5f63cc7f78fe75eeeac7e19f171ff3c3df054d84c1e38bedb6a03"
url: "https://pub.flutter-io.cn"
source: hosted
version: "4.0.0+1"
flutter_local_notifications_platform_interface:
dependency: transitive
description:
name: flutter_local_notifications_platform_interface
sha256: "7cf643d6d5022f3baed0be777b0662cce5919c0a7b86e700299f22dc4ae660ef"
url: "https://pub.flutter-io.cn"
source: hosted
version: "7.0.0+1"
flutter_localizations:
dependency: "direct main"
description: flutter
@ -372,6 +412,14 @@ packages:
url: "https://pub.flutter-io.cn"
source: hosted
version: "2.1.7"
petitparser:
dependency: transitive
description:
name: petitparser
sha256: cb3798bef7fc021ac45b308f4b51208a152792445cce0448c9a4ba5879dd8750
url: "https://pub.flutter-io.cn"
source: hosted
version: "5.4.0"
platform:
dependency: transitive
description:
@ -481,6 +529,14 @@ packages:
url: "https://pub.flutter-io.cn"
source: hosted
version: "0.5.1"
timezone:
dependency: transitive
description:
name: timezone
sha256: "1cfd8ddc2d1cfd836bc93e67b9be88c3adaeca6f40a00ca999104c30693cdca0"
url: "https://pub.flutter-io.cn"
source: hosted
version: "0.9.2"
tuple:
dependency: transitive
description:
@ -529,6 +585,14 @@ packages:
url: "https://pub.flutter-io.cn"
source: hosted
version: "1.0.0"
xml:
dependency: transitive
description:
name: xml
sha256: "5bc72e1e45e941d825fd7468b9b4cc3b9327942649aeb6fc5cdbf135f0a86e84"
url: "https://pub.flutter-io.cn"
source: hosted
version: "6.3.0"
sdks:
dart: ">=3.0.5 <4.0.0"
flutter: ">=3.10.0"

1
apps/flutter/dev_app/pubspec.yaml

@ -54,6 +54,7 @@ dependencies:
# Use with the CupertinoIcons class for iOS style icons.
cupertino_icons: ^1.0.2
flex_color_scheme: ^7.1.2
flutter_local_notifications: ^14.1.1
dev_dependencies:
flutter_test:

BIN
apps/flutter/dev_app/res/images/logo.png

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.4 KiB

Loading…
Cancel
Save