自定义声音推送通知不起作用(颤振)

ali*_*ali 4 push-notification dart flutter firebase-cloud-messaging

{
  "to": "XXXX",
  "notification": {
    "title": "ASAP Alert",
    "body": "Please open your app"
  },
  "data": {
    "screen": "/Nexpage1",
    "sound": "alarm",
    "click_action": "FLUTTER_NOTIFICATION_CLICK"
  }
}
Run Code Online (Sandbox Code Playgroud)

上面是我的推送通知负载。我已经在原始文件夹中插入了alarm.mp3文件,但是它仍然没有给我警报声,我也尝试了alarm.mp3,json有什么问题吗?是因为我的dart文件中的代码?

这是原始文件中的mp3文件

J. *_*Saw 8

ShadowSheep 很好地回答了这个问题,但为了让 iOS 声音发挥作用,我想澄清一件事。

您必须将声音添加到 XCode 中(这就是 ShadowSheep 所说的将资产包含在 中main bundle)。您只需将音频文件(.caf 或上述其他支持的格式)拖放到 XCode 中的根目录(通常称为 Runner for Flutter)即可:

Xcode 图像

如果您已完成此操作并遵循上述问题/答案中描述的设置,那么您应该可以开展业务。


sha*_*eep 6

阅读此内容后,似乎应该在Android上自动对其进行管理(如果您未使用通知生成器),但是您也必须指定.mp3扩展名,并将其放在notificationfield(而不是data一个)中。

"sound": "alarm.mp3"
Run Code Online (Sandbox Code Playgroud)

iOS引擎盖下的行为非常不同,但是您也可以通过sound:在通知有效负载中设置字段来使用自定义声音。无论如何,.mp3这不是有效的APN通知文件格式,因此您还需要指定文件扩展名。

"sound": "filename.caf"
Run Code Online (Sandbox Code Playgroud)

请遵循Apple文档,以便为您的应用构建您的自定义声音文件。

mp3格式无效

准备自定义警报声音

本地和远程通知可以指定传递通知时要播放的自定义警报声音。您可以将音频数据打包为aiff,wav或caf文件。因为它们是由系统声音设备播放的,所以自定义声音必须采用以下音频数据格式之一:

  • Linear PCM

  • MA4 (IMA/ADPCM)

  • µLaw

  • aLaw

将自定义声音文件放在您app bundleLibrary/Sounds应用程序容器目录的 文件夹中。自定义声音播放时必须在30秒以内。如果自定义声音超过该限制,则会播放默认的系统声音。

您可以使用该afconvert工具转换声音。例如,要将a中的16位线性PCM系统声音转换Submarine.aiffIMA4 音频,请CAF file在“终端”应用中使用以下命令:

afconvert /System/Library/Sounds/Submarine.aiff ~/Desktop/sub.caf -d ima4 -f caff -v
Run Code Online (Sandbox Code Playgroud)

为了使示例将mp3文件转换为caf文件,您可以在终端中输入:

afconvert -f caff -d LEI16 alarm.mp3 alarm.caf
Run Code Online (Sandbox Code Playgroud)

阅读此文档,以深入了解所有通用和特定的通知有效负载字段。

更新

我已经测试了Android部分,并且可以确认将.mp3文件放在res/raw/文件夹中时,声音会按照记录和预期的方式播放。

那是我的通知有效载荷:

{
 "to" : "my_device_token",
 "collapse_key" : "type_a",
 "priority" : "high",
 "notification" : {
     "body" : "Test Notification body for custom sound {{datestamp}}",
     "title": "Custom sound alert.mp3",
     "sound": "alert.mp3"
 }
}
Run Code Online (Sandbox Code Playgroud)

在此处输入图片说明

在以这种方式将.mp3文件转换为.caf文件之后,我还测试了iOS版本:

afconvert -f caff -d LEI16 alert.mp3 alert.caf
Run Code Online (Sandbox Code Playgroud)

json具有不同文件名的相同有效负载有效:

{
 "to" : "my_device_token",
 "collapse_key" : "type_a",
 "priority" : "high",
 "notification" : {
     "body" : "Test Notification body for custom sound {{datestamp}}",
     "title": "Custom sound alert.mp3",
     "sound": "alert.caf"
 }
}
Run Code Online (Sandbox Code Playgroud)

记住要在中添加文件main bundle

在此处输入图片说明

如果该应用终止或在后台运行,则该方法有效。

如果您想在应用程序处于前台状态时显示警报并播放声音,则必须onMessage像有人已经在此处告诉您那样在事件中进行管理,或者您可以在此处使用平台渠道来构建带有Notification的自己的通知。例如,Android 上的Builder和iOS 上的UNNotificationCenter

  • @shadowsheep 感谢您提供的信息。现在在最新的更新中我需要包含扩展名吗?如果是,那么如何分别处理iOS上的.caf和Android上的.mp3?我将用户令牌保存在服务器上以向他们发送通知,但我没有有关用户正在使用的设备的数据。 (2认同)

小智 6

对我来说,我使用flutter_local_notifications来创建通知通道。

包含此功能(可以创建多个通知通道)

Future<void> _createNotificationChannel(String id, String name,
String description, String sound) async {
final flutterLocalNotificationsPlugin = FlutterLocalNotificationsPlugin();
var androidNotificationChannel = AndroidNotificationChannel(
  id,
  name,
  description,
  sound: RawResourceAndroidNotificationSound(sound),
  playSound: true,
);

await flutterLocalNotificationsPlugin
    .resolvePlatformSpecificImplementation<
    AndroidFlutterLocalNotificationsPlugin>()
    ?.createNotificationChannel(androidNotificationChannel);}
Run Code Online (Sandbox Code Playgroud)

调用initState中的函数:(这创建了2个通知通道)

_createNotificationChannel("channel_id_1", "channel_name", "description", "alert");
_createNotificationChannel("channel_id_2", "channel_name", "description", "alarm");
Run Code Online (Sandbox Code Playgroud)

请记住以的文件格式保存alert和 的文件。alarmres/raw.mp3

使用此有效负载:

{
"notification": {
    "title": "My First Notification",
    "body": "Hello, I'm push notification"
},
"data": {
    "title": "My First Notification"
},
"android": {
    "notification": {
        "channel_id": "channel_id_1"
    }
},
"to": "device_token"}
Run Code Online (Sandbox Code Playgroud)

  • 那IOS呢? (3认同)
  • 即使应用程序处于后台或终止状态,这也能工作吗? (2认同)