我正在使用 react-native-push-notifications
npm 库在我的应用程序中实现通知功能。但是,我想安排通知,我正在使用 PushNotification.localNotificationSchedule
这样做。但不知何故,当我试图触发时它不起作用。我已经粘贴了下面的代码。有人可以帮我解决这个问题,我是这个功能的新手。
事件处理器
const testPush = () =>{
PushNotification.localNotificationSchedule({
title:'My notification title',
date:new Date(new Date().getTime()+3000),
message:'My notification Message',
allowWhileIdle:false
});
}
事件触发器
<Pressable
onPress={testPush}
style={{ marginTop: 13 }}
>
<View style={{ backgroundColor: '#20232a', padding: 13, alignItems: 'center', borderRadius: 10 }}>
<Text style={{ color: '#ffffff' }}>Test push</Text>
</View>
</Pressable>
最佳答案
您错过了文档 ( https://github.com/zo0r/react-native-push-notification#readme ) 中的小东西,因为他们最近更新了它 您必须为此创建 channel 。 浏览文档 https://github.com/zo0r/react-native-push-notification#channel-management-android 仅适用于 android,在 IOS 中你的代码工作正常
PushNotification.createChannel(
{
channelId: "channel-id", // (required)
channelName: "My channel", // (required)
channelDescription: "A channel to categorise your notifications", // (optional) default: undefined.
soundName: "default", // (optional) See `soundName` parameter of `localNotification` function
importance: 4, // (optional) default: 4. Int value of the Android notification importance
vibrate: true, // (optional) default: true. Creates the default vibration patten if true.
},
(created) => console.log(`createChannel returned '${created}'`) // (optional) callback returns whether the channel was created, false means it already existed.);
注意:没有 channel ,通知不起作用
在计划代码中添加以下行
channelId: "your-channel-id"
添加如下代码
PushNotification.localNotificationSchedule({
title:'My notification title',
date:new Date(new Date().getTime()+3000),
message:'My notification Message',
allowWhileIdle:false,
channelId: "your-channel-id"
});
https://stackoverflow.com/questions/64887102/