-
-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathensureNotificationPermission.ts
More file actions
22 lines (19 loc) · 931 Bytes
/
Copy pathensureNotificationPermission.ts
File metadata and controls
22 lines (19 loc) · 931 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
import { PermissionsAndroid, Platform } from 'react-native';
/**
* Asks for POST_NOTIFICATIONS on Android 13+.
*
* Declaring the permission in the manifest is not enough: without a runtime
* grant the foreground service still starts and the work still runs, but its
* notification is silently suppressed, so the task looks like it did nothing.
* The library cannot ask on the app's behalf — a permission prompt needs an
* Activity — so the app has to.
*/
export async function ensureNotificationPermission(): Promise<boolean> {
if (Platform.OS !== 'android') return true;
if (typeof Platform.Version === 'number' && Platform.Version < 33)
return true;
const permission = PermissionsAndroid.PERMISSIONS.POST_NOTIFICATIONS;
if (await PermissionsAndroid.check(permission)) return true;
const result = await PermissionsAndroid.request(permission);
return result === PermissionsAndroid.RESULTS.GRANTED;
}