react-native-alarm-notification doesnt work in background mode.
iamabdulhaseeb opened this issue · comments
React native alarm notification works fine but not in background mode :(
If anyone stucked here.
I solved it by adding these lines in android manifest
and then add these lines inside
<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>
@iamabdulhaseeb Do you know any workaround to keep the alarm ringing if the user closes (kills) the app manually
Just To add You can solve this by changing your battery optimization settings on Android.
Settings>--batteryoptimisation >AllApps>{YOUR ALARM APP}>don't optimise
Hi. Can you guys help me. My Android Alarm is running ok. But on IOS it just pop up notification but No alarm.
A complete workaround.
Add these lines in your
/android/app/src/main/AndroidManifest.xml
and
/android/app/src/debug/AndroidManifest.xml
Files
...
<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW"/>
<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
android:name=".MainApplication"
android:label="@string/app_name"
android:icon="@mipmap/ic_launcher"
android:roundIcon="@mipmap/ic_launcher_round"
android:allowBackup="false"
android:theme="@style/AppTheme">
....
<service android:name="com.emekalites.react.alarm.notification.ANService" android:enabled="true"/>
<receiver android:name="com.emekalites.react.alarm.notification.ANAlarmReceiver" android:enabled="true"/>
<receiver android:name="com.emekalites.react.alarm.notification.ANBootReceiver" android:enabled="true" 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"/>
</intent-filter>
</receiver>
...
</application>
....
</manifest>
Now if you're using FCM to trigger your Notification then follow these steps:
import React from 'react';
import ReactNativeAN from 'react-native-alarm-notification';
import {AppRegistry} from 'react-native';
import messaging from '@react-native-firebase/messaging';
messaging().setBackgroundMessageHandler(async remoteMessage => {
const alarmNotifData = {
title: 'My Notification Title',
message: 'My Notification Message',
channel: 'my_channel_id',
small_icon: 'ic_launcher',
};
// trigger the alarm
ReactNativeAN.sendNotification(alarmNotifData);
});
function HeadlessCheck({isHeadless}) {
if (isHeadless) {
// App has been launched in the background by iOS, ignore
return null;
}
return <App />;
}
export default function App() {
return (
// your rest of the project
);
}
AppRegistry.registerComponent('app', () => HeadlessCheck);
This code will work like a charm in every condition (App minimized, closed, phone locked etc).
Now to stop the alarm.. you need to come with your own approach. But here are some useful function.
ReactNativeAN.removeAllFiredNotifications();
ReactNativeAN.stopAlarmSound();
ReactNativeAN.deleteAlarm(alarmId);
Enjoy!! ✨
Hola! So here's the deal, between open source and my day job and life and what not, I have a lot to manage, so I use a GitHub bot to automate a few things here and there. This particular GitHub bot is going to mark this as stale because it has not had recent activity for a while. It will be closed if no further activity occurs in a few days. Do not take this personally--seriously--this is a completely automated action. If this is a mistake, just make a comment, DM me, send a carrier pidgeon, or a smoke signal.
Closed due to inactivity. Holler if this is a mistake, and we'll re-open it.
A complete workaround.
Add these lines in your
/android/app/src/main/AndroidManifest.xml
and/android/app/src/debug/AndroidManifest.xml
Files... <uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW"/> <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 android:name=".MainApplication" android:label="@string/app_name" android:icon="@mipmap/ic_launcher" android:roundIcon="@mipmap/ic_launcher_round" android:allowBackup="false" android:theme="@style/AppTheme"> .... <service android:name="com.emekalites.react.alarm.notification.ANService" android:enabled="true"/> <receiver android:name="com.emekalites.react.alarm.notification.ANAlarmReceiver" android:enabled="true"/> <receiver android:name="com.emekalites.react.alarm.notification.ANBootReceiver" android:enabled="true" 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"/> </intent-filter> </receiver> ... </application> .... </manifest>
Now if you're using FCM to trigger your Notification then follow these steps:
import React from 'react'; import ReactNativeAN from 'react-native-alarm-notification'; import {AppRegistry} from 'react-native'; import messaging from '@react-native-firebase/messaging'; messaging().setBackgroundMessageHandler(async remoteMessage => { const alarmNotifData = { title: 'My Notification Title', message: 'My Notification Message', channel: 'my_channel_id', small_icon: 'ic_launcher', }; // trigger the alarm ReactNativeAN.sendNotification(alarmNotifData); }); function HeadlessCheck({isHeadless}) { if (isHeadless) { // App has been launched in the background by iOS, ignore return null; } return <App />; } export default function App() { return ( // your rest of the project ); } AppRegistry.registerComponent('app', () => HeadlessCheck);
This code will work like a charm in every condition (App minimized, closed, phone locked etc). Now to stop the alarm.. you need to come with your own approach. But here are some useful function.
ReactNativeAN.removeAllFiredNotifications(); ReactNativeAN.stopAlarmSound(); ReactNativeAN.deleteAlarm(alarmId);
Enjoy!! ✨
when i copy paste your code its even stop working when app is open. Can you kindly send me the version you are using