Skip to content

Commit

Permalink
Pre-check for a valid notification manager before starting the foregr…
Browse files Browse the repository at this point in the history
…ound service

(cherry picked from commit baffa7b)
  • Loading branch information
dangeross committed Apr 15, 2024
1 parent 12b9ef2 commit 522080c
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import android.content.Context
import android.util.Log
import breez_sdk_notification.Constants.MESSAGE_TYPE_ADDRESS_TXS_CONFIRMED
import breez_sdk_notification.Constants.MESSAGE_TYPE_PAYMENT_RECEIVED
import breez_sdk_notification.NotificationHelper.Companion.getNotificationManager

@Suppress("unused")
interface MessagingService {
Expand All @@ -22,12 +23,13 @@ interface MessagingService {
/** Check if the foreground service is needed depending on the
* message type and foreground state of the application. */
fun startServiceIfNeeded(context: Context, message: Message) {
val notificationManager = getNotificationManager(context)
val isServiceNeeded = when (message.type) {
MESSAGE_TYPE_ADDRESS_TXS_CONFIRMED -> !isAppForeground(context)
MESSAGE_TYPE_PAYMENT_RECEIVED -> !isAppForeground(context)
else -> true
}
if (isServiceNeeded) startForegroundService(message)
if (notificationManager != null && isServiceNeeded) startForegroundService(message)
else Log.w(TAG, "Ignoring message ${message.type}: ${message.payload}")
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ class NotificationHelper {
private const val TAG = "NotificationHelper"
private var defaultClickAction: String? = null

private fun getNotificationManager(context: Context): NotificationManager? {
fun getNotificationManager(context: Context): NotificationManager? {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
val notificationManager =
context.getSystemService(Context.NOTIFICATION_SERVICE)
Expand All @@ -85,7 +85,7 @@ class NotificationHelper {
}

@SuppressLint("NewApi")
private fun createNotificationChannelGroup(
fun createNotificationChannelGroup(
context: Context,
groupId: String,
groupName: String,
Expand Down

0 comments on commit 522080c

Please sign in to comment.