@tower-supplies/react-native-broadcast-receiver 0.6.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (68) hide show
  1. package/LICENSE +20 -0
  2. package/README.md +53 -0
  3. package/android/build.gradle +158 -0
  4. package/android/gradle.properties +6 -0
  5. package/android/src/main/AndroidManifest.xml +5 -0
  6. package/android/src/main/java/com/broadcastreceiver/BroadcastReceiverImpl.kt +90 -0
  7. package/android/src/main/java/com/broadcastreceiver/BroadcastReceiverPackage.kt +35 -0
  8. package/android/src/main/java/com/broadcastreceiver/BroadcastReceiverReceiver.kt +104 -0
  9. package/android/src/main/java/com/broadcastreceiver/Constants.kt +8 -0
  10. package/android/src/main/java/com/broadcastreceiver/IntentConfiguration.kt +21 -0
  11. package/android/src/newarch/java/com/BroadcastReceiverModule.kt +51 -0
  12. package/android/src/oldarch/java/com/BroadcastReceiverModule.kt +54 -0
  13. package/ios/BarcodeScanner-Bridging-Header.h +2 -0
  14. package/ios/BarcodeScanner.m +14 -0
  15. package/ios/BarcodeScanner.swift +8 -0
  16. package/ios/BarcodeScanner.xcodeproj/project.pbxproj +283 -0
  17. package/lib/commonjs/BroadcastReceiver.js +51 -0
  18. package/lib/commonjs/BroadcastReceiver.js.map +1 -0
  19. package/lib/commonjs/config.js +35 -0
  20. package/lib/commonjs/config.js.map +1 -0
  21. package/lib/commonjs/error.js +12 -0
  22. package/lib/commonjs/error.js.map +1 -0
  23. package/lib/commonjs/index.js +19 -0
  24. package/lib/commonjs/index.js.map +1 -0
  25. package/lib/commonjs/js/NativeBroadcastReceiver.js +9 -0
  26. package/lib/commonjs/js/NativeBroadcastReceiver.js.map +1 -0
  27. package/lib/commonjs/package.json +1 -0
  28. package/lib/commonjs/types.js +6 -0
  29. package/lib/commonjs/types.js.map +1 -0
  30. package/lib/commonjs/util.js +10 -0
  31. package/lib/commonjs/util.js.map +1 -0
  32. package/lib/module/BroadcastReceiver.js +49 -0
  33. package/lib/module/BroadcastReceiver.js.map +1 -0
  34. package/lib/module/config.js +32 -0
  35. package/lib/module/config.js.map +1 -0
  36. package/lib/module/error.js +9 -0
  37. package/lib/module/error.js.map +1 -0
  38. package/lib/module/index.js +9 -0
  39. package/lib/module/index.js.map +1 -0
  40. package/lib/module/js/NativeBroadcastReceiver.js +5 -0
  41. package/lib/module/js/NativeBroadcastReceiver.js.map +1 -0
  42. package/lib/module/types.js +4 -0
  43. package/lib/module/types.js.map +1 -0
  44. package/lib/module/util.js +7 -0
  45. package/lib/module/util.js.map +1 -0
  46. package/lib/typescript/BroadcastReceiver.d.ts +13 -0
  47. package/lib/typescript/BroadcastReceiver.d.ts.map +1 -0
  48. package/lib/typescript/config.d.ts +11 -0
  49. package/lib/typescript/config.d.ts.map +1 -0
  50. package/lib/typescript/error.d.ts +3 -0
  51. package/lib/typescript/error.d.ts.map +1 -0
  52. package/lib/typescript/index.d.ts +9 -0
  53. package/lib/typescript/index.d.ts.map +1 -0
  54. package/lib/typescript/js/NativeBroadcastReceiver.d.ts +8 -0
  55. package/lib/typescript/js/NativeBroadcastReceiver.d.ts.map +1 -0
  56. package/lib/typescript/types.d.ts +39 -0
  57. package/lib/typescript/types.d.ts.map +1 -0
  58. package/lib/typescript/util.d.ts +4 -0
  59. package/lib/typescript/util.d.ts.map +1 -0
  60. package/package.json +162 -0
  61. package/react-native-broadcast-receiver.podspec +35 -0
  62. package/src/BroadcastReceiver.ts +60 -0
  63. package/src/config.ts +36 -0
  64. package/src/error.ts +9 -0
  65. package/src/index.ts +10 -0
  66. package/src/js/NativeBroadcastReceiver.ts +11 -0
  67. package/src/types.ts +47 -0
  68. package/src/util.ts +7 -0
package/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2022 Mohan
4
+ Permission is hereby granted, free of charge, to any person obtaining a copy
5
+ of this software and associated documentation files (the "Software"), to deal
6
+ in the Software without restriction, including without limitation the rights
7
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8
+ copies of the Software, and to permit persons to whom the Software is
9
+ furnished to do so, subject to the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be included in all
12
+ copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,53 @@
1
+ # react-native-broadcast-receiver
2
+
3
+ :sparkles: :rocket: Supports React-Native Bridgeless mode
4
+
5
+ This package allows you to register custom intent to listen in react native applications.
6
+ The idea here is to reduce the number app builds that are required if you change the native code in react native. With this you can inject custom intent config at runtime.
7
+
8
+
9
+ ## Installation
10
+
11
+ ### npm
12
+ ```sh
13
+ npm install @tower-supplies/react-native-broadcast-receiver
14
+ ```
15
+
16
+ yarn
17
+
18
+ ```sh
19
+ yarn add @tower-supplies/react-native-broadcast-receiver
20
+ ```
21
+
22
+ ### Usage
23
+
24
+ #### Listen to broadcast events with custom intent actions and extraDataKey
25
+
26
+ ```js
27
+ import { BroadcastReceiver } from '@tower-supplies/react-native-broadcast-receiver';
28
+
29
+ BroadcastReceiver.setIntentActionConfig([
30
+ { action: 'com.zzzz.yyyy.action1', datakey: '<data_key1>' },
31
+ { action: 'com.zzzz.yyyy.action2', datakey: '<data_key2>' },
32
+ { action: 'com.zzzz.yyyy.action3', datakey: '' }, // For empty dataKey an object will be returned instead of a string
33
+ ]);
34
+
35
+ React.useEffect(() => {
36
+ const sub = BroadcastReceiver.addEventListener((d) =>
37
+ setScanned((x) => [...x, d.data])
38
+ );
39
+ return () => sub.remove();
40
+ }, []);
41
+ ```
42
+
43
+ ## Contributing
44
+
45
+ See the [contributing guide](CONTRIBUTING.md) to learn how to contribute to the repository and the development workflow.
46
+
47
+ ## License
48
+
49
+ MIT
50
+
51
+ ---
52
+
53
+ Made with [create-react-native-library](https://github.com/callstack/react-native-builder-bob)
@@ -0,0 +1,158 @@
1
+ buildscript {
2
+ // Buildscript is evaluated before everything else so we can't use getExtOrDefault
3
+ def kotlin_version = rootProject.ext.has('kotlinVersion') ? rootProject.ext.get('kotlinVersion') : project.properties['BroadcastReceiver_kotlinVersion']
4
+
5
+ repositories {
6
+ google()
7
+ mavenCentral()
8
+ gradlePluginPortal()
9
+ }
10
+
11
+ dependencies {
12
+ classpath 'com.android.tools.build:gradle:7.3.1'
13
+ // noinspection DifferentKotlinGradleVersion
14
+ classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
15
+ }
16
+ }
17
+
18
+ def isNewArchitectureEnabled() {
19
+ return rootProject.hasProperty("newArchEnabled") && rootProject.getProperty("newArchEnabled") == "true"
20
+ }
21
+
22
+ apply plugin: 'com.android.library'
23
+ apply plugin: 'kotlin-android'
24
+
25
+ if (isNewArchitectureEnabled()) {
26
+ apply plugin: 'com.facebook.react'
27
+ }
28
+
29
+ def getExtOrDefault(name) {
30
+ return rootProject.ext.has(name) ? rootProject.ext.get(name) : project.properties['BroadcastReceiver_' + name]
31
+ }
32
+
33
+ def getExtOrIntegerDefault(name) {
34
+ return rootProject.ext.has(name) ? rootProject.ext.get(name) : (project.properties['BroadcastReceiver_' + name]).toInteger()
35
+ }
36
+
37
+ android {
38
+ compileSdkVersion getExtOrIntegerDefault('compileSdkVersion')
39
+ namespace "com.broadcastreceiver"
40
+
41
+ defaultConfig {
42
+ minSdkVersion getExtOrIntegerDefault('minSdkVersion')
43
+ targetSdkVersion getExtOrIntegerDefault('targetSdkVersion')
44
+ buildConfigField "boolean", "IS_NEW_ARCHITECTURE_ENABLED", isNewArchitectureEnabled().toString()
45
+ sourceSets {
46
+ main {
47
+ if (isNewArchitectureEnabled()) {
48
+ java.srcDirs += ['src/newarch']
49
+ } else {
50
+ java.srcDirs += ['src/oldarch']
51
+ }
52
+ }
53
+ }
54
+ }
55
+ buildTypes {
56
+ release {
57
+ minifyEnabled false
58
+ }
59
+ }
60
+
61
+ lintOptions {
62
+ disable 'GradleCompatible'
63
+ }
64
+
65
+ compileOptions {
66
+ sourceCompatibility JavaVersion.VERSION_1_8
67
+ targetCompatibility JavaVersion.VERSION_1_8
68
+ }
69
+
70
+ }
71
+
72
+ repositories {
73
+ mavenCentral()
74
+ google()
75
+
76
+ def found = false
77
+ def defaultDir = null
78
+ def androidSourcesName = 'React Native sources'
79
+
80
+ if (rootProject.ext.has('reactNativeAndroidRoot')) {
81
+ defaultDir = rootProject.ext.get('reactNativeAndroidRoot')
82
+ } else {
83
+ defaultDir = new File(
84
+ projectDir,
85
+ '/../../../node_modules/react-native/android'
86
+ )
87
+ }
88
+
89
+ if (defaultDir.exists()) {
90
+ maven {
91
+ url defaultDir.toString()
92
+ name androidSourcesName
93
+ }
94
+
95
+ logger.info(":${project.name}:reactNativeAndroidRoot ${defaultDir.canonicalPath}")
96
+ found = true
97
+ } else {
98
+ def parentDir = rootProject.projectDir
99
+
100
+ 1.upto(5, {
101
+ if (found) return true
102
+ parentDir = parentDir.parentFile
103
+
104
+ def androidSourcesDir = new File(
105
+ parentDir,
106
+ 'node_modules/react-native'
107
+ )
108
+
109
+ def androidPrebuiltBinaryDir = new File(
110
+ parentDir,
111
+ 'node_modules/react-native/android'
112
+ )
113
+
114
+ if (androidPrebuiltBinaryDir.exists()) {
115
+ maven {
116
+ url androidPrebuiltBinaryDir.toString()
117
+ name androidSourcesName
118
+ }
119
+
120
+ logger.info(":${project.name}:reactNativeAndroidRoot ${androidPrebuiltBinaryDir.canonicalPath}")
121
+ found = true
122
+ } else if (androidSourcesDir.exists()) {
123
+ maven {
124
+ url androidSourcesDir.toString()
125
+ name androidSourcesName
126
+ }
127
+
128
+ logger.info(":${project.name}:reactNativeAndroidRoot ${androidSourcesDir.canonicalPath}")
129
+ found = true
130
+ }
131
+ })
132
+ }
133
+
134
+ if (!found) {
135
+ throw new GradleException(
136
+ "${project.name}: unable to locate React Native android sources. " +
137
+ "Ensure you have you installed React Native as a dependency in your project and try again."
138
+ )
139
+ }
140
+ }
141
+
142
+ def kotlin_version = getExtOrDefault('kotlinVersion')
143
+
144
+ dependencies {
145
+ //noinspection GradleDynamicVersion
146
+ implementation "com.facebook.react:react-native:+"
147
+ implementation "com.google.code.gson:gson:2.13.2"
148
+ implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
149
+ // From node_modules
150
+ }
151
+
152
+ if (isNewArchitectureEnabled()) {
153
+ react {
154
+ jsRootDir = file("../src/")
155
+ libraryName = "BroadcastReceiver"
156
+ codegenJavaPackageName = "com.broadcastreceiver"
157
+ }
158
+ }
@@ -0,0 +1,6 @@
1
+ BroadcastReceiver_kotlinVersion=1.7.0
2
+ BroadcastReceiver_minSdkVersion=21
3
+ BroadcastReceiver_targetSdkVersion=31
4
+ BroadcastReceiver_compileSdkVersion=33
5
+ BroadcastReceiver_ndkversion=21.4.7075529
6
+ android.useAndroidX=true
@@ -0,0 +1,5 @@
1
+ <manifest xmlns:android="http://schemas.android.com/apk/res/android"
2
+ xmlns:tools="http://schemas.android.com/tools"
3
+ package="com.broadcastreceiver">
4
+ <uses-permission android:name="com.google.android.c2dm.permission.RECEIVE"/>
5
+ </manifest>
@@ -0,0 +1,90 @@
1
+ package com.broadcastreceiver
2
+
3
+ import android.annotation.SuppressLint
4
+ import android.content.IntentFilter
5
+ import android.provider.Settings
6
+ import android.util.Log
7
+ import android.os.Build;
8
+ import android.content.Context;
9
+ import com.facebook.react.BuildConfig
10
+ import com.facebook.react.bridge.*
11
+ import com.facebook.react.modules.core.DeviceEventManagerModule
12
+
13
+
14
+ class BroadcastReceiverImpl(reactContext: ReactApplicationContext) : ReactContextBaseJavaModule(reactContext){
15
+ private var broadcastReceiver = BroadcastReceiverReceiver()
16
+
17
+ init {
18
+ cReactContext = reactContext
19
+ }
20
+
21
+ override fun getName(): String {
22
+ return Constants.ModuleName
23
+ }
24
+
25
+ private fun getReactContext(): ReactApplicationContext {
26
+ return cReactContext
27
+ }
28
+
29
+ // Example method
30
+ // See https://reactnative.dev/docs/native-modules-android
31
+ fun multiply(a: Double, b: Double, promise: Promise) {
32
+ promise.resolve(a * b)
33
+ }
34
+
35
+ fun setIntentConfig(args: ReadableArray, promise: Promise) {
36
+ if (BuildConfig.DEBUG) {
37
+ Log.d(name, args.toString())
38
+ Log.d(name, "addActions: register receiver")
39
+ }
40
+ val argsArray = args.toArrayList().filterIsInstance<List<String>>()
41
+ val data = argsArray.map {
42
+ Pair(it[0], it[1])
43
+ }
44
+ IntentConfiguration.addToMap(data)
45
+ registerBroadcastReceiver()
46
+ promise.resolve(true)
47
+ }
48
+
49
+ @SuppressLint("HardwareIds")
50
+ fun getPhoneID(promise: Promise) {
51
+ try {
52
+ val secureId: String = Settings.Secure.getString(
53
+ getReactContext().contentResolver, Settings.Secure.ANDROID_ID
54
+ )
55
+ promise.resolve(Arguments.fromJavaArgs(arrayOf(secureId)))
56
+ } catch (e: Exception) {
57
+ promise.reject(RuntimeException("Could not get unique Id"))
58
+ }
59
+ }
60
+
61
+
62
+ companion object {
63
+ lateinit var cReactContext: ReactApplicationContext
64
+
65
+ fun sendEvent(eventName: String, params: WritableMap) {
66
+ cReactContext.emitDeviceEvent(eventName, params)
67
+ }
68
+ }
69
+
70
+ fun registerBroadcastReceiver() {
71
+ if (BuildConfig.DEBUG) Log.d(name, "register receiver")
72
+
73
+ val filter = IntentFilter()
74
+ val filterMap = IntentConfiguration.getMap()
75
+
76
+ filterMap.forEach { filter.addAction(it.key) }
77
+ filter.addCategory("android.intent.category.DEFAULT")
78
+
79
+ val context = getReactContext()
80
+ if (Build.VERSION.SDK_INT >= 34 && context.applicationInfo.targetSdkVersion >= 34) {
81
+ context.registerReceiver(broadcastReceiver, filter, Context.RECEIVER_EXPORTED);
82
+ } else {
83
+ context.registerReceiver(broadcastReceiver, filter);
84
+ }
85
+ }
86
+
87
+ fun unregisterBroadcastReceiver() {
88
+ getReactContext().unregisterReceiver(broadcastReceiver)
89
+ }
90
+ }
@@ -0,0 +1,35 @@
1
+ package com.broadcastreceiver
2
+
3
+ import com.facebook.react.TurboReactPackage
4
+ import com.facebook.react.bridge.NativeModule
5
+ import com.facebook.react.bridge.ReactApplicationContext
6
+ import com.facebook.react.module.model.ReactModuleInfo
7
+ import com.facebook.react.module.model.ReactModuleInfoProvider
8
+
9
+
10
+ class BroadcastReceiverPackage : TurboReactPackage() {
11
+ override fun getModule(name: String, reactContext: ReactApplicationContext): NativeModule? {
12
+ return if (name == Constants.ModuleName) {
13
+ BroadcastReceiverModule(reactContext)
14
+ } else {
15
+ null
16
+ }
17
+ }
18
+
19
+ override fun getReactModuleInfoProvider(): ReactModuleInfoProvider {
20
+ return ReactModuleInfoProvider {
21
+ val moduleInfos: MutableMap<String, ReactModuleInfo> =
22
+ HashMap()
23
+ moduleInfos[Constants.ModuleName] = ReactModuleInfo(
24
+ Constants.ModuleName,
25
+ Constants.ModuleName,
26
+ false, // canOverrideExistingModule
27
+ false, // needsEagerInit
28
+ true, // hasConstants
29
+ false, // isCxxModule
30
+ true // isTurboModule
31
+ )
32
+ moduleInfos
33
+ }
34
+ }
35
+ }
@@ -0,0 +1,104 @@
1
+ package com.broadcastreceiver
2
+
3
+ import android.content.BroadcastReceiver
4
+ import android.content.Context
5
+ import android.content.Intent
6
+ import android.os.Build
7
+ import android.os.Parcelable
8
+ import android.util.Log
9
+ import com.facebook.react.bridge.Arguments
10
+ import com.facebook.react.bridge.WritableMap
11
+ import com.google.gson.Gson
12
+
13
+ class BroadcastReceiverReceiver : BroadcastReceiver() {
14
+ override fun onReceive(context: Context?, intent: Intent?) {
15
+ // Check if the intent is not null
16
+ if (intent != null) {
17
+ // Get data from intent
18
+ val data = getDataFromIntent(intent)
19
+ if (data != null) {
20
+ val params = Arguments.createMap()
21
+ params.putString(Constants.Action, intent.action)
22
+
23
+ when (data) {
24
+ is ResultData.StringData -> {
25
+ // Handle the String case
26
+ params.putString(Constants.DataProp, data.value)
27
+ }
28
+
29
+ is ResultData.MapData -> {
30
+ // Handle the WritableMap case
31
+ params.putMap(Constants.DataProp, data.map)
32
+ }
33
+ }
34
+
35
+ BroadcastReceiverImpl.sendEvent(Constants.BroadcastEventName, params)
36
+ }
37
+ }
38
+ }
39
+
40
+ sealed class ResultData {
41
+ data class StringData(val value: String?) : ResultData()
42
+ data class MapData(val map: WritableMap) : ResultData()
43
+ }
44
+
45
+ private fun extraToString(intent: Intent, key: String): String {
46
+ val extra = intent.extras?.get(key)
47
+
48
+ if (extra is Parcelable) {
49
+ val parcelable = if (Build.VERSION.SDK_INT > Build.VERSION_CODES.TIRAMISU) {
50
+ intent.getParcelableExtra<Parcelable>(key, Parcelable::class.java)
51
+ } else {
52
+ intent.getParcelableExtra<Parcelable>(key)
53
+ }
54
+ val json = Gson().toJson(parcelable)
55
+ // Log.d(TAG, "Parcelable: $json")
56
+ return json?.trim() ?: ""
57
+ }
58
+
59
+ if (extra is Array<*> && extra.isArrayOf<Parcelable>()) {
60
+ val parcelableArray = if (Build.VERSION.SDK_INT > Build.VERSION_CODES.TIRAMISU) {
61
+ intent.getParcelableArrayExtra<Parcelable>(key, Parcelable::class.java)
62
+ } else {
63
+ intent.getParcelableArrayExtra(key)
64
+ }
65
+ val json = Gson().toJson(parcelableArray)
66
+ // Log.d(TAG, "ParcelableArray: $json")
67
+ return json?.trim() ?: ""
68
+ }
69
+
70
+ // Log.d(TAG, "Basic: $extra")
71
+ return extra?.toString()?.trim() ?: ""
72
+ }
73
+
74
+ private fun getDataFromIntent(intent: Intent): ResultData? {
75
+ val action = intent.action
76
+ val intentFilterMap = IntentConfiguration.getMap()
77
+
78
+ Log.d(TAG, intentFilterMap.toString())
79
+ if (intentFilterMap.containsKey(action)) {
80
+ val dataKey = intentFilterMap[action]
81
+
82
+ // If a data key is given, only the corresponding string will be returned; otherwise, the entire object will be returned.
83
+ if (!dataKey.isNullOrEmpty() && intent.extras?.containsKey(dataKey) == true) {
84
+ val barcodeData = extraToString(intent, dataKey)
85
+ return ResultData.StringData(barcodeData)
86
+ } else {
87
+ // Create a WritableMap if there are no specific string requirements
88
+ val dataMap = Arguments.createMap()
89
+ intent.extras?.let { extras ->
90
+ for (key in extras.keySet()) {
91
+ dataMap.putString(key, extraToString(intent, key))
92
+ }
93
+ }
94
+ return ResultData.MapData(dataMap)
95
+ }
96
+ }
97
+
98
+ return null
99
+ }
100
+
101
+ companion object {
102
+ const val TAG = "Receiver"
103
+ }
104
+ }
@@ -0,0 +1,8 @@
1
+ package com.broadcastreceiver
2
+
3
+ object Constants {
4
+ const val BroadcastEventName = "BROADCAST_EVENT"
5
+ const val ModuleName = "RNBroadcastReceiver"
6
+ const val DataProp = "data"
7
+ const val Action = "action"
8
+ }
@@ -0,0 +1,21 @@
1
+ package com.broadcastreceiver
2
+
3
+ object IntentConfiguration {
4
+ private val filterMap: MutableMap<String, String> = mutableMapOf(
5
+ "com.cipherlab.barcodebaseapi.PASS_DATA_2_APP" to "Decoder_Data",
6
+ "com.barcodeservice.broadcast.string" to "barcodedata",
7
+ "BROADCAST_BARCODE" to "BROADCAST_BARCODE_STRING",
8
+ "zebra.barcode" to "com.motorolasolutions.emdk.datawedge.data_string",
9
+ "com.honeywell.scantointent.intent.action.BARCODE" to "data"
10
+ )
11
+
12
+ fun getMap(): MutableMap<String, String> {
13
+ return filterMap
14
+ }
15
+
16
+ fun addToMap(args: List<Pair<String, String>>) {
17
+ args.forEach {
18
+ filterMap[it.first] = it.second
19
+ }
20
+ }
21
+ }
@@ -0,0 +1,51 @@
1
+ package com.broadcastreceiver
2
+
3
+ import android.util.Log
4
+ import com.facebook.react.BuildConfig
5
+ import com.facebook.react.bridge.*
6
+
7
+ class BroadcastReceiverModule(reactContext: ReactApplicationContext) :
8
+ NativeBroadcastReceiverSpec(reactContext), LifecycleEventListener {
9
+ private var implementation = BroadcastReceiverImpl(reactContext)
10
+ private var name = implementation.name
11
+
12
+ init {
13
+ reactContext.addLifecycleEventListener(
14
+ this
15
+ )
16
+ }
17
+
18
+ override fun getName(): String {
19
+ return implementation.name
20
+ }
21
+
22
+ // Example method
23
+ // See https://reactnative.dev/docs/native-modules-android
24
+ fun multiply(a: Double, b: Double, promise: Promise) {
25
+ implementation.multiply(a, b, promise)
26
+ }
27
+
28
+ override fun setIntentConfig(args: ReadableArray, promise: Promise) {
29
+ implementation.setIntentConfig(args, promise)
30
+ }
31
+
32
+ override fun getPhoneID(promise: Promise) {
33
+ implementation.getPhoneID(promise)
34
+ }
35
+
36
+ //region: LifecycleEventListener
37
+ override fun onHostResume() {
38
+ if (BuildConfig.DEBUG) Log.d(name, "onHostResume: register Application receivers")
39
+ implementation.registerBroadcastReceiver()
40
+ }
41
+
42
+ override fun onHostPause() {
43
+ if (BuildConfig.DEBUG) Log.d(name, "onHostPause: unregister receivers")
44
+ implementation.unregisterBroadcastReceiver()
45
+ }
46
+
47
+ override fun onHostDestroy() {
48
+ if (BuildConfig.DEBUG) Log.d(name, "onHostDestroy: Destroy host")
49
+ }
50
+ //endregion
51
+ }
@@ -0,0 +1,54 @@
1
+ package com.broadcastreceiver
2
+
3
+ import android.util.Log
4
+ import com.facebook.react.BuildConfig
5
+ import com.facebook.react.bridge.*
6
+
7
+ class BroadcastReceiverModule(reactContext: ReactApplicationContext) : ReactContextBaseJavaModule(reactContext), LifecycleEventListener {
8
+ private var implementation = BroadcastReceiverImpl(reactContext)
9
+ private var name = implementation.name
10
+
11
+ init {
12
+ reactContext.addLifecycleEventListener(
13
+ this
14
+ )
15
+ }
16
+
17
+ @ReactMethod
18
+ override fun getName(): String {
19
+ return implementation.name
20
+ }
21
+
22
+ // Example method
23
+ // See https://reactnative.dev/docs/native-modules-android
24
+ @ReactMethod
25
+ fun multiply(a: Double, b: Double, promise: Promise) {
26
+ implementation.multiply(a, b, promise)
27
+ }
28
+
29
+ @ReactMethod
30
+ fun setIntentConfig(args: ReadableArray, promise: Promise) {
31
+ implementation.setIntentConfig(args, promise)
32
+ }
33
+
34
+ @ReactMethod
35
+ fun getPhoneID(promise: Promise) {
36
+ implementation.getPhoneID(promise)
37
+ }
38
+
39
+ //region: LifecycleEventListener
40
+ override fun onHostResume() {
41
+ if (BuildConfig.DEBUG) Log.d(name, "onHostResume: register Application receivers")
42
+ implementation.registerBroadcastReceiver()
43
+ }
44
+
45
+ override fun onHostPause() {
46
+ if (BuildConfig.DEBUG) Log.d(name, "onHostPause: unregister receivers")
47
+ implementation.unregisterBroadcastReceiver()
48
+ }
49
+
50
+ override fun onHostDestroy() {
51
+ if (BuildConfig.DEBUG) Log.d(name, "onHostDestroy: Destroy host")
52
+ }
53
+ //endregion
54
+ }
@@ -0,0 +1,2 @@
1
+ #import <React/RCTBridgeModule.h>
2
+ #import <React/RCTViewManager.h>
@@ -0,0 +1,14 @@
1
+ #import <React/RCTBridgeModule.h>
2
+
3
+ @interface RCT_EXTERN_MODULE(RNBroadcastReceiver, NSObject)
4
+
5
+ RCT_EXTERN_METHOD(multiply:(float)a withB:(float)b
6
+ withResolver:(RCTPromiseResolveBlock)resolve
7
+ withRejecter:(RCTPromiseRejectBlock)reject)
8
+
9
+ + (BOOL)requiresMainQueueSetup
10
+ {
11
+ return NO;
12
+ }
13
+
14
+ @end
@@ -0,0 +1,8 @@
1
+ @objc(RNBroadcastReceiver)
2
+ class RNBroadcastReceiver: NSObject {
3
+
4
+ @objc(multiply:withB:withResolver:withRejecter:)
5
+ func multiply(a: Float, b: Float, resolve:RCTPromiseResolveBlock,reject:RCTPromiseRejectBlock) -> Void {
6
+ resolve(a*b)
7
+ }
8
+ }