Skip to content

Chore: migrate to awesome notifications in bg service #327

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

Open
wants to merge 8 commits into
base: main
Choose a base branch
from
Open
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
3 changes: 2 additions & 1 deletion android/app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ android {
applicationId "com.exptech.dpip"
minSdkVersion 26
targetSdkVersion 35
versionCode 300001305
versionCode 300001400
versionName flutterVersionName
multiDexEnabled true
}
Expand Down Expand Up @@ -90,6 +90,7 @@ flutter {
}

dependencies {
implementation 'org.maplibre.gl:android-sdk:11.5.2'
implementation 'androidx.core:core-splashscreen:1.0.1'
implementation 'com.google.android.material:material:1.12.0'
coreLibraryDesugaring 'com.android.tools:desugar_jdk_libs:2.1.4'
Expand Down
5 changes: 3 additions & 2 deletions android/app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,12 @@
android:name="${applicationName}"
android:icon="@mipmap/ic_launcher"
android:theme="@style/LaunchTheme"
android:enableOnBackInvokedCallback="true">
android:enableOnBackInvokedCallback="true"
android:allowBackup="false"
tools:replace="android:allowBackup">
<activity
android:name=".MainActivity"
android:exported="true"
android:allowBackup="true"
android:largeHeap="true"
android:launchMode="singleTop"
android:theme="@style/NormalTheme"
Expand Down
2 changes: 1 addition & 1 deletion ios/Runner/AppDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ class AppDelegate: FlutterAppDelegate, CLLocationManagerDelegate {
}

// MARK: - Setup Methods

private func setupFlutterChannels() {
guard let controller = window?.rootViewController as? FlutterViewController else { return }

Expand Down
4 changes: 2 additions & 2 deletions lib/core/notify.dart
Original file line number Diff line number Diff line change
Expand Up @@ -240,7 +240,7 @@ Future<void> notifyInit() async {
channelGroupKey: 'group_evacuation',
channelKey: 'evacuation_minor-general-v2',
channelName: '避難資訊(一般)',
channelDescription: '所在地(鄉鎮)發布防空、土石流、淹水或堰塞湖避難警訊時',
channelDescription: '所在地(鄉鎮)發布防空、土石流、淹水或堰塞湖避難資訊時',
importance: NotificationImportance.High,
defaultPrivacy: NotificationPrivacy.Public,
playSound: true,
Expand Down Expand Up @@ -285,7 +285,7 @@ Future<void> notifyInit() async {
channelGroupKey: 'group_tsunami',
channelKey: 'tsunami-silent-v2',
channelName: '太平洋海嘯消息 (無聲通知)',
channelDescription: '地震報告所在地震度 3 以下的地區',
channelDescription: '太平洋海嘯消息發布時',
groupAlertBehavior: GroupAlertBehavior.Children,
importance: NotificationImportance.Min,
defaultPrivacy: NotificationPrivacy.Public,
Expand Down
69 changes: 37 additions & 32 deletions lib/core/service.dart
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@ import 'package:dpip/global.dart';
import 'package:dpip/utils/extensions/latlng.dart';
import 'package:dpip/utils/location_to_code.dart';
import 'package:dpip/utils/log.dart';
import 'package:flutter/cupertino.dart';
import 'package:awesome_notifications/awesome_notifications.dart';
import 'package:flutter_background_service/flutter_background_service.dart';
import 'package:flutter_local_notifications/flutter_local_notifications.dart';
import 'package:flutter/material.dart';
import 'package:intl/intl.dart';
import 'package:maplibre_gl/maplibre_gl.dart';
import 'package:permission_handler/permission_handler.dart';
Expand Down Expand Up @@ -97,26 +97,24 @@ void _setupPositionListener() {

Future<void> _initializeAndroidForegroundService() async {
_isAndroidServiceInitialized = true;
const notificationChannel = AndroidNotificationChannel(
'my_foreground',
'前景自動定位',
description: '前景自動定位',
importance: Importance.low,
);

final notifications = FlutterLocalNotificationsPlugin();

await notifications.initialize(
const InitializationSettings(
iOS: DarwinInitializationSettings(),
android: AndroidInitializationSettings('@mipmap/ic_launcher'),
),
await AwesomeNotifications().initialize(
null, // 使用預設 launcher icon
[
NotificationChannel(
channelKey: 'my_foreground',
channelName: '前景自動定位',
channelDescription: '背景定位服務通知',
importance: NotificationImportance.Low,
defaultColor: const Color(0xFF2196f3),
ledColor: Colors.white,
locked: true,
playSound: false,
onlyAlertOnce: true,
)
],
);

await notifications
.resolvePlatformSpecificImplementation<AndroidFlutterLocalNotificationsPlugin>()
?.createNotificationChannel(notificationChannel);

await _backgroundService.configure(
androidConfiguration: AndroidConfiguration(
onStart: _onServiceStart,
Expand Down Expand Up @@ -146,7 +144,6 @@ Future<void> _onServiceStart(ServiceInstance service) async {
GlobalProviders.init();

final locationService = LocationService();
final notifications = FlutterLocalNotificationsPlugin();

// Setup service event listeners
service.on(ServiceEvent.stopService.name).listen((event) {
Expand All @@ -162,12 +159,16 @@ Future<void> _onServiceStart(ServiceInstance service) async {
if (service is AndroidServiceInstance) {
// Initialize foreground service and notification
await service.setAsForegroundService();
await notifications.show(
888,
'DPIP',
'前景服務啟動中...',
const NotificationDetails(
android: AndroidNotificationDetails('my_foreground', '前景自動定位', icon: '@drawable/ic_stat_name', ongoing: true),
await AwesomeNotifications().createNotification(
content: NotificationContent(
id: 888,
channelKey: 'my_foreground',
title: 'DPIP',
body: '前景服務啟動中...',
notificationLayout: NotificationLayout.Default,
locked: true,
autoDismissible: false,
icon: 'resource://drawable/ic_stat_name',
),
);

Expand All @@ -183,6 +184,7 @@ Future<void> _onServiceStart(ServiceInstance service) async {

// Define the periodic location update task
Future<void> updateLocation() async {
_locationUpdateTimer?.cancel();
if (!await service.isForegroundService()) return;

// Get current position and location info
Expand All @@ -209,12 +211,15 @@ Future<void> _onServiceStart(ServiceInstance service) async {
final notificationBody = '$timestamp\n$latitude,$longitude $locationName';

service.invoke(ServiceEvent.sendDebug.name, {'notifyBody': notificationBody});
await notifications.show(
888,
notificationTitle,
notificationBody,
const NotificationDetails(
android: AndroidNotificationDetails('my_foreground', '前景自動定位', icon: 'ic_stat_name', ongoing: true),
await AwesomeNotifications().createNotification(
content: NotificationContent(
id: 888,
channelKey: 'my_foreground',
title: notificationTitle,
body: notificationBody,
notificationLayout: NotificationLayout.Default,
locked: true,
autoDismissible: false,
),
);
service.setForegroundNotificationInfo(title: notificationTitle, content: notificationBody);
Expand Down
3 changes: 1 addition & 2 deletions pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name: dpip
description: "Disaster Prevention Information Platform"
publish_to: 'none'
version: 3.0.013
version: 3.0.014

environment:
sdk: '>=3.7.0 <4.0.0'
Expand Down Expand Up @@ -31,7 +31,6 @@ dependencies:
url: https://github.com/ExpTechTW/flutter_background_service.git
path: packages/flutter_background_service
ref: master
flutter_local_notifications: ^19.1.0
flutter_localizations:
sdk: flutter
flutter_markdown: ^0.7.7
Expand Down
Loading