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

Commit

Permalink
fix: add more logs and tests
Browse files Browse the repository at this point in the history
  • Loading branch information
gtokman committed Apr 20, 2024
1 parent 6f8af7a commit 6a4b4c7
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 23 deletions.
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package com.candlefinance.push

import android.content.Intent
import android.os.Bundle
import android.util.Log
import com.facebook.react.HeadlessJsTaskService
import com.facebook.react.bridge.Arguments
Expand All @@ -15,7 +14,7 @@ class PushNotificationHeadlessTaskService : HeadlessJsTaskService() {
private val defaultTimeout: Long = 10000 // 10 seconds
override fun getTaskConfig(intent: Intent): HeadlessJsTaskConfig? {
return NotificationPayload.fromIntent(intent)?.let {
Log.d(TAG, "Starting headless task with payload: $it")
Log.d(TAG, "Starting headless task with payload: ${it.rawData}")
HeadlessJsTaskConfig(
HEADLESS_TASK_KEY,
it.toWritableMap(),
Expand Down Expand Up @@ -80,7 +79,7 @@ class FirebaseMessagingService : FirebaseMessagingService() {
payload.rawData.forEach { (key, value) ->
json.put(key, value)
}
serviceIntent.putExtra("data", json.toString())
serviceIntent.putExtra("NotificationPayload", json.toString())
if (baseContext.startService(serviceIntent) != null) {
HeadlessJsTaskService.acquireWakeLockNow(baseContext)
} else {
Expand Down
38 changes: 21 additions & 17 deletions android/src/main/java/com/candlefinance/push/NotificationUtils.kt
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package com.candlefinance.push

import android.annotation.SuppressLint
import android.app.ActivityManager
import android.app.NotificationChannel
import android.app.NotificationManager
import android.app.PendingIntent
Expand All @@ -14,15 +13,10 @@ import android.os.Parcelable
import android.util.Log
import androidx.annotation.ChecksSdkIntAtLeast
import androidx.core.app.NotificationCompat
import androidx.core.app.NotificationManagerCompat
import androidx.core.content.ContextCompat
import androidx.lifecycle.DefaultLifecycleObserver
import androidx.lifecycle.Lifecycle
import androidx.lifecycle.LifecycleObserver
import androidx.lifecycle.LifecycleOwner
import androidx.lifecycle.OnLifecycleEvent
import androidx.lifecycle.ProcessLifecycleOwner
import com.facebook.react.HeadlessJsTaskService
import com.facebook.react.bridge.Arguments
import com.facebook.react.bridge.WritableMap
import kotlinx.coroutines.CoroutineScope
Expand All @@ -31,6 +25,7 @@ import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
import kotlinx.parcelize.IgnoredOnParcel
import kotlinx.parcelize.Parcelize
import org.json.JSONObject
import java.net.URL

class PushNotificationsConstants {
Expand All @@ -41,6 +36,7 @@ class PushNotificationsConstants {
const val TITLE = "title" // title
const val BODY = "body" // body
const val SUBTITLE = "subtitle" // subtitle
const val PRIORITY = "priority" // priority
const val IMAGEURL = "imageUrl" // imageUrl
const val DEFAULT_NOTIFICATION_CHANNEL_ID = "default_notification_channel_id" // default_notification_channel_id
}
Expand Down Expand Up @@ -100,7 +96,7 @@ class PushNotificationsUtils(
payload: NotificationPayload,
targetClass: Class<*>?
) {
Log.d("PushNotificationsUtils", "Show notification with payload: ${payload.rawData}")
Log.d(Tag, "Show notification with payload: ${payload.rawData}")
CoroutineScope(Dispatchers.IO).launch {
val notificationManager = ContextCompat.getSystemService(context, NotificationManager::class.java)
val notificationBuilder = NotificationCompat.Builder(context, channelId)
Expand All @@ -110,12 +106,13 @@ class PushNotificationsUtils(
.setSmallIcon(getResourceIdByName("ic_default_notification", "drawable"))
.setContentTitle(notificationContent[PushNotificationsConstants.TITLE])
.setContentText(notificationContent[PushNotificationsConstants.BODY])
.setPriority(NotificationCompat.PRIORITY_HIGH)
.setSubText(notificationContent[PushNotificationsConstants.SUBTITLE])
.setPriority(notificationContent[PushNotificationsConstants.PRIORITY]?.toInt() ?: NotificationCompat.PRIORITY_DEFAULT)
.setAutoCancel(true)

Log.d("PushNotificationsUtils", "targetClass: $targetClass")
Log.d(Tag, "targetClass: $targetClass")
if (targetClass != null) {
Log.d("PushNotificationsUtils", "targetClass is not null")
Log.d(Tag, "targetClass is not null")
val intent = Intent(context, targetClass).apply {
flags = Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_CLEAR_TASK
val url = notificationContent[PushNotificationsConstants.URL]
Expand All @@ -129,18 +126,21 @@ class PushNotificationsUtils(
putExtra(PushNotificationsConstants.DEEPLINK, deepLink)
}
putExtra(PushNotificationsConstants.OPENAPP, true)
val json = JSONObject()
notificationContent.forEach { (key, value) -> json.put(key, value) }
putExtra("rawData", json.toString())
}

notificationBuilder.setContentIntent(
PendingIntent.getActivity(
context,
notificationId,
intent,
PendingIntent.FLAG_UPDATE_CURRENT
PendingIntent.FLAG_UPDATE_CURRENT or if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) PendingIntent.FLAG_IMMUTABLE else 0
)
)
} else {
Log.e("PushNotificationsUtils", "targetClass is null")
Log.e(Tag, "targetClass is null")
}

if (notificationContent.containsKey(PushNotificationsConstants.IMAGEURL)) {
Expand All @@ -160,37 +160,41 @@ class PushNotificationsUtils(
}
}

private const val Tag = "PushNotificationUtils"

class PushNotificationUtils(context: Context) {
private val utils = PushNotificationsUtils(context)
private val lifecycleObserver = AppLifecycleListener()

init {
if (context is LifecycleOwner) {
Log.d("PushNotificationUtils", "Add lifecycle observer to context")
Log.d(Tag, "Add lifecycle observer to context")
context.lifecycle.addObserver(lifecycleObserver)
} else {
Log.e("PushNotificationUtils", "Context is not a lifecycle owner")
Log.e(Tag, "Context is not a lifecycle owner")
ProcessLifecycleOwner.get().lifecycle.addObserver(lifecycleObserver)
}
}

fun showNotification(
payload: NotificationPayload
) {
Log.d("PushNotificationUtils", "Show notification with payload: $payload")
Log.d(Tag, "Show notification with payload: $payload")

val notificationId = (System.currentTimeMillis() % Int.MAX_VALUE).toInt()
val targetClass = payload.rawData["targetClass"]?.let {
Log.d("PushNotificationUtils", "targetClass: $it")
Log.d(Tag, "targetClass: $it")
try {
Class.forName(it)
} catch (e: ClassNotFoundException) {
Log.e("PushNotificationUtils", "Class not found: $it")
Log.e(Tag, "Class not found: $it")
null
}
}
if (targetClass != null) {
utils.showNotification(notificationId, payload, targetClass)
} else {
Log.e(Tag, "targetClass is null")
}
}

Expand Down
6 changes: 4 additions & 2 deletions android/src/main/java/com/candlefinance/push/PushModule.kt
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,7 @@ import android.content.Context
import android.content.Context.MODE_PRIVATE
import android.content.Intent
import android.content.pm.PackageManager
import android.net.Uri
import android.os.Build
import android.provider.Settings
import android.util.Log
import androidx.core.app.ActivityCompat
import androidx.core.content.ContextCompat
Expand Down Expand Up @@ -172,6 +170,8 @@ class PushModule(
PushNotificationEventManager.sendEvent(
PushNotificationEventType.NOTIFICATION_OPENED, payload.toWritableMap()
)
} else {
Log.d(TAG, "No notification payload found in intent")
}
}

Expand Down Expand Up @@ -208,6 +208,8 @@ class PushModule(
PushNotificationEventType.LAUNCH_NOTIFICATION_OPENED,
payload.toWritableMap()
)
} else {
Log.d(TAG, "No launch notification found in intent")
}
}
} else {
Expand Down
1 change: 0 additions & 1 deletion src/apis/addMessageEventListener.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ export const addMessageEventListener = (
) => void
): EmitterSubscription =>
nativeEventEmitter.addListener(event, (nativeMessage: NativeMessage) => {
console.log('nativeMessage', nativeMessage);
listener(
normalizeNativeMessage(nativeMessage),
nativeMessage.completionHandlerId
Expand Down

0 comments on commit 6a4b4c7

Please sign in to comment.