Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 8 additions & 7 deletions android/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,16 @@ def isNewArchitectureEnabled() {
apply plugin: "com.android.library"
apply plugin: "kotlin-android"


def appProject = rootProject.allprojects.find { it.plugins.hasPlugin('com.android.application') }

if (isNewArchitectureEnabled()) {
apply plugin: "com.facebook.react"
}

def safeExtGet(prop, fallback) {
rootProject.ext.has(prop) ? rootProject.ext.get(prop) : fallback
}

def getExtOrDefault(name) {
return rootProject.ext.has(name) ? rootProject.ext.get(name) : project.properties["CourierReactNative_" + name]
}
Expand Down Expand Up @@ -61,15 +64,15 @@ android {
}
}

compileSdkVersion getExtOrIntegerDefault("compileSdkVersion")
compileSdkVersion safeExtGet("compileSdkVersion", getExtOrIntegerDefault("compileSdkVersion"))

buildFeatures {
buildConfig true
}

defaultConfig {
minSdkVersion getExtOrIntegerDefault("minSdkVersion")
targetSdkVersion getExtOrIntegerDefault("targetSdkVersion")
minSdkVersion safeExtGet("minSdkVersion", getExtOrIntegerDefault("minSdkVersion"))
targetSdkVersion safeExtGet("targetSdkVersion", getExtOrIntegerDefault("targetSdkVersion"))
buildConfigField "boolean", "IS_NEW_ARCHITECTURE_ENABLED", isNewArchitectureEnabled().toString()
}

Expand Down Expand Up @@ -100,10 +103,8 @@ def kotlin_version = getExtOrDefault("kotlinVersion")

dependencies {

// For < 0.71, this will be from the local maven repo
// For > 0.71, this will be replaced by `com.facebook.react:react-android:$version` by react gradle plugin
//noinspection GradleDynamicVersion
implementation "com.facebook.react:react-android:0.73.7"
implementation "com.facebook.react:react-android:+"
implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"

// For converting to json
Expand Down
10 changes: 5 additions & 5 deletions android/gradle.properties
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
CourierReactNative_kotlinVersion=1.7.0
CourierReactNative_minSdkVersion=23
CourierReactNative_targetSdkVersion=31
CourierReactNative_compileSdkVersion=31
CourierReactNative_ndkversion=21.4.7075529
CourierReactNative_kotlinVersion=1.9.24
CourierReactNative_minSdkVersion=24
CourierReactNative_targetSdkVersion=34
CourierReactNative_compileSdkVersion=34
CourierReactNative_ndkversion=26.1.10909125
Original file line number Diff line number Diff line change
Expand Up @@ -77,11 +77,7 @@ class CourierClientModule(
return@launch
}

if (reactActivity == null) {
promise.rejectMissingContext()
return@launch
}

val context = reactApplicationContext
val courierDevice = device?.let {
CourierDevice(
appId = it.getString("appId"),
Expand All @@ -97,7 +93,7 @@ class CourierClientModule(
client.tokens.putUserToken(
token = token,
provider = provider,
device = courierDevice ?: CourierDevice.current(reactActivity!!)
device = courierDevice ?: CourierDevice.current(context)
)
promise.resolve(null)
} catch (e: Exception) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,8 @@ class CourierPreferencesViewManager : SimpleViewManager<CourierPreferences>() {

override fun createViewInstance(reactContext: ThemedReactContext): CourierPreferences {
themedReactContext = reactContext
val activity = reactContext.currentActivity as FragmentActivity
return CourierReactNativePreferencesView(activity)
val context = (reactContext.currentActivity as? FragmentActivity) ?: reactContext
return CourierReactNativePreferencesView(context)
}

@ReactProp(name = "onScrollPreferences")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ class CourierSystemModule(reactContext: ReactApplicationContext): ReactNativeMod

init {

// Listen to push notification events
Courier.shared.onPushNotificationEvent { event ->
when (event.trackingEvent) {
CLICKED -> postPushNotificationJavascriptEvent(CourierEvents.Push.CLICKED_EVENT, event.remoteMessage)
Expand All @@ -45,8 +44,8 @@ class CourierSystemModule(reactContext: ReactApplicationContext): ReactNativeMod

@ReactMethod
fun registerPushNotificationClickedOnKilledState() {
reactActivity?.let { activity ->
checkIntentForPushNotificationClick(activity.intent)
activity?.let { act ->
checkIntentForPushNotificationClick(act.intent)
}
}

Expand All @@ -66,8 +65,8 @@ class CourierSystemModule(reactContext: ReactApplicationContext): ReactNativeMod
@ReactMethod
fun requestNotificationPermission(promise: Promise) {

reactActivity?.let { activity ->
Courier.shared.requestNotificationPermission(activity)
activity?.let { act ->
Courier.shared.requestNotificationPermission(act)
}

promise.resolve("unknown")
Expand All @@ -77,9 +76,9 @@ class CourierSystemModule(reactContext: ReactApplicationContext): ReactNativeMod
@ReactMethod
fun getNotificationPermissionStatus(promise: Promise) {

reactActivity?.let { context ->
activity?.let { act ->

val isGranted = Courier.shared.isPushPermissionGranted(context)
val isGranted = Courier.shared.isPushPermissionGranted(act)
val status = if (isGranted) "authorized" else "denied"
promise.resolve(status)
return
Expand All @@ -92,7 +91,6 @@ class CourierSystemModule(reactContext: ReactApplicationContext): ReactNativeMod

@ReactMethod(isBlockingSynchronousMethod = true)
fun openSettingsForApp(): String? {
// TODO: Move this to the native package in the future
val context = reactApplicationContext
try {
val intent = Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package com.courierreactnative

import android.app.Activity
import com.courier.android.Courier
import com.facebook.react.ReactActivity
import com.facebook.react.bridge.Promise
import com.facebook.react.bridge.ReactApplicationContext
import com.facebook.react.bridge.ReactContextBaseJavaModule
Expand All @@ -10,14 +10,10 @@ abstract class ReactNativeModule(val tag: String, private val name: String, reac

override fun getName() = name

val reactActivity: ReactActivity? get() = currentActivity as? ReactActivity
val activity: Activity? get() = currentActivity

init {

// User Agent is used to ensure we know the SDK
// the requests come from
Courier.agent = Utils.COURIER_AGENT

}

internal fun Promise.rejectMissingContext() {
Expand Down
17 changes: 14 additions & 3 deletions src/Modules.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import {
NativeModules,
Platform,
UIManager,
TurboModuleRegistry,
requireNativeComponent,
} from 'react-native';

Expand All @@ -13,18 +14,28 @@ export class Modules {
'- You are not using Expo Go\n';

static readonly Client = Modules.getNativeModule(
'CourierClientModule',
NativeModules.CourierClientModule
);
static readonly Shared = Modules.getNativeModule(
'CourierSharedModule',
NativeModules.CourierSharedModule
);
static readonly System = Modules.getNativeModule(
'CourierSystemModule',
NativeModules.CourierSystemModule
);

static getNativeModule<T>(nativeModule: T | undefined): T {
return nativeModule
? nativeModule
static getNativeModule<T>(
moduleName: string,
bridgeModule: T | undefined
): T {
const resolved =
(TurboModuleRegistry?.get(moduleName) as T | null) ??
bridgeModule ??
undefined;
return resolved
? resolved
: (new Proxy(
{},
{
Expand Down
Loading