javascript - 在 React Native 中安排本地通知不起作用

我正在使用 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/

相关文章:

node.js - docker-compose npm install && npm start

react-native - React native 每次打开页面时如何执行函数

scala - scala中以下两种模式匹配案例有什么区别

wordpress - Disqus iframe 透明度不适用于 Chrome 87

python - 删除txt中的特定字符

python - Windows 上的 Gcloud 命令(使用 git bash)正在记录错误 :

vue.js - 如何制作可点击的带有悬停效果的q卡?

c - 打印函数,C语言的hello world

azure-devops - Azure Devops - Terraform 任务失败并出现错误

c++ - 仅当使用 unordered_map 而不是 vector 时,将 const 作为此参