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.