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

Sri*_*a K 2 javascript push-notification npm react-native react-native-push-notification

我正在使用react-native-push-notificationsnpm 库在我的应用程序中实现通知功能。但是,我想安排通知,我正在使用它PushNotification.localNotificationSchedule。但不知何故,当我试图触发时它不起作用。我已经粘贴了下面的代码。有人可以帮我解决这个问题我是这个功能的新手。

事件处理程序

const testPush = () =>{
      PushNotification.localNotificationSchedule({
        title:'My notification title',
        date:new Date(new Date().getTime()+3000),
        message:'My notification Message',
        allowWhileIdle:false
      });
    }
Run Code Online (Sandbox Code Playgroud)

事件触发器

      <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>
Run Code Online (Sandbox Code Playgroud)

Cha*_*ddy 6

您错过了文档中的小东西(https://github.com/zo0r/react-native-push-notification#readme),因为他们最近更新了它您必须为此创建频道。浏览文档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.);
Run Code Online (Sandbox Code Playgroud)

注意:没有频道,通知不起作用

在计划代码中添加以下行

频道 ID:“您的频道 ID”

添加如下代码

PushNotification.localNotificationSchedule({
    title:'My notification title',
    date:new Date(new Date().getTime()+3000),
    message:'My notification Message',
    allowWhileIdle:false,
    channelId: "your-channel-id"
  });
Run Code Online (Sandbox Code Playgroud)