├── .gitignore ├── .npmignore ├── LICENSE ├── README.md ├── android ├── build.gradle └── src │ └── main │ ├── AndroidManifest.xml │ └── java │ └── me │ └── jhen │ └── react │ ├── BadgeModule.java │ └── BadgePackage.java ├── index.android.js └── package.json /.gitignore: -------------------------------------------------------------------------------- 1 | # Android/IJ 2 | .idea/workspace.xml 3 | .idea/libraries 4 | .gradle 5 | local.properties 6 | *.iml 7 | build 8 | node_modules -------------------------------------------------------------------------------- /.npmignore: -------------------------------------------------------------------------------- 1 | # Android/IJ 2 | .idea/workspace.xml 3 | .idea/libraries 4 | .gradle 5 | local.properties 6 | *.iml 7 | build -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2015 Jhen-Jie Hong 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # React Native Android Badge 2 | 3 | [![NPM version](http://img.shields.io/npm/v/react-native-android-badge.svg?style=flat)](https://www.npmjs.com/package/react-native-android-badge) 4 | 5 | A react native wrapper for [leolin310148/ShortcutBadger](https://github.com/leolin310148/ShortcutBadger). 6 | 7 | ## Why? 8 | 9 | A react-native `PushNotificationIOS` provide setting badge of functions, it belongs to the native iOS support, but Android requires third-party library to do. 10 | 11 | ## Setup 12 | 13 | ```bash 14 | $ npm i --save react-native-android-badge 15 | $ react-native link 16 | ``` 17 | 18 | ## Usage 19 | 20 | ```js 21 | var BadgeAndroid = require('react-native-android-badge'); 22 | 23 | BadgeAndroid.setBadge(10); 24 | ``` 25 | -------------------------------------------------------------------------------- /android/build.gradle: -------------------------------------------------------------------------------- 1 | buildscript { 2 | // The Android Gradle plugin is only required when opening the android folder stand-alone. 3 | // This avoids unnecessary downloads and potential conflicts when the library is included as a 4 | // module dependency in an application project. 5 | if (project == rootProject) { 6 | repositories { 7 | google() 8 | jcenter() 9 | } 10 | 11 | dependencies { 12 | classpath("com.android.tools.build:gradle:3.6.3") 13 | } 14 | } 15 | } 16 | 17 | apply plugin: 'com.android.library' 18 | 19 | def safeExtGet(prop, fallback) { 20 | rootProject.ext.has(prop) ? rootProject.ext.get(prop) : fallback 21 | } 22 | 23 | android { 24 | compileSdkVersion safeExtGet('compileSdkVersion', 29) 25 | 26 | defaultConfig { 27 | minSdkVersion safeExtGet('minSdkVersion', 16) 28 | targetSdkVersion safeExtGet('targetSdkVersion', 29) 29 | versionCode 1 30 | versionName "1.0" 31 | } 32 | lintOptions { 33 | abortOnError false 34 | } 35 | } 36 | 37 | repositories { 38 | mavenCentral() 39 | maven { 40 | url "$projectDir/../../react-native/android" 41 | } 42 | google() 43 | jcenter() 44 | } 45 | 46 | dependencies { 47 | implementation 'com.facebook.react:react-native:+' 48 | implementation 'me.leolin:ShortcutBadger:1.1.22@aar' 49 | } 50 | -------------------------------------------------------------------------------- /android/src/main/AndroidManifest.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | -------------------------------------------------------------------------------- /android/src/main/java/me/jhen/react/BadgeModule.java: -------------------------------------------------------------------------------- 1 | package me.jhen.react; 2 | 3 | import android.content.Context; 4 | 5 | import com.facebook.react.bridge.NativeModule; 6 | import com.facebook.react.bridge.ReactApplicationContext; 7 | import com.facebook.react.bridge.ReactContext; 8 | import com.facebook.react.bridge.ReactContextBaseJavaModule; 9 | import com.facebook.react.bridge.ReactMethod; 10 | 11 | import me.leolin.shortcutbadger.ShortcutBadger; 12 | 13 | public class BadgeModule extends ReactContextBaseJavaModule { 14 | 15 | private Context context; 16 | 17 | public BadgeModule(ReactApplicationContext reactContext) { 18 | super(reactContext); 19 | 20 | this.context = (Context) reactContext; 21 | } 22 | 23 | @Override 24 | public String getName() { 25 | return "BadgeAndroid"; 26 | } 27 | 28 | @ReactMethod 29 | public void setBadge(int number) { 30 | ShortcutBadger.applyCount(getReactApplicationContext(), number); 31 | } 32 | } -------------------------------------------------------------------------------- /android/src/main/java/me/jhen/react/BadgePackage.java: -------------------------------------------------------------------------------- 1 | package me.jhen.react; 2 | 3 | import java.util.*; 4 | 5 | import com.facebook.react.ReactPackage; 6 | import com.facebook.react.bridge.NativeModule; 7 | import com.facebook.react.bridge.JavaScriptModule; 8 | import com.facebook.react.bridge.ReactApplicationContext; 9 | import com.facebook.react.uimanager.ViewManager; 10 | 11 | public class BadgePackage implements ReactPackage { 12 | 13 | @Override 14 | public List createNativeModules( 15 | ReactApplicationContext reactContext) { 16 | List modules = new ArrayList<>(); 17 | 18 | modules.add(new BadgeModule(reactContext)); 19 | return modules; 20 | } 21 | 22 | // Deprecated in RN 0.47 23 | public List> createJSModules() { 24 | return Collections.emptyList(); 25 | } 26 | 27 | @Override 28 | public List createViewManagers(ReactApplicationContext reactContext) { 29 | return Arrays.asList(); 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /index.android.js: -------------------------------------------------------------------------------- 1 | var { NativeModules } = require('react-native'); 2 | module.exports = NativeModules.BadgeAndroid; -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "react-native-android-badge", 3 | "description": "A react native wrapper for leolin310148/ShortcutBadger.", 4 | "version": "0.4.3", 5 | "main": "index.android.js", 6 | "repository": { 7 | "type": "git", 8 | "url": "git@github.com:jhen0409/react-native-android-badge.git" 9 | }, 10 | "author": "Jhen ", 11 | "license": "MIT", 12 | "homepage": "https://github.com/jhen0409/react-native-android-badge#readme", 13 | "peerDependencies": { 14 | "react-native": "*" 15 | } 16 | } 17 | --------------------------------------------------------------------------------