I'm using this package notification_listener_service on flutter for reading the notifications on android device.
It works in debug, but when I make the release with flutter build apk --release it doesn't listen to the notifications, neither if I build it with flutter build apk --debug
I gave permission in the AndroidManifest:
<service
android:label="notifications"
android:name="notification.listener.service.NotificationListener"
android:permission="android.permission.BIND_NOTIFICATION_LISTENER_SERVICE"
android:exported="true">
<intent-filter>
<action android:name="android.service.notification.NotificationListenerService" />
</intent-filter>
</service>
And the code is this:
import 'dart:math';
import 'package:flutter/material.dart';
import 'package:notification_listener_service/notification_event.dart';
import 'package:notification_listener_service/notification_listener_service.dart';
void main() {
runApp(MaterialApp(
home: HomePage(),
debugShowCheckedModeBanner: false,
debugShowMaterialGrid: false,
));
}
class HomePage extends StatefulWidget {
const HomePage({super.key});
@override
State<HomePage> createState() => _HomePageState();
}
class _HomePageState extends State<HomePage> {
bool havePermission = false;
String errorMessage = "";
final List<ServiceNotificationEvent> events = [];
@override
void initState() {
super.initState();
print("INIT STATE");
checkPermission();
}
Future<void> checkPermission() async {
/// check if notification permission is enabled
bool status = await NotificationListenerService.isPermissionGranted();
/// if permission listen notifications
if (status) listen();
havePermission = status;
setState(() {});
}
Future<void> listen() async {
try {
print("LISTEN");
NotificationListenerService.notificationsStream.listen((event) {
print("NEW EVENT: ");
print(event);
events.add(event);
setState(() {});
});
} catch (e) {
await Future.delayed(Duration(milliseconds: 100));
errorMessage = e.toString();
setState(() {});
}
}
Future<void> sendReply(ServiceNotificationEvent event) async {
await Future.delayed(Duration(seconds: 2));
print(event.packageName?.contains("telegram"));
if (event.packageName?.contains("telegram") ?? false) {
final msg = ["ciaooo", "Come va?", "Sono una AI replier"];
event.sendReply(msg[Random().nextInt(3)]);
}
}
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(
title: Text('Escort Reply Messages'),
),
body: SingleChildScrollView(
padding: EdgeInsets.all(32),
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
crossAxisAlignment: CrossAxisAlignment.center,
children: [
Text("Permission added: ${havePermission ? "Yes" : "No"}"),
SizedBox(height: 32),
Text("Error: ${errorMessage.isNotEmpty}"),
Text(
errorMessage,
style: TextStyle(color: Colors.red),
),
SizedBox(height: 32),
Text("Your notification"),
ListView.separated(
itemCount: events.length,
padding: EdgeInsets.symmetric(vertical: 32),
shrinkWrap: true,
separatorBuilder: (_, i) => Divider(color: Colors.black),
itemBuilder: (_, i) => Row(
children: [
Expanded(
child: Text(
"packageName: ${events[i].packageName}\ntitle: ${events[i].title}\ncontent: ${events[i].content}\ncanReply: ${events[i].canReply}",
),
),
],
),
),
],
),
),
),
);
}
}
When I'm running it in debug it works, I gave permission to read notifications on my android device, the listener start, when I receive a notification is successfully added to the list of notifications and showed in the list.
But in release the listener doesn't start, and I don't have any error, every time I receive a notification it doesn't added to the list of notifications.
Somebody can help me?