How to enable notifications in Flutter when the app is completely closed (even removed from recent apps)? #148863
Unanswered
ashikbright
asked this question in
Programming Help
Replies: 1 comment
-
Thanks for posting in the GitHub Community, @ashikbright! We're happy you're here. You are more likely to get a useful response if you are posting your question in the applicable category, the Discussions category is solely related to conversations around the GitHub product Discussions. This question should be in the Programming Help category. I've gone ahead and moved it for you. Good luck! |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
Select Topic Area
General
Body
I am developing a Flutter app that needs to send notifications even when the app is completely closed, including when it is removed from the recent apps list. I am using the following setup:
Packages:
flutter_local_notifications for notifications. battery_plus for detecting battery state changes (e.g., charger plugged in or disconnected). Goal:
Trigger a notification when the charger is plugged in or disconnected, even if the app is not running in the background. Current Issue:
Notifications work when the app is running or in the background but stop when the app is killed (removed from recent apps).
Getting notification when app is in recents..when clear not getting notification
main.dart:
import 'package:flutter/material.dart';
import 'package:battery_plus/battery_plus.dart';
import 'package:flutter_local_notifications/flutter_local_notifications.dart';
import 'package:permission_handler/permission_handler.dart';
void main() {
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({Key? key}) : super(key: key);
@OverRide
Widget build(BuildContext context) {
return MaterialApp(
debugShowCheckedModeBanner: false,
title: 'Charger Notification',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: const HomeScreen(),
);
}
}
class HomeScreen extends StatefulWidget {
const HomeScreen({Key? key}) : super(key: key);
@OverRide
State createState() => _HomeScreenState();
}
class _HomeScreenState extends State {
final Battery _battery = Battery();
final FlutterLocalNotificationsPlugin _notificationsPlugin =
FlutterLocalNotificationsPlugin();
BatteryState? _batteryState;
@OverRide
void initState() {
super.initState();
_initializeNotifications();
_listenToBatteryState();
}
Future _initializeNotifications() async {
// Request notification permission
if (await Permission.notification.isDenied) {
await Permission.notification.request();
}
}
void _showNotification(String title, String body) async {
const AndroidNotificationDetails androidNotificationDetails =
AndroidNotificationDetails(
'charger_channel',
'Charger Notifications',
channelDescription: 'Notifications when charger is plugged or unplugged',
importance: Importance.high,
priority: Priority.high,
);
}
void _listenToBatteryState() {
_battery.onBatteryStateChanged.listen((BatteryState state) {
if (_batteryState != state) {
setState(() {
_batteryState = state;
});
}
@OverRide
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text('Charger Notification App'),
),
body: Center(
child: Text(
_batteryState == BatteryState.charging
? 'Device is Charging'
: _batteryState == BatteryState.connectedNotCharging
? 'Device is Not Charging'
: 'Unknown State',
style: const TextStyle(fontSize: 20),
),
),
);
}
}
manifest.xml
<!-- Required to query activities that can process text, see:
https://developer.android.com/training/package-visibility and
https://developer.android.com/reference/android/content/Intent#ACTION_PROCESS_TEXT.
Beta Was this translation helpful? Give feedback.
All reactions