npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

react-native-alarm-module

v3.1.0

Published

schedules alarms

Downloads

25

Readme

react-native-alarm-module

A native module for scheduling alarms. the main aim is to execute a headless js task from a set alarm. this was needed for executing a time critical task.

Supported Platforms

  • Android (Min. SDK 21)

Installation

npm install react-native-alarm-module
yarn add react-native-alarm-module

Usage

// setAlarmClock, setExact, setAndAllowWhileIdle, setExactAndAllowWhileIdle
import {setAlarm, cancelAlarm} from 'react-native-alarm-module';
import {View, Button, ToastAndroid, AppRegistry} from 'react-native';

// You have to register the task you would like executed as a headless task
AppRegistry.registerHeadlessTask('ShowToastTask', () => () => ToastAndroid.show('Alarm toast!', ToastAndroid.SHORT));

export default function App() {
  const [lastDate, setLastDate] = useState(new Date(Date.now() + 5 * 1000));

  const setAlarmOnPress = useCallback(() => {
    const newDate = new Date(Date.now() + 5 * 1000);
    setLastDate(newDate);

    setAlarm({
      taskName: 'ShowToastTask', // required
      timestamp: newDate.valueOf(), // required
      type: 'setAlarmClock', // optional
      allowedInForeground: true, // optional 
      wakeup: true, // optional
      extra: 'something extra', // optional
    });

    ToastAndroid.show(
      `alarm set for ${newDate.toISOString()}`,
      ToastAndroid.SHORT,
    );
  }, []);

  const cancel = useCallback(() => {
    cancelAlarm({
      taskName: 'ShowToastTask',
      timestamp: lastDate.valueOf(),
    });
    ToastAndroid.show(
      `alarm cancelled for ${lastDate.toISOString()}`,
      ToastAndroid.SHORT,
    );
  }, [lastDate]);

  return (
    <View>
      <Button onPress={setAlarmOnPress} title="Set Alarm in 5 seconds" />
      <Button onPress={cancel} title="Cancel last alarm" />
    </View>
  );
}

Notes

Alarm fired in background

Currently you cannot launch your app when the alarm is fired and your app is not in foreground in newer Android versions due to the newly added limitations.

setAlarmClock type for android

When setAlarmClock type is used to set an alarm on Android, this library tries to launch your application if the clock is touched. If the needed class is not found, it simply won't do anything if the clock is touched.

When you want to handle the intent, you have to use subscribeToOnNewIntent method. remember to call .remove() when you are done with it. To handle the launch intent from the alarm clock icon, you have to handle the event early in your app or you will miss it.

Canceling an alarm

cancelAlarm Uses your task name and the timestamp value the alarm was set to fire to cancel the alarm. it uses the time in seconds without the first digit as the request code under the hood to create a pending intent to cancel the alarm.

RTC_WAKEUP

To set an alarm with wakeup: true you have to add the following to your AndroidManifest.xml file, Otherwise your app will crash:

<uses-permission android:name="android.permission.WAKE_LOCK" />

Rebooting

This library does not save and restore your scheduled alarm if the device is rebooted. you have to save them yourself before scheduling them with this library and setup them again on reboot using the android.intent.action.BOOT_COMPLETED broadcast and the related permission.

Contributing

See the contributing guide to learn how to contribute to the repository and the development workflow.

License

MIT