I'm trying to use a notification package (react-native-alarm-notification) in my React Native project, but am running into some issues. After installing, trying to run scheduleAlarm() results in the following error:
[Unhandled promise rejection: TypeError: null is not an object (evaluating '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. 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. 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.
Does anyone have a solution to the problem?
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. but if it doesn't then You need to link it manually.
Make following changes:
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
add:
include ':react-native-alarm-notification'
project(':react-native-alarm-notification').projectDir = new File(rootProject.projectDir, '../node_modules/react-native-alarm-notification/android')
Checkout the documentation here