|
| 1 | +import { Platform } from 'react-native'; |
| 2 | +import NotificationsUtilsModule from './NotificationsUtilsModule'; |
| 3 | + |
| 4 | +type OpenSettingsOptions = { |
| 5 | + channelId?: string; |
| 6 | +}; |
| 7 | + |
| 8 | +interface INotificationsUtils { |
| 9 | + /** |
| 10 | + * API used to open the Platform specific System settings for the application. |
| 11 | + * |
| 12 | + * If the API version is >= 26: |
| 13 | + * - With no `channelId`, the notification settings screen is displayed. |
| 14 | + * - With a `channelId`, the notification settings screen for the specific channel is displayed. |
| 15 | + * |
| 16 | + * If the API version is < 26, the application settings screen is displayed. The `channelId` |
| 17 | + * is ignored. |
| 18 | + * |
| 19 | + * If an invalid `channelId` is provided (e.g. does not exist), the settings screen will redirect |
| 20 | + * back to your application. |
| 21 | + * |
| 22 | + * On iOS, this is a no-op & instantly resolves. |
| 23 | + * |
| 24 | + * @platform android |
| 25 | + * @param channelId The ID of the channel which will be opened. Can be ignored/omitted to display the |
| 26 | + * overall notification settings. |
| 27 | + */ |
| 28 | + openSettings(options?: OpenSettingsOptions): Promise<void>; |
| 29 | +} |
| 30 | + |
| 31 | +const NotificationsUtils: INotificationsUtils = { |
| 32 | + openSettings: (options) => { |
| 33 | + if (Platform.OS === 'android') { |
| 34 | + if (typeof options?.channelId !== 'string') { |
| 35 | + throw new Error( |
| 36 | + `NotificationsUtils.openSettings: Expected 'channelId' to be a string, got ${typeof options?.channelId}.` |
| 37 | + ); |
| 38 | + } |
| 39 | + const { channelId } = options; |
| 40 | + |
| 41 | + return NotificationsUtilsModule.openAppNotificationsSettings(channelId); |
| 42 | + } |
| 43 | + return NotificationsUtilsModule.openAppNotificationsSettings(); |
| 44 | + }, |
| 45 | +}; |
| 46 | + |
| 47 | +export default NotificationsUtils; |
0 commit comments