Allow background sound notification

This commit is contained in:
2026-02-26 21:43:05 +08:00
parent 6ccf820438
commit 9cc99e612a
7 changed files with 265 additions and 142 deletions
+45 -4
View File
@@ -7,6 +7,10 @@ class NotificationService {
static final FlutterLocalNotificationsPlugin _plugin =
FlutterLocalNotificationsPlugin();
static const String _channelId = 'tasq_default_channel';
static const String _channelName = 'General';
static const String _highChannelId = 'tasq_custom_sound_channel_2';
static const String _highChannelName = 'High Priority';
/// Call during app startup, after any necessary permissions have been
/// granted. The callback receives a [NotificationResponse] when the user
@@ -19,6 +23,38 @@ class NotificationService {
);
const iosSettings = DarwinInitializationSettings();
// Ensure the Android notification channel exists with sound and vibration
final androidImpl = _plugin
.resolvePlatformSpecificImplementation<
AndroidFlutterLocalNotificationsPlugin
>();
if (androidImpl != null) {
const channel = AndroidNotificationChannel(
_channelId,
_channelName,
description: 'General notifications for TasQ',
importance: Importance.defaultImportance,
playSound: true,
enableVibration: true,
);
const highChannel = AndroidNotificationChannel(
_highChannelId,
_highChannelName,
description: 'High priority notifications (sound + vibration)',
importance: Importance.max,
playSound: true,
enableVibration: true,
// 👇 Tell Android to use your specific file in the raw folder
sound: RawResourceAndroidNotificationSound('tasq_notification'),
);
try {
await androidImpl.createNotificationChannel(channel);
} catch (_) {}
try {
await androidImpl.createNotificationChannel(highChannel);
} catch (_) {}
}
await _plugin.initialize(
settings: const InitializationSettings(
android: androidSettings,
@@ -36,12 +72,17 @@ class NotificationService {
String? payload,
}) async {
const androidDetails = AndroidNotificationDetails(
'default_channel',
'General',
importance: Importance.high,
_highChannelId,
_highChannelName,
importance: Importance.max,
priority: Priority.high,
playSound: true,
enableVibration: true,
sound: RawResourceAndroidNotificationSound('tasq_notification'),
);
const iosDetails = DarwinNotificationDetails(
sound: 'tasq_notification.wav',
);
const iosDetails = DarwinNotificationDetails();
await _plugin.show(
id: id,
title: title,