如何在Flutter中设置后台自定义声音通知?

7
我写了一个Flutter应用程序,但无法使用自定义音频配置Firebase云消息传递。 我可以接收通知,但当应用程序在后台运行时,它们会带有默认的声音。 在前景中,我使用本地通知库,它运行良好,但我也需要在后台工作。
这是我发送到云消息传递的内容:
{
   "to":"<firebase_token>",
   "notification":{
      "sound":"arrive",
      "title":"My Title",
      "body":"My body"
   },
   "data":{
      "click_action":"FLUTTER_NOTIFICATION_CLICK",
      "status":"done",
      "screen":"screenA",
      "message":"ACTION"
   },
   "apns":{
      "headers":{
         "apns-priority":"5",
         "apns-push-type":"background"
      },
      "payload":{
         "aps":{
            "content-available":1
         }
      }
   }
}

这是我的工作本地通知配置:
void showNotification({
    String title,
    String body,
  }) {
    var androidPlatformChannelSpecifics = AndroidNotificationDetails(
      'your channel id',
      'your channel name',
      'your channel description',
      importance: Importance.Max,
      priority: Priority.Max,
      ticker: 'ticker',
      playSound: true,
      sound: RawResourceAndroidNotificationSound('arrive')
    );

    var iOSPlatformChannelSpecifics = IOSNotificationDetails();

    var platformChannelSpecifics = NotificationDetails(
      androidPlatformChannelSpecifics,
      iOSPlatformChannelSpecifics,
    );
    notifications.show(0, title, body, platformChannelSpecifics,
        payload: 'Custom_Sound',);
  }

本地通知库看到了我的自定义声音,但云消息推送播放默认声音。可能的问题是什么?

我的声音位于:android\app\src\main\res\raw\arrive.mp3

我的导入内容如下:

    flutter_local_notifications: ^1.4.3 
    firebase_messaging: ^6.0.16

Flutter医生:

[√] Flutter (Channel stable, v1.12.13+hotfix.9, on Microsoft Windows [Version 10.0.18362.836], locale hu-HU)
    • Flutter version 1.12.13+hotfix.9 at C:\flutter src\flutter
    • Framework revision f139b11009 (8 weeks ago), 2020-03-30 13:57:30 -0700
    • Engine revision af51afceb8
    • Dart version 2.7.2

 
[√] Android toolchain - develop for Android devices (Android SDK version 28.0.3)
    • Android SDK at C:\Users\koros\AppData\Local\Android\sdk
    • Android NDK location not configured (optional; useful for native profiling support)
    • Platform android-29, build-tools 28.0.3
    • Java binary at: C:\Program Files\Android\Android Studio\jre\bin\java
    • Java version OpenJDK Runtime Environment (build 1.8.0_152-release-1343-b01)
    • All Android licenses accepted.

[√] Android Studio (version 3.4)
    • Android Studio at C:\Program Files\Android\Android Studio
    • Flutter plugin version 35.3.1
    • Dart plugin version 183.6270
    • Java version OpenJDK Runtime Environment (build 1.8.0_152-release-1343-b01)

[√] VS Code (version 1.45.1)
    • VS Code at C:\Users\koros\AppData\Local\Programs\Microsoft VS Code
    • Flutter extension version 3.10.2

[√] Connected device (1 available)
    • SM A520F • 52003aa8f4ea64d5 • android-arm64 • Android 8.0.0 (API 26) (emulator)

• No issues found!
1个回答

6

您可以编写 Firebase Messaging 的后台处理程序方法,然后在后台处理程序中调用 showNotification 方法。示例代码:

Future<dynamic> onBackgroundMessageHandler(Map<String, dynamic> message) async {

  if (message['data'] != null) {
    final data = message['data'];
    final title = data['title'];
    final body = data['message'];
    showNotification(title, body);
  } 

  return Future<void>.value();
}


FirebaseMessaging _firebaseMessaging = FirebaseMessaging();

_firebaseMessaging.configure(onBackgroundMessage: Platform.isIOS ? null : onBackgroundMessageHandler);

2
谢谢,我试过了,但对我没用。本地通知库在onBackgroundMessageHandler中无法唤醒:/这些代码在“onMessage”参数中起作用,但在这里不起作用。你觉得这是正常的还是我的本地通知有问题呢? - korosiadam99
3
iOS是什么意思? - Nicks
1
在跟随这个操作后,我能够展示带有自定义声音的通知,但是系统托盘上会出现两个通知,一个来自 Firebase Messaging,另一个来自本地通知。我是在 Firebase Messaging 的 on backgroundMessage 中调用了 localNotification.show 方法。如何不显示 Firebase Messaging 的通知? - Lallawmzuala khawlhring
@korosiadam99,我也遇到了“PluginRegistrantCallback”错误。你在android文件夹里做了什么? - Sharath B Naik
@Lallawmzualakhawlhring,你有关于托盘上双重通知的解决方案吗? - Md. Asaduzzaman
显示剩余4条评论

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接