-
-
Notifications
You must be signed in to change notification settings - Fork 8
/
index.ts
97 lines (89 loc) · 3.69 KB
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
import { NativeModules, Platform } from 'react-native';
const { NotificationBadge } = NativeModules;
export type BadgeSetting = 'enabled' | 'disabled' | 'notSupported' | 'unknown';
export type NotificationPermission = 'badge' | 'alert' | 'sound' | 'carPlay' | 'criticalAlert' | 'providesAppNotificationSettings' | 'provisional' | 'announcement';
/**
* **Asynchronously sets the Badge count.**
*
* Note: If no notification permissions have been granted yet, this will also ask the user for notification permissions (only `[.badge]`), so request permissions before calling this using a library like [react-native-permissions](https://github.com/react-native-community/react-native-permissions)
* @param badgeCount The new badge count to set
* @example
* await setBadgeCount(2)
*/
export function setBadgeCount(badgeCount: number): Promise<void> {
if (Platform.OS === 'ios') {
return NotificationBadge.setBadgeCount(badgeCount);
} else {
throw new Error(`setBadgeCount is not supported on ${Platform.OS}!`)
}
}
/**
* **Asynchronously returns the current Badge count.**
*
* Note: If no notification permissions have been granted yet, this will also ask the user for notification permissions (only `[.badge]`), so request permissions before calling this using a library like [react-native-permissions](https://github.com/react-native-community/react-native-permissions)
* @example
* const badgeCount = await getBadgeCount()
*/
export function getBadgeCount(): Promise<number> {
if (Platform.OS === 'ios') {
return NotificationBadge.getBadgeCount();
} else {
throw new Error(`getBadgeCount is not supported on ${Platform.OS}!`)
}
}
/**
* Same as [`getBadgeCount`](#getbadgecount), but synchronously.
* @example
* const badgeCount = getBadgeCountSync()
*/
export function getBadgeCountSync(): number {
if (Platform.OS === 'ios') {
return NotificationBadge.getBadgeCountSync();
} else {
throw new Error(`getBadgeCountSync is not supported on ${Platform.OS}!`)
}
}
/**
* Asynchronously gets the current state of the "Notification Badge" permission setting.
* @example
* const permission = await getNotificationBadgeSetting()
* if (permission === 'enabled') {
* await setBadgeCount(5)
* } else {
* console.log("Badge permission has not yet been granted. I'll ask the user later")
* }
*/
export function getNotificationBadgeSetting(): Promise<BadgeSetting> {
if (Platform.OS === 'ios') {
return NotificationBadge.getNotificationBadgeSetting();
} else {
throw new Error(`getNotificationBadgeSetting is not supported on ${Platform.OS}!`)
}
}
/**
* Asynchronously request the user to grant the specified permissions.
* @param permissions The array of permissions to grant
* @example
* const granted = await requestNotificationPermissions(['alert', 'badge', 'sound'])
*/
export function requestNotificationPermissions(permissions: NotificationPermission[]): Promise<boolean> {
if (Platform.OS === 'ios') {
return NotificationBadge.requestNotificationPermissions(permissions);
} else {
throw new Error(`requestNotificationPermissions is not supported on ${Platform.OS}!`)
}
}
/**
* Remove all notification with the given Thread ID from the User's Notification Center
* @param threadId The Thread ID to filter notifications by which shall be removed
* @returns The count of the notifications that were removed
* @example
* await removeNotificationsWithThreadId('group-chat-1')
*/
export function removeNotificationsWithThreadId(threadId: string): Promise<number> {
if (Platform.OS === 'ios') {
return NotificationBadge.removeNotificationsWithThreadId(threadId);
} else {
throw new Error(`removeNotificationsWithThreadId is not supported on ${Platform.OS}!`)
}
}