android - react-native-firebase 自定义声音在 android 8、android 9 上不起作用
问题描述
我正在开发自定义声音通知功能。自定义声音在版本 8 以下的 iOS 和 Android 上正常工作。通知也出现在所有版本的 Android 中。但是,自定义声音不适用于 android 8 和 9。我还创建了一个频道 ID。
下面我分享了我的代码。谁能帮帮我吗?提前致谢。
async checkPermission() {
const enabled = await firebase.messaging().hasPermission();
if (enabled) {
this.getToken();
} else {
this.requestPermission();
}
let channel = new firebase.notifications.Android.Channel(
"channelId",
"Channel Name",
firebase.notifications.Android.Importance.Max
).setDescription("A natural description of the channel");
firebase.notifications().android.createChannel(channel);
firebase
.notifications()
.getInitialNotification()
.then(notificationOpen => {
if (notificationOpen) {
const action = notificationOpen.action;
const notification = notificationOpen.notification;
}
});
// the listener returns a function you can use to unsubscribe
this.unsubscribeFromNotificationListener = firebase
.notifications()
.onNotification(notification => {
if (Platform.OS === "android") {
const channel = new firebase.notifications.Android.Channel(
"channelId",
"Channel Name",
firebase.notifications.Android.Importance.Max
)
.setDescription("A natural description of the channel")
.setSound(
notification.data.sound ? notification.data.sound : "default"
);
firebase.notifications().android.createChannel(channel);
const localNotification = new firebase.notifications.Notification({
sound: notification.data.sound
? notification.data.sound
: "default",
show_in_foreground: true
})
.setNotificationId(notification.notificationId)
.setTitle(notification.title)
.setSubtitle(notification.subtitle)
.setBody(notification.body)
.setData(notification.data)
.setSound(
notification.data.sound ? notification.data.sound : "default"
)
.android.setSmallIcon("notification_icon_black")
.android.setChannelId("channelId")
.android.setAutoCancel(true)
.android.setVibrate(1000)
.android.setColor("#000000") // you can set a color here
.android.setGroup(notification.notificationId)
.android.setPriority(firebase.notifications.Android.Priority.High);
firebase
.notifications()
.displayNotification(localNotification)
.catch(err => console.error(err));
} else if (Platform.OS === "ios") {
const localNotification = new firebase.notifications.Notification()
.setNotificationId(notification.notificationId)
.setTitle(notification.title)
.setSound(
notification.data.sound ? notification.data.sound : "default"
)
.setSubtitle(notification.subtitle)
.setBody(notification.body)
.setData(notification.data)
.ios.setBadge(notification.ios.badge);
firebase
.notifications()
.displayNotification(localNotification)
.catch(err => console.error(err));
}
});
const notificationOpen = await firebase
.notifications()
.getInitialNotification();
if (notificationOpen) {
const action = notificationOpen.action;
const notification = notificationOpen.notification;
if (notification.data) {
//handle data
}
}
this.notificationOpenedListener = firebase
.notifications()
.onNotificationOpened(notificationOpen => {
const notification = notificationOpen.notification;
if (notification.data) {
//handle data
}
});
}
async requestPermission() {
try {
await firebase.messaging().requestPermission();
// User has authorised
this.getToken();
} catch (error) {
// User has rejected permissions
console.log("permission rejected");
}
}
async getToken() {
let fcmToken = await firebase.messaging().getToken();
if (fcmToken) {
console.log("fcm token===>", fcmToken);
}
}
解决方案
花了一整天后,我找到了解决方案。我刚刚清除了应用程序数据并重新启动了我的手机,它工作正常。
存在一个问题,因为我在特定类型的通知上使用了自定义声音。
我还在有效负载中传递了频道 ID,以便在应用程序处于后台和终止模式时根据通知类型获得不同的声音。
{
"to" : "DEVICE_TOKEN",
"notification" : {
"body" : "NOTIFICATION BODY",
"title" : "NOTIFICATION TITILE",
"sound" : "default", //change your sound based on your requirement
"android_channel_id":"CHANEEL_NAME",
}
}
推荐阅读
- python - NoneType 实际上是什么意思?
- javascript - 如何向客户端发送响应并将响应放入客户端的变量中(快递)
- php - ESP8266 到 000webhost 没有获取数据一个 php 文件
- java - 如何从 S3 存储桶添加 jar 文件的依赖项?
- python - awscli 1.18.34 要求 botocore==1.15.34,但您将拥有不兼容的 botocore 1.15.49
- javascript - 如何从数组中删除特定元素?
- python - 如何将powershell输出写入文件?[Python]
- syntax - “@”在 Elixir 中有什么作用?
- bdd - 如何修复 Nunit 3 日志中无序的场景和步骤执行并使它们有序
- python - AWS_IO_TLS_CTX_ERROR