Flutter通知监听插件notification_listener_service的使用
Flutter通知监听插件notification_listener_service的使用
notification_listener_service
是一个Flutter插件,用于与Android的通知服务进行交互。它允许开发者接收系统发送的新通知或移除通知的调用。
安装和使用
添加依赖
在您的 pubspec.yaml
文件中添加以下依赖项:
dependencies:
notification_listener_service: any # 或者是Pub上的最新版本
修改AndroidManifest.xml
在 AndroidManifest.xml
中添加以下代码以绑定通知服务到您的应用程序:
<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>
使用方法
检查权限
检查是否已授予通知权限:
final bool status = await NotificationListenerService.isPermissionGranted();
请求权限
请求通知权限(将打开通知设置页面,并在权限授予后返回 true
):
final bool status = await NotificationListenerService.requestPermission();
监听通知事件
监听传入的通知事件:
NotificationListenerService.notificationsStream.listen((event) {
log("Current notification: $event");
});
通知事件属性
ServiceNotificationEvent
提供了以下属性:
int? id
: 通知IDbool? canReply
: 是否可以回复该通知bool? haveExtraPicture
: 通知是否有额外图片bool? hasRemoved
: 通知是否已被移除Uint8List? extrasPicture
: 通知额外图片数据,使用Image.memory
显示图片Uint8List? largeIcon
: 通知大图标,使用Image.memory
显示图片String? packageName
: 通知包名String? title
: 通知标题Uint8List? appIcon
: 通知应用图标,使用Image.memory
显示图片String? content
: 通知内容Future<bool> sendReply(String message)
: 发送直接消息回复给传入的通知
回复通知
回复通知示例:
try {
await event.sendReply("This is an auto response");
} catch (e) {
log(e.toString());
}
示例应用
下面是一个完整的示例应用,展示了如何在前台使用 notification_listener_service
插件:
import 'dart:async';
import 'dart:developer';
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(const MyApp());
}
class MyApp extends StatefulWidget {
const MyApp({Key? key}) : super(key: key);
@override
State<MyApp> createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> {
StreamSubscription<ServiceNotificationEvent>? _subscription;
List<ServiceNotificationEvent> events = [];
@override
void initState() {
super.initState();
}
@override
Widget build(BuildContext context) {
return MaterialApp(
debugShowCheckedModeBanner: false,
home: Scaffold(
appBar: AppBar(
title: const Text('Plugin example app'),
),
body: Center(
child: Column(
children: [
SingleChildScrollView(
scrollDirection: Axis.horizontal,
child: Row(
crossAxisAlignment: CrossAxisAlignment.center,
children: [
TextButton(
onPressed: () async {
final res = await NotificationListenerService.requestPermission();
log("Is enabled: $res");
},
child: const Text("Request Permission"),
),
const SizedBox(height: 20.0),
TextButton(
onPressed: () async {
final bool res = await NotificationListenerService.isPermissionGranted();
log("Is enabled: $res");
},
child: const Text("Check Permission"),
),
const SizedBox(height: 20.0),
TextButton(
onPressed: () {
_subscription = NotificationListenerService.notificationsStream.listen((event) {
log("$event");
setState(() {
events.add(event);
});
});
},
child: const Text("Start Stream"),
),
const SizedBox(height: 20.0),
TextButton(
onPressed: () {
_subscription?.cancel();
},
child: const Text("Stop Stream"),
),
],
),
),
Expanded(
child: ListView.builder(
shrinkWrap: true,
itemCount: events.length,
itemBuilder: (_, index) => Padding(
padding: const EdgeInsets.only(bottom: 8.0),
child: ListTile(
onTap: () async {
try {
await events[index].sendReply("This is an auto response");
} catch (e) {
log(e.toString());
}
},
trailing: events[index].hasRemoved!
? const Text(
"Removed",
style: TextStyle(color: Colors.red),
)
: const SizedBox.shrink(),
leading: events[index].appIcon == null
? const SizedBox.shrink()
: Image.memory(
events[index].appIcon!,
width: 35.0,
height: 35.0,
),
title: Text(events[index].title ?? "No title"),
subtitle: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text(
events[index].content ?? "no content",
style: const TextStyle(fontWeight: FontWeight.bold),
),
const SizedBox(height: 8.0),
events[index].canReply!
? const Text(
"Replied with: This is an auto reply",
style: TextStyle(color: Colors.purple),
)
: const SizedBox.shrink(),
events[index].largeIcon != null
? Image.memory(
events[index].largeIcon!,
)
: const SizedBox.shrink(),
],
),
isThreeLine: true,
),
),
),
)
],
),
),
),
);
}
}
这个示例应用展示了如何请求和检查通知权限、启动和停止通知事件流,以及处理接收到的通知事件。希望这些信息对您有所帮助!如果您有任何问题,请随时提问。
更多关于Flutter通知监听插件notification_listener_service的使用的实战系列教程也可以访问 https://www.itying.com/category-92-b0.html
更多关于Flutter通知监听插件notification_listener_service的使用的实战系列教程也可以访问 https://www.itying.com/category-92-b0.html
当然,以下是如何在Flutter应用中使用notification_listener_service
插件来监听系统通知的一个代码示例。这个插件允许你的Flutter应用监听来自Android系统的通知。
首先,确保你已经在pubspec.yaml
文件中添加了notification_listener_service
依赖:
dependencies:
flutter:
sdk: flutter
notification_listener_service: ^x.y.z # 请替换为最新版本号
然后,运行flutter pub get
来获取依赖。
配置AndroidManifest.xml
在android/app/src/main/AndroidManifest.xml
文件中,你需要添加服务声明以允许应用监听通知:
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.yourapp">
<application
...>
<!-- 添加这个服务声明 -->
<service
android:name=".NotificationListenerServiceImplementation"
android:permission="android.permission.BIND_NOTIFICATION_LISTENER_SERVICE">
<intent-filter>
<action android:name="android.service.notification.NotificationListenerService" />
</intent-filter>
</service>
</application>
</manifest>
创建NotificationListenerService实现类
在android/app/src/main/kotlin/com/example/yourapp/
(或者对应的Java目录)下创建一个新的Kotlin/Java类,例如NotificationListenerServiceImplementation.kt
:
package com.example.yourapp
import android.service.notification.NotificationListenerService
import android.service.notification.StatusBarNotification
import io.flutter.embedding.engine.FlutterEngine
import io.flutter.plugin.common.MethodChannel
class NotificationListenerServiceImplementation : NotificationListenerService() {
companion object {
private const val CHANNEL = "com.example.yourapp/notification_listener"
}
private lateinit var flutterEngine: FlutterEngine
private lateinit var methodChannel: MethodChannel
override fun onCreate() {
super.onCreate()
// 假设你有一个方法来获取当前的FlutterEngine,这里你需要根据实际情况实现
flutterEngine = FlutterEngine(this)
methodChannel = MethodChannel(flutterEngine.dartExecutor.binaryMessenger, CHANNEL)
methodChannel.setMethodCallHandler { call, result ->
// 这里可以处理来自Dart端的调用,如果有需要的话
}
}
override fun onDestroy() {
super.onDestroy()
flutterEngine.destroy()
}
override fun onNotificationPosted(sbn: StatusBarNotification) {
// 当有新通知到来时,通过MethodChannel发送通知数据到Dart端
val notificationData = mapOf(
"packageName" to sbn.packageName,
"title" to sbn.notification.extras.getString("android.title"),
"text" to sbn.notification.extras.getString("android.text")
)
methodChannel.invokeMethod("onNotificationPosted", notificationData)
}
override fun onNotificationRemoved(sbn: StatusBarNotification) {
// 当通知被移除时,同样可以通过MethodChannel发送通知到Dart端(可选)
val notificationData = mapOf(
"packageName" to sbn.packageName,
"key" to sbn.key
)
methodChannel.invokeMethod("onNotificationRemoved", notificationData)
}
}
在Flutter端接收通知
在你的Flutter应用中,你需要设置一个MethodChannel
来接收来自NotificationListenerService
的通知。
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatefulWidget {
@override
_MyAppState createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> {
static const platform = MethodChannel('com.example.yourapp/notification_listener');
@override
void initState() {
super.initState();
_listenForNotifications();
}
void _listenForNotifications() {
platform.setMethodCallHandler((call) async {
if (call.method == "onNotificationPosted") {
final Map<String, dynamic> arguments = call.arguments;
print("Notification posted:");
print("Package: ${arguments['packageName']}");
print("Title: ${arguments['title']}");
print("Text: ${arguments['text']}");
} else if (call.method == "onNotificationRemoved") {
final Map<String, dynamic> arguments = call.arguments;
print("Notification removed:");
print("Package: ${arguments['packageName']}");
print("Key: ${arguments['key']}");
}
});
}
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(
title: Text('Notification Listener Demo'),
),
body: Center(
child: Text('Listening for notifications...'),
),
),
);
}
}
这个代码示例展示了如何在Flutter应用中设置notification_listener_service
插件来监听Android系统的通知,并在Flutter端处理这些通知。请根据你的实际需求调整代码,特别是如何获取和保持FlutterEngine
的引用部分。