React native TypeError:_notification.default.localNotification不是函数

React native TypeError:_notification.default.localNotification不是函数,react-native,react-native-push-notification,React Native,React Native Push Notification,我是react native的新手,我正在尝试在我的应用程序中实现本地推送通知,当我尝试使用推送通知时,我出现以下错误: TypeError:\u notification.default.localNotification不是函数。(在“\u notification.default.localNotification()”中,“\u notification.default.localNotification”未定义)。 我试过各种各样的选择,但还是不知道哪里不对 以下是我的react本机推

我是react native的新手,我正在尝试在我的应用程序中实现本地推送通知,当我尝试使用推送通知时,我出现以下错误:
TypeError:\u notification.default.localNotification不是函数。(在“\u notification.default.localNotification()”中,“\u notification.default.localNotification”未定义)
。 我试过各种各样的选择,但还是不知道哪里不对

以下是我的react本机推送通知配置类:


export default class NotificationService {
  constructor(onNotification) {
    this.configure(onNotification);
    this.lastId = 0;
  }

  configure(onNotification) {
    PushNotification.configure({
      onNotification: onNotification,

      permissions: {
        alert: true,
        badge: true,
        sound: true
      },

      popInitialNotification: true,
    });
  }

  localNotification() {
    this.lastId++;
    PushNotification.createChannel(
      {
        channelId: "custom-channel-id", // (required)
      },
    );
    PushNotification.localNotification({
      channelId: "custom-channel-id",
      title: "Local Notification", 
      message: "My Notification Message", 
      playSound: false, 
      soundName: 'default', 
      actions: '["Yes", "No"]'
    });
  }

  scheduleNotification() {
    this.lastId++;
    PushNotification.localNotificationSchedule({
      date: new Date(Date.now() + (30 * 1000)), //30 seconds
      title: "Scheduled Notification", 
      message: "My Notification Message",
      playSound: true, 
      soundName: 'default', 
    });
  }

  checkPermission(cbk) {
    return PushNotification.checkPermissions(cbk);
  }

  cancelNotif() {
    PushNotification.cancelLocalNotifications({id: ''+this.lastId});
  }

  cancelAll() {
    PushNotification.cancelAllLocalNotifications();
  }
}
以及我要触发通知的类:

import { localNotification } from '../../service/notification.service'


notif() {
    localNotification();
}


.
.
.
.

<TouchableOpacity onPress={this.notif}>
  <LinearGradient
    style={theme.ButtonSettings}
    start={{ x: 0, y: 0 }}
    end={{ x: 1, y: 0 }}
    colors={[theme.GRADIENT_BLUE_1, theme.GRADIENT_BLUE_2]}>
    <MyText style={theme.buttonSettingsText}>zapisz</MyText>
   </LinearGradient>
  </TouchableOpacity>

从'../../service/notification.service'导入{localNotification}
notif(){
localNotification();
}
.
.
.
.
扎皮兹

我花了很多时间讨论这个问题,我不知道这是react本机推送通知配置的问题,或者可能只是导入或smh的一些愚蠢错误。

您没有在任何地方实例化通知服务

你可以试试:

import{NotificationService}来自“../../service/notification.service”
(顺便说一下,这是一个奇怪的导入路径,您的文件实际上是
.service
?)假设
notification.service
是您声明的
NotificationService
的文件名

然后,在您要调用该方法的组件中的某个位置,例如它是
构造函数
方法或
组件didmount
方法,您将声明
NotificationService
的一个实例,并向其传递一个
onNotification
方法:

const myNotificationService=newnotificationservice(onNotification)

然后在
notif
方法中,可以调用
localNotification
方法:

notif(){myNotificationService.localNotification()}


或者,在
ClassNotification
文件中,您可以导出一个实例,而不是导出
ClassNotification
,该实例将在其余代码中使用(单例模式)

我照你说的做了,现在我有了
ReferenceError:找不到变量:onNotification
根据这行:
const myNotificationService=newnotificationservice(onNotification)
你必须在你的代码中定义
onNotification
其他内容。从NotificationService类来看,这似乎是必须传递给类构造函数的方法。