Skip to content

fivecar/react-native-notification-badge

 
 

Repository files navigation

react-native-notification-badge

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.

react-native-notification-badge GitHub stars GitHub followers

Buy Me a Coffee at ko-fi.com

Installation

  1. npm install react-native-notification-badge or yarn add react-native-notification-badge
  2. cd ios && pod install

API

Note: All APIs throw if Platform.OS !== 'ios'!. So wrap your calls with a Platform.OS check first.

getBadgeCount

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()

getBadgeCountSync

Same as getBadgeCount, but synchronously.

const badgeCount = getBadgeCountSync()

setBadgeCount

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)

getNotificationBadgeSetting

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")
}

requestNotificationPermissions

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'])

removeNotificationsWithThreadId

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')

Other

Check out my other react-native libraries:

About

🔴 A notification badge count manager for React Native

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • Swift 28.9%
  • Objective-C 26.4%
  • JavaScript 19.2%
  • TypeScript 17.7%
  • Ruby 7.8%