Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
Amitb01FUB opened this issue Sep 12, 2022 · 1 comment
Closed

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

Amitb01FUB opened this issue Sep 12, 2022 · 1 comment

Comments

@Amitb01FUB
Copy link

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
Copy link
Author

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant