我正在使用自定义声音通知功能。自定义声音在版本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);
}
}
答案 0 :(得分:1)
尝试一下。
首先将声音文件放置在res / raw文件夹中,并在通知通道中包含setSound(),
第二点,多亏了@PatelDhara,还要确保从设备中删除您的应用,以便新的频道配置能够正常工作。
然后将其放在代码行下面,对我来说很好用。
首先创建频道。
const channel = new firebase.notifications.Android.Channel(name, Description, firebase.notifications.Android.Importance.High)
.setDescription(ChannelName)
.setSound(default.mp3) //Set audio here
...
firebase.notifications().android.createChannel(channel);
然后:
const notification = new firebase.notifications.Notification()
.setNotificationId(id)
.setTitle(title)
.setSound(channel.sound); //Get sound from channel and set in notification builder
notification
.android.setChannelId(channel.channelId)
firebase.notifications().displayNotification(notification);
还要确保每当获得有效负载时,位于res / raw文件夹中的音频文件名和要获得有效负载的声音值都应该相同。
赞:
{
"to" : "DEVICE-TOKEN",
"notification" : {
"body" : "NOTIFICATION BODY",
"title" : "NOTIFICATION TITILE",
"sound" : "default"
}
}
注意:在响应本机中,Android O在0.56.0之前的版本(反应本机版本)中存在问题,因此请尝试升级您的版本。
答案 1 :(得分:1)
花了一整天后,我找到了解决方案。 我刚刚清除了应用数据,然后重新启动了手机,一切正常。
有一个问题,因为我在特定类型的通知上使用了自定义声音。
我还通过了有效负载中的频道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",
}
}