UNPKG

react-native-notification-badge

Version:
103 lines (102 loc) 4.27 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.removeNotificationsWithThreadId = exports.requestNotificationPermissions = exports.getNotificationBadgeSetting = exports.getBadgeCountSync = exports.getBadgeCount = exports.setBadgeCount = void 0; const react_native_1 = require("react-native"); const { NotificationBadge } = react_native_1.NativeModules; /** * **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) */ function setBadgeCount(badgeCount) { if (react_native_1.Platform.OS === 'ios') { return NotificationBadge.setBadgeCount(badgeCount); } else { throw new Error(`setBadgeCount is not supported on ${react_native_1.Platform.OS}!`); } } exports.setBadgeCount = setBadgeCount; /** * **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() */ function getBadgeCount() { if (react_native_1.Platform.OS === 'ios') { return NotificationBadge.getBadgeCount(); } else { throw new Error(`getBadgeCount is not supported on ${react_native_1.Platform.OS}!`); } } exports.getBadgeCount = getBadgeCount; /** * Same as [`getBadgeCount`](#getbadgecount), but synchronously. * @example * const badgeCount = getBadgeCountSync() */ function getBadgeCountSync() { if (react_native_1.Platform.OS === 'ios') { return NotificationBadge.getBadgeCountSync(); } else { throw new Error(`getBadgeCountSync is not supported on ${react_native_1.Platform.OS}!`); } } exports.getBadgeCountSync = getBadgeCountSync; /** * 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") * } */ function getNotificationBadgeSetting() { if (react_native_1.Platform.OS === 'ios') { return NotificationBadge.getNotificationBadgeSetting(); } else { throw new Error(`getNotificationBadgeSetting is not supported on ${react_native_1.Platform.OS}!`); } } exports.getNotificationBadgeSetting = getNotificationBadgeSetting; /** * 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']) */ function requestNotificationPermissions(permissions) { if (react_native_1.Platform.OS === 'ios') { return NotificationBadge.requestNotificationPermissions(permissions); } else { throw new Error(`requestNotificationPermissions is not supported on ${react_native_1.Platform.OS}!`); } } exports.requestNotificationPermissions = requestNotificationPermissions; /** * 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') */ function removeNotificationsWithThreadId(threadId) { if (react_native_1.Platform.OS === 'ios') { return NotificationBadge.removeNotificationsWithThreadId(threadId); } else { throw new Error(`removeNotificationsWithThreadId is not supported on ${react_native_1.Platform.OS}!`); } } exports.removeNotificationsWithThreadId = removeNotificationsWithThreadId;