首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >TypeError:_notification.default.localNotification不是函数

TypeError:_notification.default.localNotification不是函数
EN

Stack Overflow用户
提问于 2021-04-13 21:25:23
回答 1查看 237关注 0票数 0

我是react原生的新手,我正在尝试在我的应用程序中实现本地推送通知,当我尝试使用推送通知时,我遇到了这个错误:TypeError: _notification.default.localNotification is not a function. (In '_notification.default.localNotification()', '_notification.default.localNotification' is undefined)。我尝试了各种选择,但仍然找不到哪里错了。

下面是我的react-native-push-native的配置类:

代码语言:javascript
复制
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();
  }
}

以及我想要触发通知的类:

代码语言:javascript
复制
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>

我在这上面坐了很长时间,我不知道这是react-native-push-native配置的问题,或者只是导入或smh的一些愚蠢的错误。

EN

回答 1

Stack Overflow用户

发布于 2021-04-14 03:07:00

您没有在任何地方实例化Notification服务。

您可以尝试:

import { NotificationService } from '../../service/notification.service' (顺便说一下,这是一个看起来很奇怪的导入路径,您的文件实际上是.service吗?)假设notification.service是在其中声明NotificationService的文件的名称

然后,在您想要调用该方法的组件中的某个位置,例如,它是constructor方法或componentDidMount方法,您可以声明NotificationService的一个实例,并向其传递一个onNotification方法:

const myNotificationService = new NotificationService(onNotification)

然后在您的notif方法中,您可以像这样调用localNotification方法:

notif() {myNotificationService.localNotification()}

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

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/67075785

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档