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文件中的代码?
ShadowSheep 很好地回答了这个问题,但为了让 iOS 声音发挥作用,我想澄清一件事。
您必须将声音添加到 XCode 中(这就是 ShadowSheep 所说的将资产包含在 中main bundle
)。您只需将音频文件(.caf 或上述其他支持的格式)拖放到 XCode 中的根目录(通常称为 Runner for Flutter)即可:
如果您已完成此操作并遵循上述问题/答案中描述的设置,那么您应该可以开展业务。
阅读此内容后,似乎应该在Android上自动对其进行管理(如果您未使用通知生成器),但是您也必须指定.mp3
扩展名,并将其放在notification
field(而不是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 bundle
或Library/Sounds
应用程序容器目录的 文件夹中。自定义声音播放时必须在30秒以内。如果自定义声音超过该限制,则会播放默认的系统声音。您可以使用该
afconvert
工具转换声音。例如,要将a中的16位线性PCM系统声音转换Submarine.aiff
为IMA4
音频,请CAF file
在“终端”应用中使用以下命令:Run Code Online (Sandbox Code Playgroud)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
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。
小智 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
和 的文件。alarm
res/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)
归档时间: |
|
查看次数: |
2312 次 |
最近记录: |