Skip to content

Commit f353beb

Browse files
committed
API documentation
1 parent 176cd7e commit f353beb

File tree

6 files changed

+250
-10
lines changed

6 files changed

+250
-10
lines changed

README.md

Lines changed: 227 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ dependencies:
4242
reflex: <latest version>
4343
```
4444
45-
Run pub get and get packages.
45+
Run `pub get` and get packages.
4646

4747
### Step 2: Add Service in `AndroidManifest.xml`
4848

@@ -61,7 +61,232 @@ Add the following service inside the `application` tag of `AndroidManifest.xml`.
6161
</application>
6262
```
6363

64-
### Step 3
64+
**Android 12+ Compatibility**
65+
Add `android:exported="true"` field in the service tag to make it compatible with Android 12+.
66+
67+
```xml
68+
...
69+
<service
70+
android:label="notifications"
71+
android:name="com.devsonflutter.reflex.notification.NotificationListener"
72+
android:permission="android.permission.BIND_NOTIFICATION_LISTENER_SERVICE"
73+
android:exported="true">
74+
<intent-filter>
75+
<action android:name="android.service.notification.NotificationListenerService" />
76+
</intent-filter>
77+
</service>
78+
</application>
79+
```
80+
81+
### Step 3: Instantiate Reflex
82+
83+
Reflex must only be instantiated only once.
84+
85+
```dart
86+
Reflex reflex = Reflex();
87+
```
88+
89+
90+
## Example
91+
92+
Go to example section in pub.dev to see the full example code.
93+
94+
In GitHub, head over to `example/lib/main.dart` to see the full example code.
95+
96+
### Import
97+
98+
This single import is enough for using reflex.
99+
100+
```dart
101+
import 'package:reflex/reflex.dart';
102+
```
103+
104+
### Listen & Reply
105+
106+
```dart
107+
StreamSubscription<ReflexEvent>? _subscription;
108+
final List<ReflexEvent> _notificationLogs = [];
109+
final List<ReflexEvent> _autoReplyLogs = [];
110+
bool isListening = false;
111+
112+
Reflex reflex = Reflex(
113+
debug: true,
114+
packageNameList: ["com.whatsapp", "com.tyup"],
115+
packageNameExceptionList: ["com.android.systemui"],
116+
autoReply: AutoReply(
117+
packageNameList: ["com.whatsapp"],
118+
message: "[Reflex] This is an automated reply.",
119+
),
120+
);
121+
122+
@override
123+
void initState() {
124+
super.initState();
125+
initPlatformState();
126+
}
127+
128+
Future<void> initPlatformState() async {
129+
startListening();
130+
}
131+
132+
void onData(ReflexEvent event) {
133+
setState(() {
134+
if (event.type == ReflexEventType.notification) {
135+
_notificationLogs.add(event);
136+
} else if (event.type == ReflexEventType.reply) {
137+
_autoReplyLogs.add(event);
138+
}
139+
});
140+
debugPrint(event.toString());
141+
}
142+
143+
void startListening() {
144+
try {
145+
_subscription = reflex.notificationStream!.listen(onData);
146+
setState(() {
147+
isListening = true;
148+
});
149+
} on ReflexException catch (exception) {
150+
debugPrint(exception.toString());
151+
}
152+
}
153+
154+
void stopListening() {
155+
_subscription?.cancel();
156+
setState(() => isListening = false);
157+
}
158+
```
159+
160+
## Quick Guide
161+
162+
A quick guide to **Flutter Reflex plugin**!
163+
164+
### Debugging
165+
166+
Debugging allows you to debug the plugin's functionality, logs are shown in the console.
167+
168+
By default debug logging is enabled. You can configure it using the debug field in the Reflex class.
169+
170+
171+
```dart
172+
Reflex reflex = Reflex(
173+
debug: false,
174+
);
175+
```
176+
177+
### Listen notification permission
178+
179+
See if listening notification permission has been granted or not.
180+
181+
```dart
182+
bool isPermissionGranted = await Reflex.isPermissionGranted;
183+
```
184+
185+
### Request notification listening permission
186+
187+
Use the function to grant notification listening permission.
188+
189+
```dart
190+
await Reflex.requestPermission();
191+
```
192+
193+
### Notification Stream
194+
195+
Use the reflex object to get a notification stream to listen to notifications in your flutter application.
196+
197+
```dart
198+
StreamSubscription<ReflexEvent>? _subscription;
199+
_subscription = reflex.notificationStream!.listen((event) {
200+
// Application Logic
201+
});
202+
```
203+
204+
The stream is subscribed for `ReflexEvent` whenever a notification is received.
205+
206+
#### Reflex Event
207+
208+
The incoming reflex event contains:
209+
210+
* **type**: `ReflexEventType.notification` whenever a notification is received to flutter application, and `ReflexEventType.reply` whenever an automated reply is sent.
211+
212+
* **packageName**: Application's package name from which notifications are received and reply are sent.
213+
214+
* **title**: Notification title
215+
216+
* **message**: Message contained in the notification and while sending reply.
217+
218+
* **timestamp**: Timestamp of the notification received and reply sent.
219+
220+
### Listen notification from specific apps
221+
222+
Specify list of package names to listen to notifications from those applications.
223+
224+
If `packageNameList: null` plugin will listen to notifications from all packages.
225+
226+
```dart
227+
Reflex reflex = Reflex(
228+
debug: true,
229+
packageNameList: ["com.whatsapp", "com.facebook"],
230+
);
231+
```
232+
233+
### Avoid notification from specific apps
234+
235+
Specify package name exception list to avoid listening notifications from those applications.
236+
237+
If `packageNameExceptionList: null`, the plugin will listen to notifications for `packageNameList` if not null.
238+
239+
```dart
240+
Reflex reflex = Reflex(
241+
debug: true,
242+
packageNameExceptionList: ["com.whatsapp"],
243+
);
244+
```
245+
246+
### Auto Reply
247+
248+
Send an automated reply while listening notification.
249+
250+
```dart
251+
AutoReply autoReply = AutoReply(
252+
message: "[Reflex] This is an automated reply.",
253+
),
254+
```
255+
256+
#### Auto reply specific apps
257+
258+
Specify `packageNameList` in AutoReply to reply to specific applications.
259+
260+
```dart
261+
AutoReply autoReply = AutoReply(
262+
packageNameList: ["com.whatsapp"],
263+
message: "[Reflex] This is an automated reply.",
264+
),
265+
```
266+
267+
The `AutoReply` object is used by the Reflex's `autoReply` field to automatically reply to applications.
268+
269+
```dart
270+
Reflex reflex = Reflex(
271+
debug: true,
272+
packageNameList: ["com.whatsapp", "com.tyup"],
273+
packageNameExceptionList: ["com.miui.securitycenter"],
274+
autoReply: AutoReply(
275+
packageNameList: ["com.whatsapp"],
276+
message: "[Reflex] This is an automated reply.",
277+
),
278+
);
279+
```
280+
281+
If the `autoReply` field is `null` in `Reflex` class, Auto reply feature will be disabled.
282+
283+
## Ambiguity
284+
285+
A `ReflexException` will be thrown if,
286+
287+
* `Reflex`'s `packageNameList` and `packageNameExceptionList` contains any similar package name.
288+
289+
* any package name in `AutoReply`'s `packageNameList` is contained in `Reflex`'s `packageNameExceptionList`.
65290

66291
## Project Created & Maintained By
67292

lib/reflex.dart

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ import 'package:reflex/src/platform/reflex_platform.dart';
1717
export 'package:reflex/src/helper/helper.dart';
1818

1919
class Reflex {
20-
/// [Reflex] Constructor
2120
Reflex({
2221
this.debug = true,
2322
this.packageNameList,
@@ -28,10 +27,18 @@ class Reflex {
2827
}
2928

3029
/// [debug] logs on the terminal.
30+
/// debug is set to `true` by default.
3131
final bool debug;
32+
33+
/// [autoReply] if not null, the plugin will send an automated reply to listening apps.
3234
final AutoReply? autoReply;
35+
36+
/// [pacakgeNameList] list of package names to listen for notifications.
3337
final List<String>? packageNameList;
38+
39+
/// [packageNameExceptionList] list of package names to avoid listening for notifications.
3440
final List<String>? packageNameExceptionList;
41+
3542
static late final ReflexPlatform reflexPlatform;
3643

3744
// Initialize [ReflexPlatform] with [debug] and [autoReply]

lib/src/helper/events/reflex_event.dart

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,13 +22,13 @@ class ReflexEvent {
2222
/// Event type Notification/Reply
2323
ReflexEventType? type;
2424

25-
/// The package name of the app that sent the notification.
25+
/// Receiving notification/sending reply package name
2626
String? packageName;
2727

28-
/// Notification Title
28+
/// Notification/AutoReply Title
2929
String? title;
3030

31-
/// Notification Message
31+
/// Notification/AutoReply Message
3232
String? message;
3333

3434
/// The time stamp of the notification.

lib/src/helper/exception/reflex_exception.dart

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ for more details.
88
99
*/
1010

11+
/// Exception thrown by Reflex plugin
1112
class ReflexException implements Exception {
1213
String message;
1314

lib/src/helper/model/auto_reply.dart

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,10 @@ class AutoReply {
1414
required this.message,
1515
});
1616

17-
/// Package Name List for [AutoReply]
17+
/// Package Name List to send automated reply
1818
List<String>? packageNameList;
1919

20-
/// Notification Message
20+
/// Automated message to send for [AutoReply]
2121
String message;
2222

2323
factory AutoReply.fromMap(Map<dynamic, dynamic> map) {

lib/src/helper/utils/reflex_utils.dart

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,12 @@
1-
import 'package:reflex/src/helper/exception/reflex_exception.dart';
1+
/*
2+
3+
Copyright (c) 2022 DevsOnFlutter (Devs On Flutter)
4+
All rights reserved.
5+
6+
The plugin is governed by the BSD-3-clause License. Please see the LICENSE file
7+
for more details.
8+
9+
*/
210

311
class ReflexUtils {
412
ReflexUtils._();
@@ -19,7 +27,6 @@ class ReflexUtils {
1927
for (String packageName in list2) {
2028
if (!list1.contains(packageName)) {
2129
return packageName;
22-
2330
}
2431
}
2532
}

0 commit comments

Comments
 (0)