我正在开发一个应用程序,每当我在前台和后台收到通知时,我需要一个自定义的声音来播放。它在前台工作正常,但在后台,它播放设备的默认声音。
我正在用一部实体手机测试Realme GT(RMX2202)应用,我已经在模拟器中试过了,同样的情况也发生了
我正在使用软件包 flutter_local_notifications
我可能错过了什么?我已经检查了多个教程和人与相同的问题,但没有一个解决我的问题。
这是我的代码:
WidgetsFlutterBinding.ensureInitialized();
RemoteMessage? initialMessage = newLogged == true
? await FirebaseMessaging.instance.getInitialMessage()
: null;
void initMessaging() async {
final token = await FirebaseMessaging.instance.getToken();
log(DateTime.now().toString() +
' ' +
' ------------ FIREBASE TOKEN -----> $token ');
firebaseToken = token.toString();
FirebaseMessaging messaging = FirebaseMessaging.instance;
late FlutterLocalNotificationsPlugin fltNotification;
var androiInit = AndroidInitializationSettings("@mipmap/ic_launcher");
var iosInit = DarwinInitializationSettings();
var initSetting = InitializationSettings(android: androiInit, iOS: iosInit);
fltNotification = FlutterLocalNotificationsPlugin();
fltNotification.initialize(initSetting);
var androidDetails = AndroidNotificationDetails(
'id',
'name',
channelDescription: 'description',
importance: Importance.max,
priority: Priority.high,
playSound: true,
ticker: 'ticker',
icon: ('@mipmap/ic_launcher'),
sound: RawResourceAndroidNotificationSound('newservice'),
// sound: UriAndroidNotificationSound("assets/sounds/classic.mp3"),
);
var iosDetails = DarwinNotificationDetails();
var generalNotificationDetails =
NotificationDetails(android: androidDetails, iOS: iosDetails);
FirebaseMessaging.onMessage.listen((RemoteMessage message) {
RemoteNotification? notification = message.notification;
AndroidNotification? android = message.notification?.android;
if (notification != null && android != null) {
fltNotification.show(notification.hashCode, notification.title,
notification.body, generalNotificationDetails);
}
});
}
Android清单:
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.mkis.gb24">
<application
android:label="new GB24"
android:name="${applicationName}"
android:icon="@mipmap/ic_launcher">
<activity
android:name=".MainActivity"
android:exported="true"
android:launchMode="singleTop"
android:theme="@style/LaunchTheme"
android:configChanges="orientation|keyboardHidden|keyboard|screenSize|smallestScreenSize|locale|layoutDirection|fontScale|screenLayout|density|uiMode"
android:hardwareAccelerated="true"
android:windowSoftInputMode="adjustResize">
<!-- Specifies an Android theme to apply to this Activity as soon as
the Android process has started. This theme is visible to the user
while the Flutter UI initializes. After that, this theme continues
to determine the Window background behind the Flutter UI. -->
<meta-data
android:name="io.flutter.embedding.android.NormalTheme"
android:resource="@style/NormalTheme"
/>
<meta-data
android:name="com.google.firebase.messaging.default_notification_icon"
android:resource="@mipmap/ic_launcher"
/>
<meta-data
android:name="com.google.firebase.messaging.default_notification_channel_id"
android:value="default_notification_channel_id" />
<intent-filter>
<action android:name="FLUTTER_NOTIFICATION_CLICK" />
<category android:name="android.intent.category.DEFAULT" />
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
<action android:name="com.google.firebase.MESSAGING_EVENT" />
</intent-filter>
</activity>
<!-- Don't delete the meta-data below.
This is used by the Flutter tool to generate GeneratedPluginRegistrant.java -->
<meta-data
android:name="flutterEmbedding"
android:value="2" />
</application>
</manifest>
1条答案
按热度按时间gab6jxml1#
已通过强制使channel.id、channel.name和channel.description的名称完全相同来解决此问题。
所以基本上改变了这一点:
到这
并补充道:
编辑:同样重要的是,在使用firebase时,必须在发送
click_action
通知时指定通知通道。最后,在AndroidManifest.xml中:
因为如果未指定通道,Android将使用其他通道,并使用设备的默认通知声音。