Skip to content

Commit 73ff392

Browse files
authored
Extract registration logic and improve API naming (#13)
Extract shared registration logic and rename methods for clarity
1 parent 3781498 commit 73ff392

File tree

3 files changed

+71
-71
lines changed

3 files changed

+71
-71
lines changed

README.md

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -153,38 +153,38 @@ MaterialApp(
153153
)
154154
```
155155

156-
### 3. Advanced: Delayed Permission Flow
156+
### 3. Advanced: Delayed Registration Flow
157157

158158
For apps that need to ask for permission at a specific time (e.g., after user onboarding):
159159

160160
```dart
161161
void main() async {
162162
WidgetsFlutterBinding.ensureInitialized();
163163
164-
// Initialize without requesting permission
164+
// Initialize without registering device
165165
await PntaFlutter.initialize(
166166
'prj_XXXXXXXXX',
167-
requestPermission: false, // Skip permission request
167+
registerDevice: false, // Skip device registration
168168
);
169169
170170
runApp(MyApp());
171171
}
172172
173-
// Later in your app, when ready to ask for permission:
173+
// Later in your app, when ready to register:
174174
Future<void> setupNotifications() async {
175-
await PntaFlutter.requestPermission(
175+
await PntaFlutter.registerDevice(
176176
metadata: {
177177
'user_id': '123',
178178
'user_email': 'user@example.com',
179179
},
180180
);
181181
182182
// Optional: Get the device token if you need it for your backend
183-
// final deviceToken = await PntaFlutter.requestPermission(...);
183+
// final deviceToken = await PntaFlutter.registerDevice(...);
184184
// if (deviceToken != null) {
185-
// print('Permission granted and device registered!');
185+
// print('Device registered successfully!');
186186
// } else {
187-
// print('Permission denied or registration failed');
187+
// print('Registration failed');
188188
// }
189189
}
190190
```
@@ -229,25 +229,25 @@ PntaFlutter.onNotificationTap.listen((payload) {
229229

230230
### Core Methods
231231

232-
#### `PntaFlutter.initialize(String projectId, {Map<String, dynamic>? metadata, bool requestPermission, bool autoHandleLinks, bool showSystemUI})`
232+
#### `PntaFlutter.initialize(String projectId, {Map<String, dynamic>? metadata, bool registerDevice, bool autoHandleLinks, bool showSystemUI})`
233233

234234
Main initialization method that handles everything for most apps:
235235

236236
- `projectId`: Your PNTA project ID (format: `prj_XXXXXXXXX`) from [app.pnta.io](https://app.pnta.io)
237237
- `metadata`: Optional device metadata to include during registration
238-
- `requestPermission`: Whether to request permission and register device immediately (default: `true`)
238+
- `registerDevice`: Whether to register device immediately (default: `true`)
239239
- `autoHandleLinks`: Automatically handle `link_to` URLs when notifications are tapped (default: `true`)
240240
- `showSystemUI`: Show system notification banner/sound when app is in foreground (default: `false`)
241241

242-
Returns `Future<String?>` - the device token if permission was granted and device registered, null otherwise.
242+
Returns `Future<String?>` - the device token if device was registered, null otherwise.
243243

244-
#### `PntaFlutter.requestPermission({Map<String, dynamic>? metadata})`
244+
#### `PntaFlutter.registerDevice({Map<String, dynamic>? metadata})`
245245

246-
For delayed permission scenarios. Must be called after `initialize()` with `requestPermission: false`.
246+
For delayed registration scenarios. Requests notification permission and registers device. Must be called after `initialize()` with `registerDevice: false`.
247247

248-
- `metadata`: Optional device metadata to include during registration (merged with metadata from initialize)
248+
- `metadata`: Optional device metadata to include during registration
249249

250-
Returns `Future<String?>` - the device token if permission was granted and device registered, null otherwise. **Note: You can ignore the return value if you don't need the token.**
250+
Returns `Future<String?>` - the device token if permission was granted and device registered successfully, null otherwise. **Note: You can ignore the return value if you don't need the token.**
251251

252252
#### `PntaFlutter.updateMetadata(Map<String, dynamic> metadata)`
253253

example/integration_test/plugin_integration_test.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ void main() {
2020
try {
2121
await PntaFlutter.initialize(
2222
'prj_test123',
23-
requestPermission: false, // Don't request permission in test
23+
registerDevice: false, // Don't register device in test
2424
metadata: {'test': 'true'},
2525
);
2626
// If we get here without exception, initialization succeeded

lib/pnta_flutter.dart

Lines changed: 55 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -2,33 +2,34 @@ import 'package:flutter/material.dart';
22
import 'src/link_handler.dart';
33
import 'pnta_flutter_platform_interface.dart';
44

5-
class PntaFlutterConfig {
5+
class _PntaFlutterConfig {
66
final String projectId;
77
final bool autoHandleLinks;
88
final bool showSystemUI;
9-
final bool requestPermission;
10-
final Map<String, dynamic>? metadata;
9+
final bool registerDevice;
10+
Map<String, dynamic>? metadata;
1111

12-
PntaFlutterConfig({
12+
_PntaFlutterConfig({
1313
required this.projectId,
1414
required this.autoHandleLinks,
1515
required this.showSystemUI,
16-
required this.requestPermission,
16+
required this.registerDevice,
1717
this.metadata,
1818
});
1919
}
2020

2121
class PntaFlutter {
22-
static PntaFlutterConfig? _config;
22+
static _PntaFlutterConfig? _config;
2323
static String? _deviceToken;
2424
static final GlobalKey<NavigatorState> navigatorKey =
2525
GlobalKey<NavigatorState>();
2626

27+
// Setup
2728
/// Main initialization - handles everything for most apps
2829
static Future<String?> initialize(
2930
String projectId, {
3031
Map<String, dynamic>? metadata,
31-
bool requestPermission = true,
32+
bool registerDevice = true,
3233
bool autoHandleLinks = true,
3334
bool showSystemUI = false,
3435
}) async {
@@ -42,35 +43,20 @@ class PntaFlutter {
4243
debugPrint('PNTA: Invalid project ID. Must start with "prj_".');
4344
return null;
4445
}
45-
_config = PntaFlutterConfig(
46+
_config = _PntaFlutterConfig(
4647
projectId: projectId,
4748
autoHandleLinks: autoHandleLinks,
4849
showSystemUI: showSystemUI,
49-
requestPermission: requestPermission,
50+
registerDevice: registerDevice,
5051
metadata: metadata,
5152
);
5253
LinkHandler.initialize(autoHandleLinks: autoHandleLinks);
5354
await PntaFlutterPlatform.instance
5455
.setForegroundPresentationOptions(showSystemUI: showSystemUI);
55-
if (requestPermission) {
56-
final granted =
57-
await PntaFlutterPlatform.instance.requestNotificationPermission();
58-
if (!granted) {
59-
debugPrint('PNTA: Notification permission denied.');
60-
return null;
61-
}
62-
// Pure device registration with SDK version
63-
_deviceToken = await PntaFlutterPlatform.instance.identify(projectId);
64-
65-
// Separately update metadata if provided
66-
if (metadata != null && metadata.isNotEmpty) {
67-
await PntaFlutterPlatform.instance
68-
.updateMetadata(projectId, metadata);
69-
}
70-
71-
return _deviceToken;
56+
if (registerDevice) {
57+
return await _performRegistration(metadata: metadata);
7258
} else {
73-
// Delayed permission scenario
59+
// Delayed registration scenario
7460
return null;
7561
}
7662
} catch (e, st) {
@@ -79,37 +65,18 @@ class PntaFlutter {
7965
}
8066
}
8167

82-
/// For delayed permission scenarios: requests permission, gets token, and registers device
83-
static Future<String?> requestPermission(
68+
// Registration
69+
/// For delayed registration scenarios
70+
static Future<String?> registerDevice(
8471
{Map<String, dynamic>? metadata}) async {
8572
if (_config == null) {
86-
debugPrint('PNTA: Must call initialize() before requesting permission.');
87-
return null;
88-
}
89-
try {
90-
final granted =
91-
await PntaFlutterPlatform.instance.requestNotificationPermission();
92-
if (!granted) {
93-
debugPrint('PNTA: Notification permission denied.');
94-
return null;
95-
}
96-
// Pure device registration (SDK version is sent internally by identify)
97-
_deviceToken =
98-
await PntaFlutterPlatform.instance.identify(_config!.projectId);
99-
100-
// Update metadata if provided
101-
if (metadata != null && metadata.isNotEmpty) {
102-
await PntaFlutterPlatform.instance
103-
.updateMetadata(_config!.projectId, metadata);
104-
}
105-
return _deviceToken;
106-
} catch (e, st) {
107-
debugPrint('PNTA: requestPermission error: $e\n$st');
73+
debugPrint('PNTA: Must call initialize() before registering device.');
10874
return null;
10975
}
76+
return await _performRegistration(metadata: metadata);
11077
}
11178

112-
/// Non-critical metadata updates
79+
/// Update device metadata
11380
static Future<void> updateMetadata(Map<String, dynamic> metadata) async {
11481
if (_config == null) {
11582
debugPrint('PNTA: Must call initialize() before updating metadata.');
@@ -118,15 +85,18 @@ class PntaFlutter {
11885
try {
11986
await PntaFlutterPlatform.instance
12087
.updateMetadata(_config!.projectId, metadata);
88+
_config!.metadata = metadata;
12189
} catch (e, st) {
12290
debugPrint('PNTA: updateMetadata error: $e\n$st');
12391
}
12492
}
12593

126-
/// Notification streams
94+
// Notifications
95+
/// Stream of notifications received while app is in foreground
12796
static Stream<Map<String, dynamic>> get foregroundNotifications =>
12897
PntaFlutterPlatform.instance.foregroundNotifications;
12998

99+
/// Stream of notification taps
130100
static Stream<Map<String, dynamic>> get onNotificationTap =>
131101
PntaFlutterPlatform.instance.onNotificationTap.asyncMap((payload) async {
132102
if (_config?.autoHandleLinks == true) {
@@ -135,15 +105,45 @@ class PntaFlutter {
135105
return payload;
136106
});
137107

138-
/// Manual link handling
108+
// Utilities
109+
/// Manually handle a deep link
139110
static Future<bool> handleLink(String? link) async {
140111
return await LinkHandler.handleLink(link);
141112
}
142113

143-
/// Configuration access
114+
// Getters
144115
static String? get projectId => _config?.projectId;
145116
static bool get autoHandleLinks => _config?.autoHandleLinks ?? false;
146117
static bool get showSystemUI => _config?.showSystemUI ?? false;
147118
static Map<String, dynamic>? get currentMetadata => _config?.metadata;
148119
static String? get deviceToken => _deviceToken;
120+
121+
// Private
122+
static Future<String?> _performRegistration(
123+
{Map<String, dynamic>? metadata}) async {
124+
try {
125+
final granted =
126+
await PntaFlutterPlatform.instance.requestNotificationPermission();
127+
if (!granted) {
128+
debugPrint('PNTA: Notification permission denied.');
129+
return null;
130+
}
131+
132+
// Pure device registration with SDK version
133+
_deviceToken =
134+
await PntaFlutterPlatform.instance.identify(_config!.projectId);
135+
136+
// Update metadata if provided
137+
if (metadata != null && metadata.isNotEmpty) {
138+
await PntaFlutterPlatform.instance
139+
.updateMetadata(_config!.projectId, metadata);
140+
_config!.metadata = metadata;
141+
}
142+
143+
return _deviceToken;
144+
} catch (e, st) {
145+
debugPrint('PNTA: Registration error: $e\n$st');
146+
return null;
147+
}
148+
}
149149
}

0 commit comments

Comments
 (0)