【发布时间】:2021-05-29 20:52:49
【问题描述】:
我一直致力于添加自定义声音以使用 firebase-admin 版本 ^9.2.0 和 react-native-push-notification 版本 ^5.1.0 为 react-native 应用推送通知。
我没有升级到最新版本的react-native-push-notification 的原因是即使使用正确的通道配置,自定义声音似乎也不起作用。我们也在使用 expo,使用 7+ 版本时似乎会导致启动错误。
我有两个名为 regular.mp3 和 mass.mp3 的 mp3 文件。发送推送通知的 firebase 函数使用公共数据对象发送消息,但也使用特定于平台的字段来发送推送通知声音:
admin.messaging().send({
data: {
title,
body,
lat: data.Latitude.toString(),
lng: data.Longitude.toString(),
notificationType: data.NotificationType.toString(),
},
notification:{title,body},
apns:{
payload:{
aps:{
alert:{
title,
body,
},
sound: data.NotificationType === 1 ? "mass.mp3" : "regular.mp3",
},
},
},
android: {
priority: "high",
data: {
sound: data.NotificationType === 1 ? "mass" : "regular",
},
notification: {
sound: data.NotificationType === 1 ? "mass" : "regular",
},
},
topic: topic,
})
据我了解,android 下的 data 字段确实包含在应用程序被终止并收到通知时将添加到根级 data 对象的有效负载。该插件的来源似乎也使用了确切的 data 字段来设置通知声音(在 RNReceivedMessageHandler.java 中):
JSONObject data = getPushData(notificationData.get("data"));
if (data != null) {
if (!bundle.containsKey("message")) {
bundle.putString("message", data.optString("alert", null));
}
if (!bundle.containsKey("title")) {
bundle.putString("title", data.optString("title", null));
}
if (!bundle.containsKey("sound")) {
bundle.putString("soundName", data.optString("sound", null));
}
if (!bundle.containsKey("color")) {
bundle.putString("color", data.optString("color", null));
}
这是我目前得到的:
- 当应用在前台时,自定义声音效果很好
- 当应用在后台时,自定义声音效果很好
- 应用程序被杀死时播放默认声音
这是当前用于管理通知的代码:
在 index.ts 中:
PushNotification.configure({
// (optional) Called when Token is generated (iOS and Android)
onRegister: function(token) {
console.log("TOKEN:", token);
},
// (required) Called when a remote is received or opened, or local notification is opened
onNotification: function(notification) {
console.log("NOTIFICATION:", notification);
// process the notification
// (required) Called when a remote is received or opened, or local notification is opened
//notification.finish(PushNotificationIOS.FetchResult.NoData);
},
// (optional) Called when Registered Action is pressed and invokeApp is false, if true onNotification will be called (Android)
onAction: function(notification) {
console.log("ACTION:", notification.action);
console.log("NOTIFICATION:", notification);
// process the action
},
// (optional) Called when the user fails to register for remote notifications. Typically occurs when APNS is having issues, or the device is a simulator. (iOS)
onRegistrationError: function(err) {
console.error(err.message, err);
},
// IOS ONLY (optional): default: all - Permissions to register.
permissions: {
alert: true,
badge: true,
sound: true,
},
popInitialNotification: true,
requestPermissions: true,
});
在 App.js 中
CreateIncidentPushNotification=(remoteMessage)=>{
const {data} = remoteMessage;
PushNotification.localNotification({
title: data.title,
message: data.body,
playSound: true,
soundName: data.notificationType === "1" ? "mass" : "regular",
});
}
我想知道是否有其他人对可能发生的事情有所了解。即使应用程序被终止,通知仍然可以到达我的设备,这很棒。唯一缺少的部分是声音。
【问题讨论】:
标签: android firebase react-native push-notification react-native-push-notification