From 748ee510855a9131399be3c4d6e63f739489b90c Mon Sep 17 00:00:00 2001 From: Michael Miller Date: Thu, 29 Sep 2022 13:42:32 -0400 Subject: [PATCH] Log Listener and Getting Notification Status --- .../main/java/com/courier/android/Courier.kt | 15 ++++++++----- .../com/courier/android/CourierExtensions.kt | 19 +++++++++++----- .../courier/android/service/CourierService.kt | 22 +++++++++++++------ 3 files changed, 37 insertions(+), 19 deletions(-) diff --git a/android/src/main/java/com/courier/android/Courier.kt b/android/src/main/java/com/courier/android/Courier.kt index b666013..3b119a6 100644 --- a/android/src/main/java/com/courier/android/Courier.kt +++ b/android/src/main/java/com/courier/android/Courier.kt @@ -6,8 +6,6 @@ import com.courier.android.managers.UserManager import com.courier.android.models.CourierAgent import com.courier.android.models.CourierException import com.courier.android.models.CourierProvider -import com.courier.android.models.CourierPushEvent -import com.courier.android.repositories.MessagingRepository import com.courier.android.repositories.TokenRepository import com.courier.android.utils.NotificationEventBus import com.google.firebase.messaging.FirebaseMessaging @@ -22,7 +20,7 @@ class Courier private constructor() { companion object { var USER_AGENT = CourierAgent.NATIVE_ANDROID - internal const val VERSION = "1.0.14" + internal const val VERSION = "1.0.16" internal const val TAG = "Courier SDK" internal const val COURIER_PENDING_NOTIFICATION_KEY = "courier_pending_notification_key" internal val eventBus by lazy { NotificationEventBus() } @@ -31,8 +29,8 @@ class Courier private constructor() { /** * Initializes the SDK with a static reference to a Courier singleton - * This function must be called before you can use the Courier.instance value - * Courier.instance is required for nearly all features of the SDK + * This function must be called before you can use the Courier.shared value + * Courier.shared is required for nearly all features of the SDK */ fun initialize(context: Context) { if (mInstance == null) { @@ -42,7 +40,7 @@ class Courier private constructor() { } // This will not create a memory leak - // Please call Courier.initialize(context) before using Courier.instance + // Please call Courier.initialize(context) before using Courier.shared @SuppressLint("StaticFieldLeak") private var mInstance: Courier? = null val shared: Courier get() { @@ -83,6 +81,11 @@ class Courier private constructor() { */ val userId: String? get() = UserManager.getUserId(context) + /** + * Gets called if set and a log is posted + */ + var logListener: ((data: String) -> Unit)? = null + init { // Set app debugging diff --git a/android/src/main/java/com/courier/android/CourierExtensions.kt b/android/src/main/java/com/courier/android/CourierExtensions.kt index 3ddad14..b2594b5 100644 --- a/android/src/main/java/com/courier/android/CourierExtensions.kt +++ b/android/src/main/java/com/courier/android/CourierExtensions.kt @@ -22,12 +22,7 @@ import kotlin.coroutines.suspendCoroutine internal fun Courier.Companion.log(data: String) { if (shared.isDebugging) { Log.d(TAG, data) - } -} - -internal fun Courier.Companion.warn(data: String) { - if (shared.isDebugging) { - Log.e(TAG, data) + shared.logListener?.invoke(data) } } @@ -158,4 +153,16 @@ suspend fun AppCompatActivity.requestNotificationPermission() = suspendCoroutine requestNotificationPermission { granted -> continuation.resume(granted) } +} + +fun AppCompatActivity.getNotificationPermissionStatus(onStatusChange: (granted: Boolean) -> Unit) { + val notificationManagerCompat = NotificationManagerCompat.from(this) + val areNotificationsEnabled = notificationManagerCompat.areNotificationsEnabled() + onStatusChange(areNotificationsEnabled) +} + +suspend fun AppCompatActivity.getNotificationPermissionStatus() = suspendCoroutine { continuation -> + getNotificationPermissionStatus { granted -> + continuation.resume(granted) + } } \ No newline at end of file diff --git a/android/src/main/java/com/courier/android/service/CourierService.kt b/android/src/main/java/com/courier/android/service/CourierService.kt index 369e9bc..0d6ecd4 100644 --- a/android/src/main/java/com/courier/android/service/CourierService.kt +++ b/android/src/main/java/com/courier/android/service/CourierService.kt @@ -21,8 +21,6 @@ open class CourierService: FirebaseMessagingService() { override fun onMessageReceived(message: RemoteMessage) { super.onMessageReceived(message) - // Unlikely to fail, but in case it does - // we can still hit the show notification function try { // Track the event in Courier @@ -54,11 +52,21 @@ open class CourierService: FirebaseMessagingService() { override fun onNewToken(token: String) { super.onNewToken(token) - Courier.shared.setFCMToken( - token = token, - onSuccess = { Courier.log("Courier FCM token refreshed") }, - onFailure = { Courier.log(it.toString()) } - ) + + try { + + Courier.shared.setFCMToken( + token = token, + onSuccess = { Courier.log("Courier FCM token refreshed") }, + onFailure = { Courier.log(it.toString()) } + ) + + } catch (e: Exception) { + + Courier.log(e.toString()) + + } + } open fun showNotification(message: RemoteMessage) {