[英]react-native expo error: Unhandled promise rejection: TypeError: null is not an object (evaluating 'RNAlarmNotification.scheduleAlarm')
I'm trying to use a notification package (react-native-alarm-notification) in my React Native project, but am running into some issues.我正在尝试在我的 React Native 项目中使用通知 package (react-native-alarm-notification),但遇到了一些问题。 After installing, trying to run scheduleAlarm() results in the following error:安装后,尝试运行 scheduleAlarm() 会导致以下错误:
[Unhandled promise rejection: TypeError: null is not an object (evaluating 'RNAlarmNotification.scheduleAlarm')]. [未处理的 promise 拒绝:TypeError:null 不是 object(正在评估“RNAlarmNotification.scheduleAlarm”)]。
I'm not entirely certain what the issue is, as the 'required' parameters of scheduleAlarm are filled and I am uncertain as to what null object is throwing an error.我不完全确定问题是什么,因为 scheduleAlarm 的“必需”参数已填充,我不确定 null object 抛出的错误。 Attempting to fill every possible parameter in scheduleAlarm still returns with null, as does giving only an alarm date and assuming default values are assigned.尝试在 scheduleAlarm 中填充每个可能的参数仍然会返回 null,就像只给出警报日期并假设已分配默认值一样。 Tracing the error location however shows the error coming from the regenerator-runtime and react-native modules that came from the default React project build, not from the notification package.然而,跟踪错误位置显示错误来自 regenerator-runtime 和来自默认 React 项目构建的 react-native 模块,而不是来自通知 package。
Does anyone have a solution to the problem?有没有人能解决这个问题?
Function.js: Function.js:
import { StatusBar } from 'expo-status-bar';
import React, {useState} from 'react';
import { StyleSheet, Text, View, TextInput, Button } from 'react-native';
import ReactNativeAN from 'react-native-alarm-notification';
export default function ShowTasks() {
async function alarmTest(){
console.log("Alarm test start");
const fireDate = ReactNativeAN.parseDate(new Date(Date.now() + 10000));
const alarmNotifData = {
title: "My Notification Title",
message: "My Notification Message",
channel: "my_channel_id",
small_icon: "../icon.png",
scheduleType:"once",
data: { foo: "bar" },
fire_date:fireDate,
};
const alarm = await ReactNativeAN.scheduleAlarm({...alarmNotifData});
console.log("Alarm test finish");
}
return (
<View>
<Button onPress={() => {alarmTest();}}
title= 'Click here to test alarm.'>
</Button>
<StatusBar style="auto" />
</View>
);
}
Rebuilding the App solved my issue:重建应用程序解决了我的问题:
npx react-native run-android
For android, the package will be linked automatically on the build.对于 android,package 将在构建时自动链接。 but if it doesn't then You need to link it manually.但如果没有,则您需要手动链接它。
Make following changes:进行以下更改:
android/app/src/main/java//MainApplication.java android/app/src/main/java//MainApplication.java
add import com.emekalites.react.alarm.notification.ANPackage; on the imports section
add packages.add(new ANPackage()); in List<ReactPackage> getPackages();
android/app/build.gradle android/应用程序/build.gradle
add implementation project(':react-native-alarm-notification') in the dependencies block
android/settings.gradle机器人/设置.gradle
add:添加:
include ':react-native-alarm-notification'
project(':react-native-alarm-notification').projectDir = new File(rootProject.projectDir, '../node_modules/react-native-alarm-notification/android')
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.