我想从我的expo react原生应用程序实现本地通知。当我运行调度通知时,它抛出_expoModulesCore.uuidv4不是函数错误。
这是我的代码
包.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
}
字符串
通知.js文件
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
Notifications.setNotificationHandler({
handleNotification: async () => ({
shouldShowAlert: true,
shouldPlaySound: true,
shouldSetBadge: true,
}),
});
型
在单击按钮时运行此代码时,出现此错误
第一个月
错误
1条答案
按热度按时间41ik7eoe1#
可能是您项目的某些expo包丢失了,而这些包是运行Expo-notifications包所必需的。
我通过运行这个命令修复了这个问题:
字符串