Skip to content

Conversation

@ttypic
Copy link
Collaborator

@ttypic ttypic commented Aug 19, 2025

iOS forbids to ask for permissions in the async task

Summary by CodeRabbit

  • Bug Fixes
    • Requests notification permission only when needed, reducing redundant prompts.
    • Handles platform-specific exceptions gracefully to prevent crashes during permission requests.
    • Provides clearer error feedback when permission is denied or the request fails.
    • Maintains existing push subscription behavior after permission handling.

iOS forbids to ask for permissions in the async task
@ttypic ttypic requested review from VeskeR and owenpearson August 19, 2025 16:10
@coderabbitai
Copy link

coderabbitai bot commented Aug 19, 2025

Walkthrough

Adjusted browser push permission handling: read Notification.permission, request only when 'default', wrap requestPermission in try/catch to handle platform exceptions, emit specific failure events on errors or denial, and keep subsequent push subscription logic unchanged.

Changes

Cohort / File(s) Summary of Changes
Push permission flow handling
src/plugins/push/getW3CDeviceDetails.ts
Checks Notification.permission first; calls requestPermission() only if 'default'. Wraps call in try/catch to handle platform (iOS) exceptions. Emits GettingPushDeviceDetailsFailed with ErrorInfo(…400, 40000) on exception; emits failure if permission not 'granted'. Push subscription logic unchanged.

Sequence Diagram(s)

sequenceDiagram
  participant U as Caller
  participant G as getW3CDeviceDetails
  participant N as Notification API
  participant P as Push API
  participant E as EventEmitter

  U->>G: invoke
  G->>N: read Notification.permission
  alt permission == "default"
    G->>N: requestPermission()
    note over N,G: Some platforms (e.g., iOS) may throw
    alt success
      N-->>G: permission result
    else throws
      G-->>E: emit GettingPushDeviceDetailsFailed(ErrorInfo("Failed to request permission...", 400, 40000))
      G-->>U: return
    end
  end

  alt permission != "granted"
    G-->>E: emit GettingPushDeviceDetailsFailed("User denied permission to send notifications")
    G-->>U: return
  else permission == "granted"
    G->>P: proceed with push subscription (unchanged)
    P-->>G: subscription details
    G-->>U: return subscription details
  end
Loading

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~10 minutes

Poem

A whisk of ears, a tap-tap key,
I checked the bell before I plea.
If iOS sneezes—catch and care,
Emit the truth into the air.
When granted, hop to push with glee—
Denied? I nibble logs, bug-free. 🐰🔔

Tip

🔌 Remote MCP (Model Context Protocol) integration is now available!

Pro plan users can now connect to remote MCP servers from the Integrations page. Connect with popular remote MCPs such as Notion and Linear to add more context to your reviews and chats.

✨ Finishing Touches
  • 📝 Generate Docstrings
🧪 Generate unit tests
  • Create PR with unit tests
  • Post copyable unit tests in a comment
  • Commit unit tests in branch fix/web-push-ios

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.

Support

Need help? Create a ticket on our support page for assistance with any issues or questions.

CodeRabbit Commands (Invoked using PR/Issue comments)

Type @coderabbitai help to get the list of available commands.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Status, Documentation and Community

  • Visit our Status Page to check the current availability of CodeRabbit.
  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

Caution

Some comments are outside the diff and can’t be posted inline due to platform limitations.

⚠️ Outside diff range comments (1)
src/plugins/push/getW3CDeviceDetails.ts (1)

44-47: Swap ErrorInfo code/status for permission denial

Use code=40000 (Ably error code) and statusCode=400 (HTTP). Current order is reversed.

-    machine.handleEvent(
-      new GettingPushDeviceDetailsFailed(new ErrorInfo('User denied permission to send notifications', 400, 40000)),
-    );
+    machine.handleEvent(
+      new GettingPushDeviceDetailsFailed(new ErrorInfo('User denied permission to send notifications', 40000, 400)),
+    );
📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

💡 Knowledge Base configuration:

  • MCP integration is disabled by default for public repositories
  • Jira integration is disabled by default for public repositories
  • Linear integration is disabled by default for public repositories

You can enable these sources in your CodeRabbit configuration.

📥 Commits

Reviewing files that changed from the base of the PR and between c923b2e and 791fa1a.

📒 Files selected for processing (1)
  • src/plugins/push/getW3CDeviceDetails.ts (1 hunks)
🧰 Additional context used
🧬 Code Graph Analysis (1)
src/plugins/push/getW3CDeviceDetails.ts (1)
src/plugins/push/pushactivation.ts (1)
  • GettingPushDeviceDetailsFailed (431-438)
⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (7)
  • GitHub Check: test-browser (webkit)
  • GitHub Check: test-browser (firefox)
  • GitHub Check: test-npm-package
  • GitHub Check: test-browser (chromium)
  • GitHub Check: test-node (18.x)
  • GitHub Check: test-node (16.x)
  • GitHub Check: test-node (20.x)

Comment on lines +28 to +42
let permission: NotificationPermission = Notification.permission;

if (permission === 'default') {
try {
// throws exception in iOS
permission = await Notification.requestPermission();
} catch (err) {
machine.handleEvent(
new GettingPushDeviceDetailsFailed(
new ErrorInfo('Failed to request permission to send notifications', 400, 40000),
),
);
return;
}
}
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Verification agent

🧩 Analysis chain

iOS exception handling looks good; fix ErrorInfo code/status order, include cause, and add a Notifications API guard

  • Good: Gating on Notification.permission and wrapping requestPermission in try/catch addresses the iOS “must be user-gesture” exception.
  • Issues:
    • ErrorInfo arguments are reversed (code/status). Elsewhere in this file you use (50000, 500). Here it’s (400, 40000). That will break consumers relying on Ably error code semantics.
    • The caught error isn’t included as the cause. Passing it improves debuggability.
    • Optional: Guard for SSR/unsupported browsers; referencing Notification can throw if not available.

Apply:

-  let permission: NotificationPermission = Notification.permission;
+  // Guard: Notifications API availability (SSR or unsupported browsers)
+  if (typeof window === 'undefined' || !('Notification' in window)) {
+    machine.handleEvent(
+      new GettingPushDeviceDetailsFailed(new ErrorInfo('Notifications API not supported', 40000, 400)),
+    );
+    return;
+  }
+  let permission: NotificationPermission = Notification.permission;
-      machine.handleEvent(
-        new GettingPushDeviceDetailsFailed(
-          new ErrorInfo('Failed to request permission to send notifications', 400, 40000),
-        ),
-      );
+      machine.handleEvent(
+        new GettingPushDeviceDetailsFailed(
+          new ErrorInfo('Failed to request permission to send notifications', 40000, 400, err as Error),
+        ),
+      );

To catch any other instances of reversed ErrorInfo code/status in the repo, run:


🏁 Script executed:

#!/bin/bash
# Find suspicious ErrorInfo usages with status/code potentially swapped
rg -nP -C2 '\bnew\s+ErrorInfo\([^,]+,\s*400\s*,\s*40000\b'
rg -nP -C2 '\bnew\s+ErrorInfo\([^,]+,\s*500\s*,\s*50000\b'

Length of output: 2544


Fix ErrorInfo argument order, include cause, and guard Notification API

  • Critical: Swap the ErrorInfo parameters (code/status) for all three instances in src/plugins/push/getW3CDeviceDetails.ts:
    • “Failed to request permission to send notifications” (line 37)
    • “User denied permission to send notifications” (line 46)
    • “Missing ClientOptions.pushServiceWorkerUrl” (line 54)
  • Critical: In the catch block, include the caught err as the cause when constructing ErrorInfo.
  • Optional: Add an SSR/unsupported‐browser guard before referencing Notification.

Apply the following diffs:

--- a/src/plugins/push/getW3CDeviceDetails.ts
+++ b/src/plugins/push/getW3CDeviceDetails.ts
@@ -28,7 +28,16 @@ export async function getW3CDeviceDetails(machine: PushMachineInterpreter, cli
   // throws exception in iOS
   try {
-    let permission: NotificationPermission = Notification.permission;
+    // Guard: Notifications API availability (SSR or unsupported browsers)
+    if (typeof window === 'undefined' || !('Notification' in window)) {
+      machine.handleEvent(
+        new GettingPushDeviceDetailsFailed(
+          new ErrorInfo('Notifications API not supported', 40000, 400),
+        ),
+      );
+      return;
+    }
+    let permission: NotificationPermission = Notification.permission;
 
     if (permission === 'default') {
       permission = await Notification.requestPermission();
@@ -35,7 +44,11 @@ export async function getW3CDeviceDetails(machine: PushMachineInterpreter, cli
       machine.handleEvent(
         new GettingPushDeviceDetailsFailed(
-          new ErrorInfo('Failed to request permission to send notifications', 400, 40000),
+          new ErrorInfo(
+            'Failed to request permission to send notifications',
+            40000,
+            400,
+            err as Error,
+          ),
         ),
       );
       return;
@@ -44,7 +57,7 @@ export async function getW3CDeviceDetails(machine: PushMachineInterpreter, cli
   if (permission !== 'granted') {
     machine.handleEvent(
-      new GettingPushDeviceDetailsFailed(new ErrorInfo('User denied permission to send notifications', 400, 40000)),
+      new GettingPushDeviceDetailsFailed(new ErrorInfo('User denied permission to send notifications', 40000, 400)),
     );
     return;
   }
@@ -52,7 +65,7 @@ export async function getW3CDeviceDetails(machine: PushMachineInterpreter, cli
   if (!swUrl) {
     machine.handleEvent(
-      new GettingPushDeviceDetailsFailed(new ErrorInfo('Missing ClientOptions.pushServiceWorkerUrl', 400, 40000)),
+      new GettingPushDeviceDetailsFailed(new ErrorInfo('Missing ClientOptions.pushServiceWorkerUrl', 40000, 400)),
     );
     return;
   }

Committable suggestion skipped: line range outside the PR's diff.

🤖 Prompt for AI Agents
In src/plugins/push/getW3CDeviceDetails.ts around lines 28-42 (and also update
the related locations at lines 46 and 54), the ErrorInfo numeric arguments are
reversed and the catch doesn't include the caught error as the cause; fix by
swapping the two numeric arguments for the three ErrorInfo constructions
("Failed to request permission to send notifications" at ~37, "User denied
permission to send notifications" at ~46, and "Missing
ClientOptions.pushServiceWorkerUrl" at ~54) so the status and code are in the
correct order, include the caught err as the cause when constructing ErrorInfo
in the catch block, and add a guard before referencing Notification (e.g.,
ensure window is defined and Notification exists or check typeof Notification
!== "undefined") to avoid SSR/unsupported-browser runtime errors.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

2 participants