gpt4 book ai didi

push-notification - 自定义声音推送通知不起作用(Flutter)

转载 作者:IT王子 更新时间:2023-10-29 06:37:05 26 4
gpt4 key购买 nike

{
"to": "XXXX",
"notification": {
"title": "ASAP Alert",
"body": "Please open your app"
},
"data": {
"screen": "/Nexpage1",
"sound": "alarm",
"click_action": "FLUTTER_NOTIFICATION_CLICK"
}
}

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

here's the mp3 file inside the raw file

最佳答案

阅读 this它似乎应该在 Android 上自动管理(如果你没有使用 notification builder )但你必须指定 .mp3 扩展名并将其放入 notification 字段而不是 data 一个..

"sound": "alarm.mp3"

iOS 在后台的行为非常不同,但您也可以通过在通知负载中设置 sound: 字段来使用自定义声音。总之 .mp3 不是有效的 APN 通知文件格式,您还需要指定文件扩展名。

"sound": "filename.caf"

关注Apple documentation以便为您的应用伪造自定义声音文件。

mp3 不是有效格式

Preparing Custom Alert Sounds

Local and remote notifications can specify custom alert sounds to beplayed when the notification is delivered. You can package the audiodata in an aiff, wav, or caf file. Because they are played by thesystem-sound facility, custom sounds must be in one of the followingaudio data formats:

  • Linear PCM

  • MA4 (IMA/ADPCM)

  • µLaw

  • aLaw

Place custom sound files in your app bundle or in theLibrary/Sounds folder of your app’s container directory. Customsounds must be under 30 seconds when played. If a custom sound isover that limit, the default system sound is played instead.

You can use the afconvert tool to convert sounds. For example, toconvert the 16-bit linear PCM system sound Submarine.aiff to IMA4audio in a CAF file, use the following command in the Terminal app:

afconvert /System/Library/Sounds/Submarine.aiff ~/Desktop/sub.caf -d ima4 -f caff -v

例如,要将您的 mp3 文件转换为 caf 文件,您可以在终端中键入:

afconvert -f caff -d LEI16 alarm.mp3 alarm.caf

阅读此 doc为了深入了解所有通用和特定的通知有效负载字段。

更新

我已经测试了 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"
}
}

enter image description here

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

afconvert -f caff -d LEI16 alert.mp3 alert.caf

具有不同文件名的相同 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"
}
}

请记住将文件添加到您的主包中。

enter image description here

如果应用程序终止或在后台运行,这会起作用。

如果你想在应用程序处于前台时显示警报并播放声音,你必须在 onMessage 事件上对其进行管理,就像有人已经告诉你的那样 here ,或者您可以使用 platform-channel在这里使用 Notification.Builder 构建您自己的通知在 Android 和 UNNotificationCenter在 iOS 上(例如)。

更新

这个问题已经解决了。参见 here官方评论:

Hey all 👋

As part of our roadmap (#2582) we've just shipped a complete rework ofthe firebase_messaging plugin that aims to solve this and many otherissues.

If you can, please try out the dev release (see the migration guidefor upgrading and for changes) and if you have any feedback then joinin the discussion here.

Given the scope of the rework I'm going to go ahead and close thisissue in favor of trying out the latest plugin.

Thanks everyone 🤓

关于push-notification - 自定义声音推送通知不起作用(Flutter),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54002617/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com