【发布时间】:2021-12-29 01:59:32
【问题描述】:
我正在使用自定义声音在本地安排通知,但未播放自定义声音。事实上,没有显示通知警报。即使在 setNotificationsHandler 中将 show alert 和 shouldPlaySound 设置为 true。还应该提到的是,即使我在 app.json 的 expo-notifications 插件以及通知通道和通知内容输入中添加了自定义颜色,颜色也保持不变。
我检查了安装 apk 的设备的通知设置。存在通知频道,并且该频道的默认铃声也设置为自定义声音。但是它只是在通知到来时不播放。
相关代码:
import { StatusBar } from "expo-status-bar";
import React from "react";
import { Text, View, Platform, Button } from "react-native";
import * as Notifications from "expo-notifications";
Notifications.setNotificationHandler({
handleNotification: async () => ({
shouldShowAlert: true,
shouldPlaySound: true,
shouldSetBadge: false,
}),
});
export default function App() {
React.useEffect(() => {
setNotificationChannelAsync();
}, []);
return (
<View
style={{
flex: 1,
alignItems: "center",
justifyContent: "space-around",
}}
>
<Text>This App is for Testing Notifications</Text>
<Button
title="Press to schedule a notification"
onPress={async () => {
await scheduleNotification();
}}
/>
<StatusBar style="auto" />
</View>
);
}
const setNotificationChannelAsync = () => {
if (Platform.OS === "android") {
Notifications.setNotificationChannelAsync("sound", {
name: "sound notification",
importance: Notifications.AndroidImportance.HIGH,
vibrationPattern: [0, 250, 250, 250],
lightColor: "#FF231F7C",
sound: "adhan.wav",
});
}
};
async function scheduleNotification() {
await Notifications.scheduleNotificationAsync({
content: {
title: "You've got mail! ????",
body: "Here is the notification body",
data: { data: "goes here" },
sound: "adhan.wav",
color: "#FF231F7C",
},
trigger: { seconds: 5, channelId: "sound" },
});
}
以下是最小可重现示例的 github repo。 https://github.com/basit3407/testing-custom-sound-notifications
【问题讨论】:
标签: react-native expo android-notifications