@@ -2,33 +2,34 @@ import 'package:flutter/material.dart';
22import 'src/link_handler.dart' ;
33import '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
2121class 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