GithubHelp home page GithubHelp logo

luiskarlos / react-native-alarm Goto Github PK

View Code? Open in Web Editor NEW

This project forked from liplylie/react-native-simple-alarm

0.0 0.0 0.0 1.72 MB

Alarm clock functionality for react native ios and android using react-native-push-notification and react-native-community/async-storage

JavaScript 40.83% Ruby 8.39% C 0.15% Objective-C 8.53% Java 9.06% TypeScript 30.17% Swift 0.14% Starlark 2.73%

react-native-alarm's Introduction

react-native-simple-alarm

Alarm clock functionality for react native ios and android built using react-native-push-notification and react-native-community/async-storage.

ReactNativeChatImageAudio

Installing (React Native >= 0.60.0)

Under the hood this library is using react-native-push-notification, @react-native-community/async-storage, and @react-native-community/push-notification-ios. These libraries will need to be installed as well.

npm install --save react-native-simple-alarm @react-native-community/async-storage @react-native-community/push-notification-ios react-native-push-notification

or

yarn add react-native-simple-alarm @react-native-community/async-storage @react-native-community/push-notification-ios react-native-push-notification

For iOS using cocoapods, run:

$ cd ios/ && pod install

Installing (React Native <= 0.59.x)

npm install --save react-native-simple-alarm @react-native-community/async-storage @react-native-community/push-notification-ios react-native-push-notification

or

yarn add react-native-simple-alarm @react-native-community/async-storage @react-native-community/push-notification-ios react-native-push-notification

Use react-native link to add the library to your project:

Please follow the installation for react-native-push-notification as well.

Example

$ cd example
$ yarn install

if ios:

$ cd ios/ && pod install
$ yarn ios

if android:

$ yarn android

You may come across these issues while running the example: oblador/react-native-vector-icons#1074 oblador/react-native-vector-icons#328

createAlarm

Prop Description Default
date Required: - Date for alarm to get triggered. ISO Format. example: 1996-10-15T00:05:32.000Z [string] None
active Set to true when a push notification is scheduled. Setting to true schedules an alarm notification [boolean] false
message Notification Message on Push Notification [string] "Alarm"
snooze Sets snooze time for alert. In minutes [number] 1
userInfo Any data that is needed for the alarm. [Object] {}

Also includes the props from react-native-push-notification Local Notifications except for repeatType.

ID is created by the react-native-simple-alarm. ID is uuid for ios, and number for android. OID is a property created by react-native-simple-alarm that is used to cancel ios alarms/scheduled push notifications.

import { createAlarm } from 'react-native-simple-alarm';
import moment from 'moment'

createAlarm = async () => {
  try {
    await createAlarm({
        active: false,
        date: new Date().toISOString();,
        message: 'message',
        snooze: 1,
      });
  } catch (e) {}
}

getAlarms

Returns an array of all alarms.

import { getAlarms } from 'react-native-simple-alarm';

getAlarms = async () => {
  try {
    const alarms = await getAlarms();
  } catch (e) {}
}

getAlarmById

Returns alarm object given its id. If trying to get an id that does not exist, it will return null and throw an error.

import { getAlarmById } from 'react-native-simple-alarm';

getAlarms = async () => {
  let id = '07699912-87d9-11ea-bc55-0242ac130003';
  
  try {
    const alarm = await getAlarmById(id);
  } catch (e) {}
}

editAlarm

Given alarm object, edits the alarm. If alarm active prop is set to true, it will create a scheduled push notification for alarm based on the date. If alarm active prop is set to false, it will cancel scheduled push notifications for alarm. Returns edited alarm. If alarm id does not exist, it will return null and throw an error.

import { editAlarm } from 'react-native-simple-alarm';
import moment from 'moment';

editAlarm = async () => {
  let id = '07699912-87d9-11ea-bc55-0242ac130003';
  
  try {
    await editAlarm({
        id,
        date: moment().add(1, 'days')format();,
        snooze: 1,
        message: 'Message',
        active: true
      });
  } catch (e) {}
}

activateAlarmById

Given alarm id, sets alarm active prop to true, and creates scheduled push notification for alarm based on the date. Use this instead of editAlarm if you simply want to set the alarm active prop to true. If trying to get an id that does not exist, it will return null and throw an error.

import { activateAlarmById } from 'react-native-simple-alarm';

activateAlarm = async () => {
  let id = '07699912-87d9-11ea-bc55-0242ac130003';
  
  try {
    await activateAlarmById(id);
  } catch (e) {}
}

cancelAlarmById

Given alarm id, sets alarm active prop to false, and cancels scheduled push notification for alarm based on the date. Call this when you want to cancel the alarm, and keep the alarm as well. Sets active prop to false. If trying to get an id that does not exist, it will return null and throw an error.

import { cancelAlarmById } from 'react-native-simple-alarm';

cancelAlarmById = async () => {
  let id = '07699912-87d9-11ea-bc55-0242ac130003';
  
  try {
    await cancelAlarmById(id);
  } catch (e) {}
}

deleteAlarmById

Given alarm id, deletes alarm and cancels the scheduled push notification. Returns array of alarms after deletion. If trying to get an id that does not exist, it will return null and throw an error.

import { deleteAlarmById } from 'react-native-simple-alarm';
deleteAlarmById = async () => {
  let id = '07699912-87d9-11ea-bc55-0242ac130003';
  
  try {
    await deleteAlarmById(id);
  } catch (e) {}
}

deleteAllAlarms

Deletes all alarms and cancels all alarm scheduled push notifications. Returns array of alarms after deletion (which will be empty array).

import { deleteAllAlarms } from 'react-native-simple-alarm';
deleteAllAlarms = async () => {
  try {
    await deleteAllAlarms();
  } catch (e) {}
}

Note to self:

react-native-alarm's People

Contributors

liplylie avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.