Skip to content
This repository has been archived by the owner on Sep 14, 2024. It is now read-only.

feat: Firebase setup - Added Android native method for requesting permissions and fetching device tokens. #3

Merged
merged 14 commits into from
Feb 23, 2024

Conversation

vijaygojiya
Copy link
Contributor

No description provided.

Copy link
Member

@gtokman gtokman left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice! thanks for this, few comments.

Last part is the listeners: https://reactnative.dev/docs/native-modules-android#sending-events-to-javascript

android/src/main/java/com/candlefinance/push/PushModule.kt Outdated Show resolved Hide resolved
android/src/main/java/com/candlefinance/push/PushModule.kt Outdated Show resolved Hide resolved
example/src/App.tsx Outdated Show resolved Hide resolved
src/index.tsx Outdated Show resolved Hide resolved
src/index.tsx Outdated Show resolved Hide resolved
Copy link
Member

@gtokman gtokman left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice 🚀

@gtokman gtokman marked this pull request as ready for review February 16, 2024 23:14
@gtokman gtokman force-pushed the feat/androdi_support branch from ce28712 to 78d1235 Compare February 16, 2024 23:38
fix: add new auth method and update readme
@gtokman gtokman force-pushed the feat/androdi_support branch from a28ff16 to f400bce Compare February 17, 2024 00:08
@gtokman
Copy link
Member

gtokman commented Feb 17, 2024

#1 adds the first part; now, all that is left is the emitter part (https://reactnative.dev/docs/native-modules-android#sending-events-to-javascript)

@vijaygojiya Let me know if you want to tackle that. I'm imagining something like this:

class PushPackage : ReactPackage {
  
  data class Action (val type: String, val payload: Map<String, Any?>)

  private var isInitialized = false
  private val queue = mutableListOf<Action>()
  private var module: PushModule? = null

  override fun createNativeModules(reactContext: ReactApplicationContext): List<NativeModule> {
    module = PushModule(reactContext)

    module?.onInitialized = {
      isInitialized = true
      queue.forEach {
        module?.dispatch(it.type, it.payload)
      }
      queue.clear()
    }
    
    return listOf(module as NativeModule)

  }

  override fun createViewManagers(reactContext: ReactApplicationContext): List<ViewManager<*, *>> {
    return emptyList()
  }

  fun dispatch(action: String, payload: Map<String, Any?>) {
    if (isInitialized) {
      module?.dispatch(action, payload)
    } else {
      queue.add(Action(action, payload))
    }
  }
}

// in PushModule

 companion object {
    const val notificationReceived = "notificationReceived"
    const val deviceTokenReceived = "deviceTokenReceived"
    const val errorReceived = "errorReceived"
  }

  override fun getConstants() = mapOf(
         ...
  )
  
 // Example method
  // See https://reactnative.dev/docs/native-modules-android
  @ReactMethod
  fun addListener(eventName: String?) {
    // Keep: Required for RN built in Event Emitter Calls.
  }

  @ReactMethod
  fun removeListeners(count: Int?) {
    // Keep: Required for RN built in Event Emitter Calls.
  }

  fun dispatch(action: String, payload: Map<String, Any?>) {
    val map = mapOf(
        "type" to action,
        "payload" to payload
      )
      val event: WritableMap = Arguments.makeNativeMap(map)
      reactApplicationContext.getJSModule(DeviceEventManagerModule.RCTDeviceEventEmitter::class.java)
        .emit("...", event)
  }

@vijaygojiya
Copy link
Contributor Author

#1 adds the first part; now, all that is left is the emitter part (https://reactnative.dev/docs/native-modules-android#sending-events-to-javascript)

@vijaygojiya Let me know if you want to tackle that. I'm imagining something like this:

class PushPackage : ReactPackage {
  
  data class Action (val type: String, val payload: Map<String, Any?>)

  private var isInitialized = false
  private val queue = mutableListOf<Action>()
  private var module: PushModule? = null

  override fun createNativeModules(reactContext: ReactApplicationContext): List<NativeModule> {
    module = PushModule(reactContext)

    module?.onInitialized = {
      isInitialized = true
      queue.forEach {
        module?.dispatch(it.type, it.payload)
      }
      queue.clear()
    }
    
    return listOf(module as NativeModule)

  }

  override fun createViewManagers(reactContext: ReactApplicationContext): List<ViewManager<*, *>> {
    return emptyList()
  }

  fun dispatch(action: String, payload: Map<String, Any?>) {
    if (isInitialized) {
      module?.dispatch(action, payload)
    } else {
      queue.add(Action(action, payload))
    }
  }
}

// in PushModule

 companion object {
    const val notificationReceived = "notificationReceived"
    const val deviceTokenReceived = "deviceTokenReceived"
    const val errorReceived = "errorReceived"
  }

  override fun getConstants() = mapOf(
         ...
  )
  
 // Example method
  // See https://reactnative.dev/docs/native-modules-android
  @ReactMethod
  fun addListener(eventName: String?) {
    // Keep: Required for RN built in Event Emitter Calls.
  }

  @ReactMethod
  fun removeListeners(count: Int?) {
    // Keep: Required for RN built in Event Emitter Calls.
  }

  fun dispatch(action: String, payload: Map<String, Any?>) {
    val map = mapOf(
        "type" to action,
        "payload" to payload
      )
      val event: WritableMap = Arguments.makeNativeMap(map)
      reactApplicationContext.getJSModule(DeviceEventManagerModule.RCTDeviceEventEmitter::class.java)
        .emit("...", event)
  }

Yes, I will take care of this today. Thanks

@vijaygojiya
Copy link
Contributor Author

@gtokman Could you please review my implementation for the events emitter?. also need help on understanding event queue and dispatching event actions.

@gtokman gtokman requested a review from nikwotton February 18, 2024 00:54
Comment on lines 15 to 17
private val CHANNEL_ID = "RNFireDefaultChannelID"
private val CHANNEL_NAME = "Firebase Default"
private val NOTIFICATION_ID = 123321
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

#? do we need to inject these from the JS side? If so, we might need to add an init function. I have ideas but never done Android push.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  • Will do more R& D on it and improve.

Comment on lines 36 to 38
.setSmallIcon(getResourceIdByName("ic_launcher","mipmap"))
.setContentTitle(title)
.setContentText(message)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

#? do we need to inject this, too? Is this like the app icon shown in the push?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Essentially, we now require an icon when sending notifications, and I've also introduced a feature to easily change this icon. Developers can customize the notification icon by adding a new asset to the 'drawable' folder with the exact name ic_default_notification.

src/index.tsx Show resolved Hide resolved
android/src/main/java/com/candlefinance/push/PushModule.kt Outdated Show resolved Hide resolved
android/src/main/java/com/candlefinance/push/PushModule.kt Outdated Show resolved Hide resolved
@gtokman
Copy link
Member

gtokman commented Feb 18, 2024

@gtokman Could you please review my implementation for the events emitter?. also need help on understanding event queue and dispatching event actions.

We probably don't need the queue, but you are good with the RNEmitter singleton you made regarding the dispatching events. After responding to my feedback, I will prob have to make a demo app on Firebase and see if this works.

@gtokman gtokman merged commit f642ee8 into candlefinance:main Feb 23, 2024
4 of 5 checks passed
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants