如何在颤振背景上设置自定义声音通知?

kor*_*m99 7 push-notification flutter firebase-cloud-messaging

我编写了一个 flutter 应用程序,但无法使用自定义声音配置 firebase 云消息传递。我收到通知,但当应用程序处于后台时,它们会带有默认声音。在前台,我使用本地通知库,它运行良好,但我也需要在后台工作。

\n

这是我发送的云消息:

\n
{\n   "to":"<firebase_token>",\n   "notification":{\n      "sound":"arrive",\n      "title":"My Title",\n      "body":"My body"\n   },\n   "data":{\n      "click_action":"FLUTTER_NOTIFICATION_CLICK",\n      "status":"done",\n      "screen":"screenA",\n      "message":"ACTION"\n   },\n   "apns":{\n      "headers":{\n         "apns-priority":"5",\n         "apns-push-type":"background"\n      },\n      "payload":{\n         "aps":{\n            "content-available":1\n         }\n      }\n   }\n}\n
Run Code Online (Sandbox Code Playgroud)\n

这是我工作的本地通知配置:

\n
void showNotification({\n    String title,\n    String body,\n  }) {\n    var androidPlatformChannelSpecifics = AndroidNotificationDetails(\n      'your channel id',\n      'your channel name',\n      'your channel description',\n      importance: Importance.Max,\n      priority: Priority.Max,\n      ticker: 'ticker',\n      playSound: true,\n      sound: RawResourceAndroidNotificationSound('arrive')\n    );\n\n    var iOSPlatformChannelSpecifics = IOSNotificationDetails();\n\n    var platformChannelSpecifics = NotificationDetails(\n      androidPlatformChannelSpecifics,\n      iOSPlatformChannelSpecifics,\n    );\n    notifications.show(0, title, body, platformChannelSpecifics,\n        payload: 'Custom_Sound',);\n  }\n
Run Code Online (Sandbox Code Playgroud)\n

因此本地通知库会看到我的自定义声音,但云消息传递将播放默认声音。可能是什么问题呢?

\n

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

\n

我的进口是:

\n
    flutter_local_notifications: ^1.4.3 \n    firebase_messaging: ^6.0.16\n
Run Code Online (Sandbox Code Playgroud)\n

颤医生:

\n
[\xe2\x88\x9a] Flutter (Channel stable, v1.12.13+hotfix.9, on Microsoft Windows [Version 10.0.18362.836], locale hu-HU)\n    \xe2\x80\xa2 Flutter version 1.12.13+hotfix.9 at C:\\flutter src\\flutter\n    \xe2\x80\xa2 Framework revision f139b11009 (8 weeks ago), 2020-03-30 13:57:30 -0700\n    \xe2\x80\xa2 Engine revision af51afceb8\n    \xe2\x80\xa2 Dart version 2.7.2\n\n \n[\xe2\x88\x9a] Android toolchain - develop for Android devices (Android SDK version 28.0.3)\n    \xe2\x80\xa2 Android SDK at C:\\Users\\koros\\AppData\\Local\\Android\\sdk\n    \xe2\x80\xa2 Android NDK location not configured (optional; useful for native profiling support)\n    \xe2\x80\xa2 Platform android-29, build-tools 28.0.3\n    \xe2\x80\xa2 Java binary at: C:\\Program Files\\Android\\Android Studio\\jre\\bin\\java\n    \xe2\x80\xa2 Java version OpenJDK Runtime Environment (build 1.8.0_152-release-1343-b01)\n    \xe2\x80\xa2 All Android licenses accepted.\n\n[\xe2\x88\x9a] Android Studio (version 3.4)\n    \xe2\x80\xa2 Android Studio at C:\\Program Files\\Android\\Android Studio\n    \xe2\x80\xa2 Flutter plugin version 35.3.1\n    \xe2\x80\xa2 Dart plugin version 183.6270\n    \xe2\x80\xa2 Java version OpenJDK Runtime Environment (build 1.8.0_152-release-1343-b01)\n\n[\xe2\x88\x9a] VS Code (version 1.45.1)\n    \xe2\x80\xa2 VS Code at C:\\Users\\koros\\AppData\\Local\\Programs\\Microsoft VS Code\n    \xe2\x80\xa2 Flutter extension version 3.10.2\n\n[\xe2\x88\x9a] Connected device (1 available)\n    \xe2\x80\xa2 SM A520F \xe2\x80\xa2 52003aa8f4ea64d5 \xe2\x80\xa2 android-arm64 \xe2\x80\xa2 Android 8.0.0 (API 26) (emulator)\n\n\xe2\x80\xa2 No issues found!\n
Run Code Online (Sandbox Code Playgroud)\n

小智 6

您可以为 firebase 消息传递编写后台处理程序方法,然后可以在后台处理程序中调用 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);
Run Code Online (Sandbox Code Playgroud)

  • 对ios有什么想法吗? (3认同)
  • 谢谢,我尝试了一下,但对我不起作用。本地通知库无法在 onBackgroundMessageHandler 中唤醒:/ 该行在“onMessage”参数中有效,但在这里不行。您认为我的本地通知是正常的还是有问题? (2认同)