TypeError: _notification.default.localNotification is not a function

Viewed 672

I am kinda new in react native and I am trying to implement local push notification in my app and when I try to use push notifications I have this error: TypeError: _notification.default.localNotification is not a function. (In '_notification.default.localNotification()', '_notification.default.localNotification' is undefined). I tried variety options but still cant figure out what is wrong.

Here is my configuration class for react-native-push-notifications:


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();
  }
}

and my class where I want to trigger notification:

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>

I sitting on this a lot of hours, I dont know that is problem with react-native-push-notification configuration or maybe just some stupid mistake with importing or smh.

1 Answers

You are not instantiating your Notification service anywhere.

You could try:

import { NotificationService } from '../../service/notification.service' (by the way this is a weird looking import path, is your file actually .service?) assuming notification.service is the name of the file in which you declared NotificationService

then somewhere in the component where you want to call the method, for example it's constructor method or componentDidMount method you would declare an instance of the NotificationService, passing it an onNotification method:

const myNotificationService = new NotificationService(onNotification)

and then in your notif method, you can call the localNotification method as such:

notif() {myNotificationService.localNotification()}

Alternatively, inside your ClassNotification file, instead of exporting ClassNotification, you could export an instance of it that would be used throughout the rest of your code (singleton pattern)

Related