1.0.1 • Published 2 years ago

react-native-updated-alarm v1.0.1

Weekly downloads
-
License
ISC
Repository
github
Last release
2 years ago

refactored-native-alarm

npm version install size react-native

Alarm for React Native

Table of Contents

Installation

Adding the package

npm

$ npm install refactored-native-alarm

yarn

$ yarn add rrefactored-native-alarm

Manipulating codes in your project

Check android SDK

This package is compiled with Android SDK Platform 29.
Your project SDK version doesn't matter.
Android SDK Platform 29 must be installed.

Register components

Go to AndroidManifest.xml and register the service and receiver.

<manifest ... >
    <application ... >
      <activity ... >
      </activity>

      <!-- Add the following code -->
      <service android:name="com.sea.nativealarm.util.AlarmService" android:enabled="true" android:exported="false" />
      <receiver android:name="com.sea.nativealarm.util.AlarmReceiver" android:enabled="true" android:exported="false" />
      <receiver android:name="com.sea.nativealarm.util.BootReceiver" android:enabled="false" android:exported="false" >
          <intent-filter android:priority="999">
              <action android:name="android.intent.action.BOOT_COMPLETED" />
          </intent-filter>
      </receiver>

    </application>
</manifest>

Overriding MainActivity methods

Go to MainActivity.java and override the onCreate and createReactActivityDelegate methods as follows.
It can work properly during the process of calling parameters and rebooting the android with alarm functions.

@Override
protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);

  ComponentName receiver = new ComponentName(this, BootReceiver.class);
  PackageManager packageManager = this.getPackageManager();

  packageManager.setComponentEnabledSetting(receiver,
          PackageManager.COMPONENT_ENABLED_STATE_ENABLED,
          PackageManager.DONT_KILL_APP);
}

@Override
protected ReactActivityDelegate createReactActivityDelegate() {
  return new ReactActivityDelegate(this, getMainComponentName()){
    @Nullable
    @Override
    protected Bundle getLaunchOptions() {
      Intent intent = getIntent();
      Bundle bundle = intent.getExtras();

      if(intent.getBooleanExtra("notiRemovable", true))
        AlarmModule.stop(this.getContext());

      return bundle;
    }
  };
}

Create resource directory

Configure your project's resource directory.
This is a necessary process to apply the alarm sound and notification icon.

project/app/src/main/res/raw       // alarm sound
project/app/src/main/res/drawable  // notification icon

Usage

First, import the module to use the alarm function.

import Alarm from "react-native-alarm-manager";

Props

PropTypeDescriptionNote
alarm_idNumberUnique value of an alarm.Auto Increment
alarm_timeStringValue to set the alarm time.HH:mm:00
alarm_titleStringTitle of notification.Nullable
alarm_textStringText of notification.Nullable
alarm_soundStringSound that rings when the alarm is activated.Exclude file extensions
alarm_iconStringIcon of notification.Exclude file extensions
alarm_sound_loopBooleanValue to set whether the alarm sounds repeatedly.Default: true
alarm_vibrationBooleanValue to set whether the alarm will vibrate when it is activated.Default: true
alarm_noti_removableBooleanValue to set whether to end the alarm when an notification is clicked.Default: true
alarm_activateBooleanValue to set whether to activate the alarm.Default: true

Alarm Scheduling

This schedules an alarm. Make sure that alarm_time must be in HH:mm:00 format.

  • Hour(00 ~ 23)
  • Minute(00 ~ 59)
  • Second(00)
const alarm = {
  alarm_time: 12:30:00,   // HH:mm:00
  alarm_title: 'title',
  alarm_text: 'text',
  alarm_sound: 'sound',   // sound.mp3
  alarm_icon: 'icon',     // icon.png
  alarm_sound_loop: true,
  alarm_vibration: true,
  alarm_noti_removable: true,
  alarm_activate: true
};

Alarm.schedule(
  alarm,
  success => console.log(success),  // success message
  fail => console.log(fail)         // fail message
);

Alarm Searching

Alarm searching is provided in two ways.

One

This searches for an alarm.
You can access the alarm_id to obtain alarm information for that ID.

Alarm.search(
  id,
  (success) => console.log(success), // alarm
  (fail) => console.log(fail) // fail message
);

All

This searches for all alarms.

Alarm.searchAll(
  (success) => console.log(success), // alarm list
  (fail) => console.log(fail) // fail message
);

Alarm Modifying

This is almost identical to alarm scheduling, but must be additionally given alarm_id.
Alarm information for that ID will be changed.
If you want an alarm toggle, just change the alarm_activate.

const alarm = {
  alarm_id: 3,
  alarm_time: 15:27:00,   // HH:mm:00
  alarm_title: 'title modify',
  alarm_text: 'text modify',
  alarm_sound: 'sound3',  // sound.mp3
  alarm_icon: 'icon2',    // icon.png
  alarm_sound_loop: false,
  alarm_vibration: true,
  alarm_noti_removable: false,
  alarm_activate: true    // value for alarm toggle
};

Alarm.modify(
  alarm,
  success => console.log(success),  // success message
  fail => console.log(fail)         // fail message
);

Alarm Deleting

This deletes the alarm for that ID.
This also automatically cancels the alarm schedule.

Alarm.delete(
  id,
  (success) => console.log(success), // success message
  (fail) => console.log(fail) // fail message
);

Alarm Stopping

This turns off the alarm.
This means that the sound stops and the notification disappears.

Alarm.stop(
  (success) => console.log(success), // success message
  (fail) => console.log(fail) // fail message
);

Typescript

Typescript declaration types has been added in version 1.2.0, it provides a set of convenient types that can be used while writing a typescript RN app.

import Alarm, { AlarmScheduleType } from "react-native-alarm-manager";

const alarm: AlarmScheduleType = {
  alarm_time: "15:27:00", // HH:mm:00
  alarm_title: "title",
  alarm_text: "text",
  alarm_sound: "sound", // sound.mp3
  alarm_icon: "icon", // icon.png
  alarm_sound_loop: false,
  alarm_vibration: true,
  alarm_noti_removable: false,
  alarm_activate: true, // value for alarm toggle
};

Alarm.schedule(
  alarm,
  (success) => console.log(success),
  (fail) => console.log(fail)
);
import Alarm, { AlarmType } from "react-native-alarm-manager";

const alarm: AlarmType = {
  alarm_id: 3,
  alarm_time: "15:27:00", // HH:mm:ss
  alarm_title: "title modify",
  alarm_text: "text modify",
  alarm_sound: "sound3", // sound3.mp3
  alarm_icon: "icon2", // icon2.png
  alarm_sound_loop: false,
  alarm_vibration: true,
  alarm_noti_removable: false,
  alarm_activate: true, // value for alarm toggle
};

Alarm.modify(
  alarm,
  (success) => console.log(success),
  (fail) => console.log(fail)
);