Reputation: 21
I would like to use custom notification sounds. I have written the code as follows
Future<void> _createNotificationChannel(String id, String name, String description, String sound) async {
final flutterLocalNotificationsPlugin = FlutterLocalNotificationsPlugin();
var androidNotificationChannel = AndroidNotificationChannel(
id,
name,
description: description,
sound: sound == "default" ? null : RawResourceAndroidNotificationSound(sound),
playSound: true,
);
await flutterLocalNotificationsPlugin.resolvePlatformSpecificImplementation<AndroidFlutterLocalNotificationsPlugin>()?.createNotificationChannel(androidNotificationChannel);
}
try {
_createNotificationChannel("sos", "sos_channel", "緊急通報用", "new_sos_sound");
_createNotificationChannel("claim", "claim_channel", "クレーム用", "new_claim_sound");
_createNotificationChannel("default", "default_channel", "通常の通知", "default");
print('************************* Success created notification channel *****************************');
} catch (e) {
print('*************************Error(createNotificationChannel): $e *****************************');
}
async function pushToDevice(
token: string,
title: string,
body: string,
badgeCount: number,
messageType: string
): Promise<void> {
const pushMessage = (
_token: string,
_title: string,
_body: string,
_badgeCount: number,
_messageType: string
): admin.messaging.TokenMessage => {
return {
token: _token,
data: {
metadata: "someMetadata",
messageType: _messageType
},
notification: {
title: _title,
body: _body
},
android: {
priority: "high",
notification: {
title: _title,
body: _body,
channelId:
_messageType == "sos"
? "sos"
: _messageType == "claim"
? "claim"
: "default"
}
},
}
}
await admin
.messaging()
.send(pushMessage(token, title, body, badgeCount, messageType))
}
This works when tested with "flutter run" using a real device, but does not work after the app is released.
When I check from the device settings screen, I was able to confirm that the notificaiton channel was created.
I can also confirm that custom sounds are set for each channel.
However, I cannot hear the custom sound on the device settings screen. When I was running with "flutter run", I could hear the custom sound from the device settings screen.
I don't know where the problem is.
Sorry for my poor English. Thank you in advance for your help.
◎flutter doctor
[√] Flutter (Channel stable, 3.13.3, on Microsoft Windows [Version 10.0.22000.2538], locale ja-JP)
[√] Windows Version (Installed version of Windows is version 10 or higher)
[√] Android toolchain - develop for Android devices (Android SDK version 30.0.3)
[√] Chrome - develop for the web
[√] Visual Studio - develop Windows apps (Visual Studio Community 2022 17.1.3)
[√] Android Studio (version 4.2)
[√] VS Code (version 1.84.2)
[√] Connected device (3 available)
[√] Network resources
I don't know where the problem is.
Upvotes: 2
Views: 62