簡體   English   中英

react-native expo 錯誤:未處理 promise 拒絕:TypeError:null 不是 object(正在評估“RNAlarmNotification.scheduleAlarm”)

[英]react-native expo error: Unhandled promise rejection: TypeError: null is not an object (evaluating 'RNAlarmNotification.scheduleAlarm')

我正在嘗試在我的 React Native 項目中使用通知 package (react-native-alarm-notification),但遇到了一些問題。 安裝后,嘗試運行 scheduleAlarm() 會導致以下錯誤:

[未處理的 promise 拒絕:TypeError:null 不是 object(正在評估“RNAlarmNotification.scheduleAlarm”)]。

我不完全確定問題是什么,因為 scheduleAlarm 的“必需”參數已填充,我不確定 null object 拋出的錯誤。 嘗試在 scheduleAlarm 中填充每個可能的參數仍然會返回 null,就像只給出警報日期並假設已分配默認值一樣。 然而,跟蹤錯誤位置顯示錯誤來自 regenerator-runtime 和來自默認 React 項目構建的 react-native 模塊,而不是來自通知 package。

有沒有人能解決這個問題?

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,package 將在構建時自動鏈接。 但如果沒有,則您需要手動鏈接它。

進行以下更改:

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/應用程序/build.gradle

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

機器人/設置.gradle

添加:

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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM