_expoModulesCore.uuidv4 is not a function on expo local schedule notification react-native

323 views Asked by At

I want to implement local notification from my expo react native app. When I run schedule notification it throws _expoModulesCore.uuidv4 is not a function error.

Here is my code

Package.json

{
  "main": "node_modules/expo/AppEntry.js",
  "scripts": {
    "start": "expo start",
    "android": "expo start --android",
    "ios": "expo start --ios",
    "web": "expo start --web",
    "eject": "expo eject"
  },
  "dependencies": {
    "@expo/webpack-config": "^19.0.0",
    "@react-navigation/native": "^6.1.7",
    "@react-navigation/stack": "^6.3.17",
    "expo": "^49.0.13",
    "expo-modules-core": "^1.8.0",
    "expo-notifications": "^0.23.0",
    "expo-status-bar": "~1.6.0",
    "firebase": "^10.4.0",
    "minimatch": "^9.0.3",
    "react": "18.2.0",
    "react-dom": "18.2.0",
    "react-native": "0.72.3",
    "react-native-gesture-handler": "~2.12.1",
    "react-native-maps": "1.7.1",
    "react-native-paper": "^5.10.0",
    "react-native-reanimated": "~3.4.2",
    "react-native-safe-area-context": "4.7.1",
    "react-native-screens": "~3.24.0",
    "react-native-status-bar-height": "^2.6.0",
    "react-native-web": "~0.19.7",
    "uuid": "^9.0.1",
    "uuid-random": "^1.3.2"
  },
  "devDependencies": {
    "@babel/core": "^7.22.9",
    "babel-eslint": "^10.1.0",
    "eslint": "^8.46.0",
    "eslint-config-airbnb": "^19.0.4",
    "eslint-config-prettier": "^9.0.0",
    "eslint-plugin-import": "^2.28.0",
    "eslint-plugin-jsx-a11y": "^6.7.1",
    "eslint-plugin-prettier": "^5.0.0",
    "eslint-plugin-react": "^7.33.1",
    "prettier": "^3.0.1"
  },
  "private": true
}

notification.js file

import Constants from "expo-constants";
import * as Notifications from "expo-notifications";
import React, { useState, useEffect, useRef } from "react";
import {  Platform } from "react-native";

Notifications.setNotificationHandler({
  handleNotification: async () => ({
    shouldShowAlert: true,
    shouldPlaySound: true,
    shouldSetBadge: true,
  }),
});

export default function Notification() {
  const [expoPushToken, setExpoPushToken] = useState("");  
  const [notification, setNotification] = useState(false);
  const notificationListener = useRef();
  const responseListener = useRef();

  useEffect(() => {
    registerForPushNotificationsAsync().then((token) =>
      setExpoPushToken(token)
    );

    notificationListener.current =
      Notifications.addNotificationReceivedListener((notification) => {
        setNotification(notification);
      });

    responseListener.current =
      Notifications.addNotificationResponseReceivedListener((response) => {
        console.log(response);
      });

    return () => {
      Notifications.removeNotificationSubscription(
        notificationListener.current
      );
      Notifications.removeNotificationSubscription(responseListener.current);
    };
  }, []);

  return (
    null
  );
}

export async function schedulePushNotification(
  title,
  body
) {
  const id = await Notifications.scheduleNotificationAsync({
    content: {
      title: title,
      body: body,
      // sound: 'default',
    },
    trigger: null
  });
  console.log("notif id on scheduling",id)
  return id;
}

async function registerForPushNotificationsAsync() {
  let token;
  if (Constants.isDevice) {
    const { status: existingStatus } =
      await Notifications.getPermissionsAsync();
    let finalStatus = existingStatus;
    if (existingStatus !== "granted") {
      const { status } = await Notifications.requestPermissionsAsync();
      finalStatus = status;
    }
    if (finalStatus !== "granted") {
      alert("Failed to get push token for push notification!");
      return;
    }
    token = (await Notifications.getExpoPushTokenAsync()).data;
    console.log(token);
  } else {
    alert("Must use physical device for Push Notifications");
  }

  if (Platform.OS === "android") {
    Notifications.setNotificationChannelAsync("default", {
      name: "default",
      importance: Notifications.AndroidImportance.MAX,
      vibrationPattern: [0, 250, 250, 250],
      sound: true,
      lightColor: "#FF231F7C",
      lockscreenVisibility: Notifications.AndroidNotificationVisibility.PUBLIC,
      bypassDnd: true,
    });
  }

  return token;
}

export async function cancelNotification(notifId){
  await Notifications.cancelScheduledNotificationAsync(notifId);
} 

App.js I also called this

Notifications.setNotificationHandler({
  handleNotification: async () => ({
    shouldShowAlert: true,
    shouldPlaySound: true,
    shouldSetBadge: true,
  }),
});

When I run this code on button click I get this error

await schedulePushNotification('Test', 'Testing here');

Error

enter image description here

Need help.

1

There are 1 answers

0
Ankush Patil On

It may be a case that some of your project's expo packages are missing, which are required to run the expo-notifications package.

I fixed this issue by running this command:

expo install --fix