Skip to content

Commit

Permalink
Don't turn isSubscribed flag off when user disable app notifications
Browse files Browse the repository at this point in the history
  • Loading branch information
mklkj committed May 15, 2024
1 parent feaa962 commit d18414c
Show file tree
Hide file tree
Showing 8 changed files with 59 additions and 28 deletions.
24 changes: 19 additions & 5 deletions library/src/main/java/com/pushpushgo/sdk/PushPushGo.kt
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,18 @@ import com.pushpushgo.sdk.di.NetworkModule
import com.pushpushgo.sdk.di.WorkModule
import com.pushpushgo.sdk.dto.PPGoNotification
import com.pushpushgo.sdk.exception.PushPushException
import com.pushpushgo.sdk.push.*
import com.pushpushgo.sdk.utils.*
import com.pushpushgo.sdk.push.PushNotificationDelegate
import com.pushpushgo.sdk.push.areNotificationsEnabled
import com.pushpushgo.sdk.push.createNotificationChannel
import com.pushpushgo.sdk.push.deserializeNotificationData
import com.pushpushgo.sdk.push.handleNotificationLinkClick
import com.pushpushgo.sdk.utils.getPlatformPushToken
import com.pushpushgo.sdk.utils.getPlatformType
import com.pushpushgo.sdk.utils.logDebug
import com.pushpushgo.sdk.utils.logError
import com.pushpushgo.sdk.utils.mapToBundle
import com.pushpushgo.sdk.utils.validateApiKey
import com.pushpushgo.sdk.utils.validateProjectId
import com.pushpushgo.sdk.work.UploadDelegate
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
Expand Down Expand Up @@ -269,7 +279,7 @@ class PushPushGo private constructor(
* function to register subscriber
*/
fun registerSubscriber() {
if (!areNotificationsEnabled(application)) {
if (!areNotificationsEnabled()) {
return logError("Notifications disabled! Subscriber registration canceled")
}

Expand All @@ -283,7 +293,7 @@ class PushPushGo private constructor(
*/
fun createSubscriber(): ListenableFuture<String> {
return CoroutineScope(Job() + Dispatchers.IO).future {
check(areNotificationsEnabled(application)) {
check(areNotificationsEnabled()) {
"Notifications disabled! Subscriber registration canceled"
}

Expand Down Expand Up @@ -318,7 +328,7 @@ class PushPushGo private constructor(
*/
fun migrateToNewProject(newProjectId: String, newProjectToken: String): ListenableFuture<PushPushGo> {
return CoroutineScope(Job() + Dispatchers.IO).future {
check(areNotificationsEnabled(application)) {
check(areNotificationsEnabled()) {
"Notifications disabled! Subscriber registration canceled"
}

Expand All @@ -339,6 +349,10 @@ class PushPushGo private constructor(
}
}

internal fun areNotificationsEnabled(): Boolean {
return areNotificationsEnabled(application)
}

/**
* function to construct and send beacon
*/
Expand Down
16 changes: 5 additions & 11 deletions library/src/main/java/com/pushpushgo/sdk/network/ApiRepository.kt
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,6 @@ import com.pushpushgo.sdk.network.data.TokenRequest
import com.pushpushgo.sdk.utils.getPlatformPushToken
import com.pushpushgo.sdk.utils.logDebug
import com.pushpushgo.sdk.utils.logError
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.withContext
import okhttp3.MediaType.Companion.toMediaTypeOrNull
import okhttp3.RequestBody.Companion.toRequestBody

Expand All @@ -26,9 +24,10 @@ internal class ApiRepository(

suspend fun registerToken(token: String?, apiKey: String = this.apiKey, projectId: String = this.projectId) {
logDebug("registerToken invoked: $token")
val tokenToRegister = token ?: sharedPref.lastToken.takeIf { it.isNotEmpty() } ?: withContext(Dispatchers.IO) {
getPlatformPushToken(context)
}
val tokenToRegister = token
?: sharedPref.lastToken.takeIf { it.isNotEmpty() }
?: getPlatformPushToken(context)

logDebug("Token to register: $tokenToRegister")

val data = apiService.registerSubscriber(
Expand All @@ -52,7 +51,7 @@ internal class ApiRepository(
subscriberId = sharedPref.subscriberId,
)
sharedPref.subscriberId = ""
sharedPref.isSubscribed = false
sharedPref.isSubscribed = isSubscribed
}

suspend fun unregisterSubscriber(projectId: String, token: String, subscriberId: String) {
Expand Down Expand Up @@ -97,11 +96,6 @@ internal class ApiRepository(
}

suspend fun sendBeacon(beacon: String) {
if (!sharedPref.isSubscribed) {
logDebug("Beacon not sent. Reason: not subscribed")
return
}

apiService.sendBeacon(
token = apiKey,
projectId = projectId,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,15 @@ internal fun handleNotificationLinkClick(context: Context, uri: String) {
}

internal fun areNotificationsEnabled(context: Context): Boolean {
return NotificationManagerCompat.from(context).areNotificationsEnabled()
val notificationManager = NotificationManagerCompat.from(context)

if (!notificationManager.areNotificationsEnabled()) return false
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.O) return true

val channelName = context.getString(R.string.pushpushgo_notification_default_channel_id)
val channel = notificationManager.getNotificationChannel(channelName)

return channel?.importance != NotificationManagerCompat.IMPORTANCE_NONE
}

internal fun createNotificationChannel(context: Context) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.pushpushgo.sdk.push

import android.annotation.SuppressLint
import android.app.Notification
import android.app.PendingIntent
import android.content.Context
Expand Down Expand Up @@ -43,6 +44,7 @@ internal class PushNotificationDelegate {
} else processPushMessage(pushMessage, context)
}

@SuppressLint("MissingPermission")
private fun processPushMessage(pushMessage: PushMessage, context: Context) {
val notificationManager = NotificationManagerCompat.from(context)

Expand Down Expand Up @@ -73,12 +75,12 @@ internal class PushNotificationDelegate {
}
}

fun onNewToken(token: String, isSubscribed: Boolean) {
fun onNewToken(token: String) {
logDebug("Refreshed token: $token")
if (!PushPushGo.isInitialized()) return
if (!PushPushGo.getInstance().areNotificationsEnabled()) return logDebug("Notifications are disabled. Skipping")

if (PushPushGo.isInitialized() && isSubscribed) {
PushPushGo.getInstance().getUploadManager().sendRegister(token)
}
PushPushGo.getInstance().getUploadManager().sendRegister(token)
}

fun onDestroy() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ class FcmMessagingServiceDelegate(private val context: Context) {

fun onNewToken(token: String) {
preferencesHelper.lastFCMToken = token
delegate.onNewToken(token, preferencesHelper.isSubscribed)
delegate.onNewToken(token)
}

private fun RemoteMessage.toPushMessage() = PushMessage(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ class HmsMessagingServiceDelegate(private val context: Context) {

fun onNewToken(token: String) {
preferencesHelper.lastHCMToken = token
delegate.onNewToken(token, preferencesHelper.isSubscribed)
delegate.onNewToken(token)
}

private fun RemoteMessage.toPushMessage() = PushMessage(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,11 @@ import kotlinx.coroutines.withContext
import kotlin.coroutines.resumeWithException
import kotlin.coroutines.suspendCoroutine

internal suspend fun getPlatformPushToken(context: Context) = when (getPlatformType()) {
PlatformType.FCM -> getFcmPushToken()
PlatformType.HCM -> getHcmPushToken(context)
internal suspend fun getPlatformPushToken(context: Context) = withContext(Dispatchers.IO) {
when (getPlatformType()) {
PlatformType.FCM -> getFcmPushToken()
PlatformType.HCM -> getHcmPushToken(context)
}
}

private suspend fun getFcmPushToken() = suspendCoroutine { cont ->
Expand Down
15 changes: 13 additions & 2 deletions library/src/main/java/com/pushpushgo/sdk/work/UploadDelegate.kt
Original file line number Diff line number Diff line change
Expand Up @@ -21,19 +21,30 @@ internal class UploadDelegate {
with(PushPushGo.getInstance().getNetwork()) {
when (type) {
UploadWorker.REGISTER -> registerToken(data)
UploadWorker.UNREGISTER -> unregisterSubscriber()
UploadWorker.UNREGISTER -> unregisterSubscriber(isSubscribed = false)
else -> logDebug("Unknown upload data type")
}
}
}

fun sendEvent(type: EventType, buttonId: Int, campaign: String, projectId: String?, subscriberId: String?) {
uploadScope.launch(errorHandler) {
PushPushGo.getInstance().getNetwork().sendEvent(type, buttonId, campaign, projectId, subscriberId)
PushPushGo.getInstance().getNetwork().sendEvent(
type = type,
buttonId = buttonId,
campaign = campaign,
project = projectId,
subscriber = subscriberId,
)
}
}

fun sendBeacon(beacon: JSONObject) {
if (!PushPushGo.getInstance().areNotificationsEnabled()) {
logDebug("Beacon not sent. Reason: notifications disabled")
return
}

uploadScope.launch(errorHandler) {
PushPushGo.getInstance().getNetwork().sendBeacon(beacon.toString())
}
Expand Down

0 comments on commit d18414c

Please sign in to comment.