react-native expo error: unhandle promise jecting: typeError: null 不是对象(正在评估 'RNAlarmNotification.s



我试图在我的React Native项目中使用通知包(React - Native -alarm-notification),但我遇到了一些问题。安装后,尝试运行scheduleAlarm()会导致以下错误:

[未处理的承诺拒绝:TypeError: null不是一个对象(评估'RNAlarmNotification.scheduleAlarm')].

我不完全确定问题是什么,因为scheduleAlarm的"必需"参数被填充,我不确定什么空对象抛出错误。尝试填充scheduleAlarm中的每个可能的参数仍然返回null,就像只给出一个警报日期并假设分配了默认值一样。但是,跟踪错误位置会显示错误来自于regenerator-runtime和React -native模块,这些模块来自于默认的React项目构建,而不是来自通知包。

谁有解决这个问题的办法?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>

);
}

重新构建应用程序解决了我的问题:

npx react-native run-android

对于android,该包将在构建时自动链接。但是如果没有,那么你需要手动链接。

做以下修改:

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

add implementation project(':react-native-alarm-notification') in the dependencies block

android/settings.gradle

添加:

include ':react-native-alarm-notification'
project(':react-native-alarm-notification').projectDir = new File(rootProject.projectDir, '../node_modules/react-native-alarm-notification/android')

查看这里的文档

最新更新