Fix registration lifecycle and simplify API#14
Conversation
- Add checkNotificationPermission method to check permissions without prompting - Initialize now always registers device when permissions are already granted - Simplify registerDevice API to remove metadata parameter - Ensure consistent device registration across app restarts
- Remove metadata parameter from registerDevice - uses initialize metadata - Update README examples to show proper usage pattern - Simplify permission checking logic with ternary operator
- Change initialize() and registerDevice() to return Future<void> - Use PntaFlutter.deviceToken getter to access token when needed - Update all examples and documentation to use getter pattern - Align with existing getter pattern for projectId and currentMetadata
📝 WalkthroughSummary by CodeRabbit
Summary by CodeRabbit
WalkthroughThis update changes the return types of the Changes
Sequence Diagram(s)sequenceDiagram
participant App
participant PntaFlutter (Dart)
participant PlatformChannel
participant Native (Android/iOS)
App->>PntaFlutter (Dart): initialize(projectId, ...)
PntaFlutter (Dart)->>PlatformChannel: checkNotificationPermission()
PlatformChannel->>Native (Android/iOS): checkNotificationPermission
Native (Android/iOS)-->>PlatformChannel: permissionGranted (bool)
PlatformChannel-->>PntaFlutter (Dart): permissionGranted (bool)
alt permissionGranted and registerDevice
PntaFlutter (Dart)->>PlatformChannel: registerDevice
PlatformChannel->>Native (Android/iOS): registerDevice
Native (Android/iOS)-->>PlatformChannel: (no return value)
PlatformChannel-->>PntaFlutter (Dart): (void)
else permission not granted and registerDevice
PntaFlutter (Dart)->>PlatformChannel: requestNotificationPermission
PlatformChannel->>Native (Android/iOS): requestNotificationPermission
Native (Android/iOS)-->>PlatformChannel: permissionGranted (bool)
PlatformChannel-->>PntaFlutter (Dart): permissionGranted (bool)
alt permissionGranted
PntaFlutter (Dart)->>PlatformChannel: registerDevice
PlatformChannel->>Native (Android/iOS): registerDevice
Native (Android/iOS)-->>PlatformChannel: (void)
PlatformChannel-->>PntaFlutter (Dart): (void)
else permission not granted
PntaFlutter (Dart)-->>App: (void)
end
end
PntaFlutter (Dart)-->>App: (void)
App->>PntaFlutter (Dart): access deviceToken (static getter)
Possibly related PRs
📜 Recent review detailsConfiguration used: CodeRabbit UI 📒 Files selected for processing (1)
🚧 Files skipped from review as they are similar to previous changes (1)
✨ Finishing Touches
🪧 TipsChatThere are 3 ways to chat with CodeRabbit:
SupportNeed help? Create a ticket on our support page for assistance with any issues or questions. Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments. CodeRabbit Commands (Invoked using PR comments)
Other keywords and placeholders
CodeRabbit Configuration File (
|
There was a problem hiding this comment.
Actionable comments posted: 3
🧹 Nitpick comments (5)
lib/pnta_flutter_platform_interface.dart (1)
31-34: Document the new API surface
checkNotificationPermission()is public surface; add a short Dart-doc that clarifies how it differs fromrequestNotificationPermission()(no prompt, immediate status). This avoids ambiguity for plugin integrators and keeps the interface self-describing.- Future<bool> checkNotificationPermission() { + /// Returns `true` if the user has already granted notification permission + /// without displaying any system prompt. + /// + /// Use this when you merely need the current status and do **not** want to + /// disturb the user with a permission dialog. + Future<bool> checkNotificationPermission() {lib/pnta_flutter_method_channel.dart (1)
28-34: Graceful handling of unimplemented platformsThe method currently defaults to
falsewhen the platform doesn’t implement'checkNotificationPermission'. This is fine, but consider surfacing a warning (e.g.debugPrint) so integrators know the feature is missing on their target platform rather than silently assuming “denied”.- final result = - await methodChannel.invokeMethod<bool>('checkNotificationPermission'); - return result ?? false; + final result = + await methodChannel.invokeMethod<bool>('checkNotificationPermission'); + if (result == null) { + debugPrint('[PNTA] checkNotificationPermission not implemented on this platform.'); + } + return result ?? false;android/src/main/kotlin/io/pnta/pnta_flutter/PermissionHandler.kt (1)
32-44: Drop hardActivityrequirement for a read-only permission queryFor a simple permission check you only need a
Context; insisting on a non-nullActivityforces callers to be in the foreground and breaks use-cases such as background initialisation.-fun checkNotificationPermission(activity: Activity?, result: Result) { +fun checkNotificationPermission(context: Context, result: Result) { if (Build.VERSION.SDK_INT >= 33) { - if (activity == null) { - result.error("NO_ACTIVITY", "Activity is null", null) - return - } - val granted = ContextCompat.checkSelfPermission(activity, + val granted = ContextCompat.checkSelfPermission(context, android.Manifest.permission.POST_NOTIFICATIONS) == PackageManager.PERMISSION_GRANTED result.success(granted)Overload a second function (or make
Activitynullable) for the existing request-permission path to keep API compatibility.README.md (1)
350-356: Out-of-date example uses removedrequestPermissionAPI
requestPermission()no longer exists. Replace the commented snippet with the new
await PntaFlutter.registerDevice();call (or remove it entirely) to avoid confusing readers.lib/pnta_flutter.dart (1)
131-137: Minor: avoid duplicate permission queries inside_performRegistrationWhen
skipPromptistrue,_performRegistrationcallscheckNotificationPermission()a second time even though the caller already confirmed it. Pass the result in as a parameter or guard the extra call to save an unnecessary platform hop.
📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro
📒 Files selected for processing (9)
README.md(4 hunks)android/src/main/kotlin/io/pnta/pnta_flutter/PermissionHandler.kt(1 hunks)android/src/main/kotlin/io/pnta/pnta_flutter/PntaFlutterPlugin.kt(1 hunks)example/lib/main.dart(2 hunks)ios/Classes/PermissionHandler.swift(1 hunks)ios/Classes/PntaFlutterPlugin.swift(1 hunks)lib/pnta_flutter.dart(4 hunks)lib/pnta_flutter_method_channel.dart(1 hunks)lib/pnta_flutter_platform_interface.dart(1 hunks)
🧰 Additional context used
🧬 Code Graph Analysis (2)
ios/Classes/PermissionHandler.swift (1)
android/src/main/kotlin/io/pnta/pnta_flutter/PermissionHandler.kt (1)
checkNotificationPermission(32-44)
ios/Classes/PntaFlutterPlugin.swift (1)
ios/Classes/PermissionHandler.swift (1)
checkNotificationPermission(19-31)
🔇 Additional comments (2)
ios/Classes/PntaFlutterPlugin.swift (1)
20-22: iOS branch looks goodNew switch case correctly delegates to the native permission handler and returns asynchronously. No issues spotted.
example/lib/main.dart (1)
52-65: Potential race: token may still be null
PntaFlutter.deviceTokenis read immediately afterinitialize().
On a freshly-installed app the token is usually delivered asynchronously (APNs/Firebase callback), so_deviceTokenmay remainnulleven when permission is granted.Consider awaiting
registerDevice()(if that’s still required) or listening to a dedicated token stream/callback before updating UI.
There was a problem hiding this comment.
Actionable comments posted: 0
🧹 Nitpick comments (1)
test/pnta_flutter_test.dart (1)
13-15: Add a dedicated unit-test for the newcheckNotificationPermissionAPIThe mock correctly stubs
checkNotificationPermission, but there is no test that exercises the new public-facing Dart API. Without a test, regressions in the platform channel wiring (e.g. wrong method name or return type) could slip through unnoticed.@@ void main() { test('deviceToken getter', () async { ... }); + test('checkNotificationPermission returns true', () async { + final fakePlatform = MockPntaFlutterPlatform(); + PntaFlutterPlatform.instance = fakePlatform; + + final hasPermission = await PntaFlutter.checkNotificationPermission(); + expect(hasPermission, isTrue); + }); }
…states - Add support for .provisional and .ephemeral notification statuses - Ensure main thread dispatch consistency for pre-iOS 10 branch - All notification-capable permission states now properly detected
Fix registration lifecycle for delayed registration scenarios and simplify API by using getter pattern for device token access.