For when you don't want to use a fully featured notification manager
A very simple notification badge count manager for React Native that just works.
npm install @bondulich/react-native-notification-badge
oryarn add @bondulich/react-native-notification-badge
cd ios && pod install
Note: All APIs throw if Platform.OS !== 'ios'
!. So wrap your calls with a Platform.OS
check first.
import {setBadgeCount} from '@bondulich/react-native-notification-badge';
import {Platform} from 'react-native';
if (Platform.OS === 'ios') {
await setBadgeCount(0);
}
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
const badgeCount = await getBadgeCount()
Same as getBadgeCount
, but synchronously.
const badgeCount = getBadgeCountSync()
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
await setBadgeCount(2)
Asynchronously gets the current state of the "Notification Badge" permission setting.
Possible values are: 'enabled' | 'disabled' | 'notSupported' | 'unknown'
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")
}
Asynchronously request the user to grant the specified permissions.
Possible parameter values are: ['badge' | 'alert' | 'sound' | 'carPlay' | 'criticalAlert' | 'providesAppNotificationSettings' | 'provisional' | 'announcement']
const granted = await requestNotificationPermissions(['badge', 'alert', 'sound'])
Remove all notification with the given Thread ID from the User's Notification Center.
The Thread ID is a property in the APNS payload which can be used to group notifications in iOS 10.0 or higher.
Returns the count of notifications that were removed.
const removed = await removeNotificationsWithThreadId('group-chat-2')
Check out my other react-native libraries:
- react-native-blurhash: 🖼️ Give your users the loading experience they want.
- react-native-google-nearby-messages: 📲 Communicate with nearby devices using Bluetooth, BLE, WiFi and near-ultrasonic audio. Broadcast and receive small payloads (like strings) using the easy-to-use React Native API!
- react-native-sectioned-slider: An iOS 11 Control Center inspired Slider for React Native