Skip to content

Commit

Permalink
Merge pull request #173 from Notificare/feature/SDK-556-subscription-…
Browse files Browse the repository at this point in the history
…id-event

Add onSubscriptionIdChanged event
  • Loading branch information
hpinhal authored Jul 26, 2024
2 parents 67714a6 + 05aaae9 commit f55532b
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,13 @@ import re.notifica.push.ktx.INTENT_ACTION_ACTION_OPENED
import re.notifica.push.ktx.INTENT_ACTION_LIVE_ACTIVITY_UPDATE
import re.notifica.push.ktx.INTENT_ACTION_NOTIFICATION_OPENED
import re.notifica.push.ktx.INTENT_ACTION_NOTIFICATION_RECEIVED
import re.notifica.push.ktx.INTENT_ACTION_SUBSCRIPTION_ID_CHANGED
import re.notifica.push.ktx.INTENT_ACTION_SYSTEM_NOTIFICATION_RECEIVED
import re.notifica.push.ktx.INTENT_ACTION_TOKEN_CHANGED
import re.notifica.push.ktx.INTENT_ACTION_UNKNOWN_NOTIFICATION_RECEIVED
import re.notifica.push.ktx.INTENT_EXTRA_DELIVERY_MECHANISM
import re.notifica.push.ktx.INTENT_EXTRA_LIVE_ACTIVITY_UPDATE
import re.notifica.push.ktx.INTENT_EXTRA_SUBSCRIPTION_ID
import re.notifica.push.ktx.INTENT_EXTRA_TOKEN
import re.notifica.push.models.NotificareLiveActivityUpdate
import re.notifica.push.models.NotificareNotificationDeliveryMechanism
Expand All @@ -25,11 +27,16 @@ import re.notifica.push.models.NotificareUnknownNotification
public open class NotificarePushIntentReceiver : BroadcastReceiver() {
override fun onReceive(context: Context, intent: Intent) {
when (intent.action) {
Notificare.INTENT_ACTION_SUBSCRIPTION_ID_CHANGED -> {
val subscriptionId = intent.getStringExtra(Notificare.INTENT_EXTRA_SUBSCRIPTION_ID)
onSubscriptionIdChanged(context, subscriptionId)
}
Notificare.INTENT_ACTION_TOKEN_CHANGED -> {
val token: String = requireNotNull(
intent.getStringExtra(Notificare.INTENT_EXTRA_TOKEN)
)

@Suppress("DEPRECATION")
onTokenChanged(context, token)
}
Notificare.INTENT_ACTION_NOTIFICATION_RECEIVED -> {
Expand Down Expand Up @@ -85,6 +92,16 @@ public open class NotificarePushIntentReceiver : BroadcastReceiver() {
}
}

protected open fun onSubscriptionIdChanged(context: Context, subscriptionId: String?) {
NotificareLogger.debug(
"The subscription id changed, please override onSubscriptionIdChanged if you want to receive these intents."
)
}

@Deprecated(
message = "Use onSubscriptionIdChanged() instead.",
replaceWith = ReplaceWith("onSubscriptionIdChanged(context, subscriptionId)")
)
protected open fun onTokenChanged(context: Context, token: String) {
NotificareLogger.debug(
"The push token changed, please override onTokenChanged if you want to receive these intents."
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,12 +68,14 @@ import re.notifica.push.ktx.INTENT_ACTION_NOTIFICATION_OPENED
import re.notifica.push.ktx.INTENT_ACTION_NOTIFICATION_RECEIVED
import re.notifica.push.ktx.INTENT_ACTION_QUICK_RESPONSE
import re.notifica.push.ktx.INTENT_ACTION_REMOTE_MESSAGE_OPENED
import re.notifica.push.ktx.INTENT_ACTION_SUBSCRIPTION_ID_CHANGED
import re.notifica.push.ktx.INTENT_ACTION_SYSTEM_NOTIFICATION_RECEIVED
import re.notifica.push.ktx.INTENT_ACTION_TOKEN_CHANGED
import re.notifica.push.ktx.INTENT_ACTION_UNKNOWN_NOTIFICATION_RECEIVED
import re.notifica.push.ktx.INTENT_EXTRA_DELIVERY_MECHANISM
import re.notifica.push.ktx.INTENT_EXTRA_LIVE_ACTIVITY_UPDATE
import re.notifica.push.ktx.INTENT_EXTRA_REMOTE_MESSAGE
import re.notifica.push.ktx.INTENT_EXTRA_SUBSCRIPTION_ID
import re.notifica.push.ktx.INTENT_EXTRA_TEXT_RESPONSE
import re.notifica.push.ktx.INTENT_EXTRA_TOKEN
import re.notifica.push.ktx.logNotificationInfluenced
Expand Down Expand Up @@ -928,8 +930,12 @@ internal object NotificarePushImpl : NotificareModule(), NotificarePush, Notific
this@NotificarePushImpl.subscriptionId = token
this@NotificarePushImpl.allowedUI = allowedUI

// TODO: The TOKEN_CHANGED intent does not support null tokens.
// This requires a new intent or a breaking change in the current one.
Notificare.requireContext().sendBroadcast(
Intent(Notificare.requireContext(), intentReceiver)
.setAction(Notificare.INTENT_ACTION_SUBSCRIPTION_ID_CHANGED)
.putExtra(Notificare.INTENT_EXTRA_SUBSCRIPTION_ID, token)
)

if (token != null && previousSubscriptionId != token) {
Notificare.requireContext().sendBroadcast(
Intent(Notificare.requireContext(), intentReceiver)
Expand Down
6 changes: 6 additions & 0 deletions notificare-push/src/main/java/re/notifica/push/ktx/Augment.kt
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ internal fun Notificare.eventsInternal(): NotificareInternalEventsModule {

// region Intent actions

public val Notificare.INTENT_ACTION_SUBSCRIPTION_ID_CHANGED: String
get() = "re.notifica.intent.action.SubscriptionIdChanged"

public val Notificare.INTENT_ACTION_TOKEN_CHANGED: String
get() = "re.notifica.intent.action.TokenChanged"

Expand Down Expand Up @@ -48,6 +51,9 @@ public val Notificare.INTENT_ACTION_LIVE_ACTIVITY_UPDATE: String

// region Intent extras

public val Notificare.INTENT_EXTRA_SUBSCRIPTION_ID: String
get() = "re.notifica.intent.extra.SubscriptionId"

public val Notificare.INTENT_EXTRA_TOKEN: String
get() = "re.notifica.intent.extra.Token"

Expand Down

0 comments on commit f55532b

Please sign in to comment.