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

Commit

Permalink
fix: android runtime crash
Browse files Browse the repository at this point in the history
  • Loading branch information
gtokman committed Feb 23, 2024
1 parent 1fd9c12 commit f700831
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 12 deletions.
4 changes: 2 additions & 2 deletions android/src/main/java/com/candlefinance/push/ContextHolder.kt
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@ class ContextHolder private constructor() {
}
}

fun getApplicationContext(): ReactContext {
fun getApplicationContext(): ReactContext? {
if (!::applicationContext.isInitialized) {
throw IllegalStateException("Application context not initialized. Call setApplicationContext() first.")
return null
}
return applicationContext
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,12 @@ class FirebaseMessagingService : FirebaseMessagingService() {

// Send the event
RNEventEmitter.sendEvent(notificationReceived, formattedData.toString())
NotificationUtils
.sendNotification(
ContextHolder.getInstance().getApplicationContext(), title, body
)
ContextHolder.getInstance().getApplicationContext()?.let {
NotificationUtils
.sendNotification(
it, title, body
)
}
if (remoteMessage.data.isNotEmpty()) {
Log.d(TAG, "Message data payload: ${remoteMessage.data}")
}
Expand All @@ -58,7 +60,7 @@ class FirebaseMessagingService : FirebaseMessagingService() {
// TODO: make this actually work
private fun getAppState(): String {
val reactContext = ContextHolder.getInstance().getApplicationContext()
val currentActivity = reactContext.currentActivity
val currentActivity = reactContext?.currentActivity
return if (currentActivity == null) {
"background"
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,12 @@ object NotificationUtils {
synchronized(ResourceUtils::class.java) {

val context = ContextHolder.getInstance().getApplicationContext()
val packageName = context.packageName
return context.resources.getIdentifier(name, type, packageName)
val packageName = context?.packageName
if (context != null) {
return context.resources.getIdentifier(name, type, packageName)
} else {
return -1
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,7 @@ object RNEventEmitter {
fun sendEvent(eventName: String?, eventMap: String?) {
val reactContext = ContextHolder.getInstance().getApplicationContext()
try {
reactContext
.getJSModule(DeviceEventManagerModule.RCTDeviceEventEmitter::class.java)
.emit(eventName!!, eventMap)
reactContext?.getJSModule(DeviceEventManagerModule.RCTDeviceEventEmitter::class.java)?.emit(eventName!!, eventMap)
} catch (e: Exception) {
Log.e("SEND_EVENT", "", e)
}
Expand Down

0 comments on commit f700831

Please sign in to comment.