emekalites / react-native-alarm-notification

schedule alarm and local notification in react-native
https://www.npmjs.com/package/react-native-alarm-notification
MIT License
223 stars 94 forks source link

Alarms won't fire [react-native 0.68] [jdk 11] [gradle 7] #180

Closed Amitb01FUB closed 1 year ago

Amitb01FUB commented 2 years ago

First of all, the app wouldn't even build after installing this package. I'm using gradle 7 and this package uses deprecated maven dependencies. To fix that I did some changes to the build.gradle that you can see in my comment here #175 ,but that's not the issue.

My problem is that when I create new alarms they never actually fire. When I use the scheduleAlarm fucntion I get an id back, and that id exists if I check getScheduledAlarms(), but when the time comes it just never fires.

I've disabled 'do not disturb', maxed my volume, still noting.

my code:

App.js

import ReactNativeAN from 'react-native-alarm-notification';

import { NativeEventEmitter, NativeModules } from 'react-native';

const { RNAlarmNotification } = NativeModules;
const RNAlarmEmitter = new NativeEventEmitter(RNAlarmNotification);

const dismissSubscription = RNAlarmEmitter.addListener(
    'OnNotificationDismissed', (data) => console.log(JSON.parse(e))
);

const openedSubscription = RNAlarmEmitter.addListener(
    'OnNotificationOpened', (data) => console.log(JSON.parse(e))
);

...

const launchAlarm = async () =>{

  const fireDate = ReactNativeAN.parseDate(new Date(Date.now() + 5000));     // set the fire date for 1 second from now

  const alarmNotifData = {
    title: 'My Notification Title',
    message: 'My Notification Message',
    channel: '0',
    small_icon: 'ic_launcher',
    autoCancel: true,
    bypassDnd: true,
    loopSound: true,
    volume:0.5,

    // You can add any additional data that is important for the notification
    // It will be added to the PendingIntent along with the rest of the bundle.
    // e.g.
      data: { foo: 'bar' },
  };
    const alarms = await ReactNativeAN.getScheduledAlarms();
    console.log(alarms);
    for (let i = 0; i < alarms.length; i++){
      await ReactNativeAN.deleteAlarm(alarms[i]?.id);
    }

    //Schedule Future Alarm
    try {
      const alarm = await ReactNativeAN.scheduleAlarm({ ...alarmNotifData, fire_date: fireDate });
      console.log(alarm); // { id: 1 }
    } catch (e){
      console.log('failed to create alarm- ',e);
    }
}

class App extends Component {
...

    render() {
        return (
    ...

      <Button
          onPress={() => {
            console.log('test');
            launchAlarm();
          }}
          title="TEST BUTTON"
          color="#DAD8D6"
          accessibilityLabel="TEST BUTTON"
        />
      ...
      );
    }
}

MainActivity.java

...
import android.content.Intent;
import android.os.Bundle;

import com.emekalites.react.alarm.notification.BundleJSONConverter;
import com.facebook.react.modules.core.DeviceEventManagerModule;
import org.json.JSONObject;
...

public class MainActivity extends ReactActivity {

    @Override
      public void onNewIntent(Intent intent) {
          super.onNewIntent(intent);
          try {
              Bundle bundle = intent.getExtras();
              if (bundle != null) {
                  JSONObject data = BundleJSONConverter.convertToJSON(bundle);
                  getReactInstanceManager().getCurrentReactContext().getJSModule(DeviceEventManagerModule.RCTDeviceEventEmitter.class).emit("OnNotificationOpened", data.toString());
              }
          } catch (Exception e) {
              System.err.println("Exception when handling notification opened. " + e);
          }
      }
...
}

Would be glad for some help

Amitb01FUB commented 1 year ago

Solved: As said in the Readme's "manual installation" section, I had to add this block to my AndroidManifest.xml:

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

    <application ....>
        <receiver
            android:name="com.emekalites.react.alarm.notification.AlarmReceiver"
            android:enabled="true"
            android:exported="true">
            <intent-filter>
                <action android:name="ACTION_DISMISS" />
                <action android:name="ACTION_SNOOZE" />
            </intent-filter>
        </receiver>

        <receiver
            android:name="com.emekalites.react.alarm.notification.AlarmDismissReceiver"
            android:enabled="true"
            android:exported="true" />

        <receiver
            android:name="com.emekalites.react.alarm.notification.AlarmBootReceiver"
            android:directBootAware="true"
            android:enabled="false"
            android:exported="true">
            <intent-filter>
                <action android:name="android.intent.action.BOOT_COMPLETED" />
                <action android:name="android.intent.action.QUICKBOOT_POWERON" />
                <action android:name="com.htc.intent.action.QUICKBOOT_POWERON" />
                <action android:name="android.intent.action.LOCKED_BOOT_COMPLETED" />
            </intent-filter>
        </receiver>

Unlike what the Readme said, my app didn't crash, and I didn't manually install the package so I don't know why this wasn't there already