diff --git a/.changes/notification-android-granted-permission.md b/.changes/notification-android-granted-permission.md new file mode 100644 index 000000000..b078587c5 --- /dev/null +++ b/.changes/notification-android-granted-permission.md @@ -0,0 +1,6 @@ +--- +notification: patch +notification-js: patch +--- + +Fix notification permission requests never resolving on Android 13 and newer when the permission has already been granted. diff --git a/packages/api-e2e/README.md b/packages/api-e2e/README.md index a01cab7eb..44275d8c1 100644 --- a/packages/api-e2e/README.md +++ b/packages/api-e2e/README.md @@ -91,10 +91,10 @@ skipped on desktop, and the rest run everywhere with the odd test gated. | `geolocation` | `getCurrentPosition`, `watchPosition` (several at once) and `clearWatch` against a location simulated from the host, the granted permission state, and the rejections with location services off (Android). | | mobile-only plugins | `barcode-scanner`, `haptics` and `nfc` need hardware or native UI the driver cannot operate, so only their global API surface is asserted. | Only there. | -The notification permission specs are desktop-only: a mobile app starts out ungranted and -`requestPermission` puts up a system dialog the session would then block on. The rest of the -notification API (action types, channels, the pending/active lists, listeners) only exists on -mobile, and is covered there without the permission. +The notification permission specs cover desktop and repeated requests on Android after +granting `POST_NOTIFICATIONS` over `adb` (Android 13+). They do not interact with the system +permission dialog. The rest of the notification API (action types, channels, the +pending/active lists, listeners) only exists on mobile and is covered there too. The geolocation specs move the device with `adb emu geo fix` (Android) and `xcrun simctl location` (iOS), so on Android they need an emulator rather than a device. diff --git a/packages/api-e2e/test/specs/notification.spec.ts b/packages/api-e2e/test/specs/notification.spec.ts index 85e021e54..8b7df6462 100644 --- a/packages/api-e2e/test/specs/notification.spec.ts +++ b/packages/api-e2e/test/specs/notification.spec.ts @@ -19,12 +19,11 @@ import { // macOS), which the suite cannot observe, and sending is fire-and-forget, so // only the permission model is covered on desktop. // -// The permission specs are desktop-only: mobile starts out ungranted and -// `requestPermission` puts up a system dialog the session would then block on. +// Permission requests can open a system dialog on mobile. The Android regression +// below grants the permission over adb first, then checks the already-granted path. // // Scheduling, action types, channels, the pending/active lists and the -// listeners are only implemented on mobile. None of them need the permission, -// so they are covered there on an app that has not been granted it. +// listeners are only implemented on mobile and are covered separately below. // // On Android, what the system actually posted (`when` and the chronometer // flags) is read back from `dumpsys notification` over adb; Appium's @@ -92,6 +91,61 @@ async function clearNotifications(): Promise { } describePlugin('notification', () => { + itOn( + 'android', + 'repeated permission requests resolve when already granted', + async () => { + adb([ + 'shell', + 'pm', + 'grant', + appId, + 'android.permission.POST_NOTIFICATIONS' + ]) + + const result = await tauri(async (api) => { + if ( + !(await api.core.invoke( + 'plugin:notification|is_permission_granted' + )) + ) { + throw new Error('Notification permission must already be granted') + } + const request = async (fn: () => Promise) => { + let timer: ReturnType | undefined + try { + return await Promise.race([ + fn(), + new Promise((_, reject) => { + timer = setTimeout( + () => + reject( + new Error('Granted notification request did not resolve') + ), + 5000 + ) + }) + ]) + } finally { + clearTimeout(timer) + } + } + const states = [] + for (let i = 0; i < 2; i++) { + states.push(await request(() => api.notification.requestPermission())) + states.push( + await request(() => window.Notification.requestPermission()) + ) + } + return { states, permission: window.Notification.permission } + }) + expect(result).toEqual({ + states: ['granted', 'granted', 'granted', 'granted'], + permission: 'granted' + }) + } + ) + itDesktop('permission is granted on desktop', async () => { const result = await tauri(async (api) => ({ granted: await api.notification.isPermissionGranted(), diff --git a/plugins/notification/android/src/main/java/NotificationPlugin.kt b/plugins/notification/android/src/main/java/NotificationPlugin.kt index 19a82833d..049d4543b 100644 --- a/plugins/notification/android/src/main/java/NotificationPlugin.kt +++ b/plugins/notification/android/src/main/java/NotificationPlugin.kt @@ -258,6 +258,8 @@ class NotificationPlugin(private val activity: Activity): Plugin(activity) { } else { if (getPermissionState(LOCAL_NOTIFICATIONS) !== PermissionState.GRANTED) { requestPermissionForAlias(LOCAL_NOTIFICATIONS, invoke, "permissionsCallback") + } else { + permissionState(invoke) } } }