react-native-push-notification / ios

React Native Push Notification API for iOS.
MIT License
732 stars 281 forks source link

TypeError: undefined is not a function (near '..._pushNotificationIos.default.localNotification...') #361

Closed DeveloperLocalizer closed 1 year ago

DeveloperLocalizer commented 2 years ago

I get the following error.

TypeError: undefined is not a function (near '..._pushNotificationIos.default.localNotification...')

I code a minimal example to send local push notifications when the app dont run at the foreground.

This is the code in my App.js

I add the library like descripted in the README.md.

Has anyone had this error before or knows what this could be? I have expanded the respective AppDelegates, and executed a "pod install". The React native version is 0.66.4

` import React, {useRef, useState, useEffect} from "react"; import {StyleSheet, Text, View, AppState} from "react-native"; import PushNotificationIOS from '@react-native-community/push-notification-ios';

const App = () => { const appState = useRef(AppState.currentState); const [appStateVisible, setAppStateVisible] = useState(appState.current);

useEffect(() => {
    console.log("called useEffect");
    AppState.addEventListener("change", _handleAppStateChange)

    return () => {
        AppState.removeEventListener("change", _handleAppStateChange)
    }
}, []);

const _handleAppStateChange = (nextAppState) => {
    if (appState.current.match(/inactive|background/) &&
        nextAppState === "active") {
        console.log("App has come to the foreground");
    } else {
        PushNotificationIOS.localNotification({
            title: 'Cold Weather Alert',
            message: `It's 3 degrees outside.`,
            playSound: true,
            soundName: 'default',
        });
    }

    appState.current = nextAppState;
    setAppStateVisible(appState.current);

    console.log("AppState: " , appState.current);
}

return (
    <View style={styles.container}>
        <Text style={styles.textStyle}>
            Current state is:
        </Text>
    </View>
);

};

const styles = StyleSheet.create({ container: { flex: 1, justifyContent: "center", alignItems: "center", }, textStyle: { textAlign: "center", fontSize: 30, color: "black" } });

export default App;

`

Hamidkhanswb commented 2 years ago

It is deprecated you can use other methods mentioned in the the documentation

Naturalclar commented 1 year ago

please use addNotificationRequest for that purpose 😄