flutter_notification_listener 1.0.0 flutter_notification_listener: ^1.0.0 copied to clipboard
Flutter Plugin to listen to all incoming notifications for Android.
import 'package:flutter/material.dart';
import 'dart:async';
import 'package:flutter_notification_listener/flutter_notification_listener.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatefulWidget {
@override
_MyAppState createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> {
@override
void initState() {
super.initState();
}
@override
Widget build(BuildContext context) {
return new MaterialApp(
home: NotificationsLog(),
);
}
}
class NotificationsLog extends StatefulWidget {
@override
_NotificationsLogState createState() => _NotificationsLogState();
}
class _NotificationsLogState extends State<NotificationsLog> {
List<NotificationEvent> _log = [];
bool started = false;
@override
void initState() {
super.initState();
initPlatformState();
}
// Platform messages are asynchronous, so we initialize in an async method.
Future<void> initPlatformState() async {
NotificationsListener.initialize();
NotificationsListener.receivePort.listen((evt) => onData(evt));
var isR = await NotificationsListener.isRunning;
print("""Service is ${!isR ? "not " : ""}aleary running""");
setState(() {
started = isR;
});
}
void onData(NotificationEvent event) {
setState(() {
_log.add(event);
});
print(event.toString());
}
void startListening() async {
print("start listening");
var hasPermission = await NotificationsListener.hasPermission;
if (!hasPermission) {
print("no permission, so open settings");
NotificationsListener.openPermissionSettings();
return;
}
var isR = await NotificationsListener.isRunning;
if (!isR) {
await NotificationsListener.startService();
}
setState(() => started = true);
}
void stopListening() async {
print("stop listening");
NotificationsListener.stopService();
setState(() => started = false);
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('Notifications Listener Example'),
),
body: Center(
child: ListView.builder(
itemCount: _log.length,
reverse: true,
itemBuilder: (BuildContext context, int idx) {
final entry = _log[idx];
return ListTile(
trailing: Text(entry.packageName.toString().split('.').last),
title: Container(
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text(entry.title??"<<no title>>"),
Text(entry.timestamp.toString().substring(0, 19)),
],
),
));
})),
floatingActionButton: FloatingActionButton(
onPressed: started ? stopListening : startListening,
tooltip: 'Start/Stop sensing',
child: started ? Icon(Icons.stop) : Icon(Icons.play_arrow),
),
);
}
}