From d18414c53e5b59d6d421a1d86339320210c087b2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Miko=C5=82aj=20Pich?= Date: Wed, 15 May 2024 12:33:58 +0200 Subject: [PATCH] Don't turn isSubscribed flag off when user disable app notifications --- .../java/com/pushpushgo/sdk/PushPushGo.kt | 24 +++++++++++++++---- .../pushpushgo/sdk/network/ApiRepository.kt | 16 ++++--------- .../pushpushgo/sdk/push/NotificationUtils.kt | 10 +++++++- .../sdk/push/PushNotificationDelegate.kt | 10 ++++---- .../service/FcmMessagingServiceDelegate.kt | 2 +- .../service/HmsMessagingServiceDelegate.kt | 2 +- .../pushpushgo/sdk/utils/PushTokenUtils.kt | 8 ++++--- .../com/pushpushgo/sdk/work/UploadDelegate.kt | 15 ++++++++++-- 8 files changed, 59 insertions(+), 28 deletions(-) diff --git a/library/src/main/java/com/pushpushgo/sdk/PushPushGo.kt b/library/src/main/java/com/pushpushgo/sdk/PushPushGo.kt index 2c60bca..e1b2a22 100644 --- a/library/src/main/java/com/pushpushgo/sdk/PushPushGo.kt +++ b/library/src/main/java/com/pushpushgo/sdk/PushPushGo.kt @@ -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 @@ -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") } @@ -283,7 +293,7 @@ class PushPushGo private constructor( */ fun createSubscriber(): ListenableFuture { return CoroutineScope(Job() + Dispatchers.IO).future { - check(areNotificationsEnabled(application)) { + check(areNotificationsEnabled()) { "Notifications disabled! Subscriber registration canceled" } @@ -318,7 +328,7 @@ class PushPushGo private constructor( */ fun migrateToNewProject(newProjectId: String, newProjectToken: String): ListenableFuture { return CoroutineScope(Job() + Dispatchers.IO).future { - check(areNotificationsEnabled(application)) { + check(areNotificationsEnabled()) { "Notifications disabled! Subscriber registration canceled" } @@ -339,6 +349,10 @@ class PushPushGo private constructor( } } + internal fun areNotificationsEnabled(): Boolean { + return areNotificationsEnabled(application) + } + /** * function to construct and send beacon */ diff --git a/library/src/main/java/com/pushpushgo/sdk/network/ApiRepository.kt b/library/src/main/java/com/pushpushgo/sdk/network/ApiRepository.kt index 0b04cbb..a44d9df 100644 --- a/library/src/main/java/com/pushpushgo/sdk/network/ApiRepository.kt +++ b/library/src/main/java/com/pushpushgo/sdk/network/ApiRepository.kt @@ -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 @@ -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( @@ -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) { @@ -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, diff --git a/library/src/main/java/com/pushpushgo/sdk/push/NotificationUtils.kt b/library/src/main/java/com/pushpushgo/sdk/push/NotificationUtils.kt index 699b329..d3c8f01 100644 --- a/library/src/main/java/com/pushpushgo/sdk/push/NotificationUtils.kt +++ b/library/src/main/java/com/pushpushgo/sdk/push/NotificationUtils.kt @@ -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) { diff --git a/library/src/main/java/com/pushpushgo/sdk/push/PushNotificationDelegate.kt b/library/src/main/java/com/pushpushgo/sdk/push/PushNotificationDelegate.kt index b27ffe6..1b782cb 100644 --- a/library/src/main/java/com/pushpushgo/sdk/push/PushNotificationDelegate.kt +++ b/library/src/main/java/com/pushpushgo/sdk/push/PushNotificationDelegate.kt @@ -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 @@ -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) @@ -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() { diff --git a/library/src/main/java/com/pushpushgo/sdk/push/service/FcmMessagingServiceDelegate.kt b/library/src/main/java/com/pushpushgo/sdk/push/service/FcmMessagingServiceDelegate.kt index 169e1b6..e07542a 100644 --- a/library/src/main/java/com/pushpushgo/sdk/push/service/FcmMessagingServiceDelegate.kt +++ b/library/src/main/java/com/pushpushgo/sdk/push/service/FcmMessagingServiceDelegate.kt @@ -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( diff --git a/library/src/main/java/com/pushpushgo/sdk/push/service/HmsMessagingServiceDelegate.kt b/library/src/main/java/com/pushpushgo/sdk/push/service/HmsMessagingServiceDelegate.kt index 5966dbf..9432bc8 100644 --- a/library/src/main/java/com/pushpushgo/sdk/push/service/HmsMessagingServiceDelegate.kt +++ b/library/src/main/java/com/pushpushgo/sdk/push/service/HmsMessagingServiceDelegate.kt @@ -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( diff --git a/library/src/main/java/com/pushpushgo/sdk/utils/PushTokenUtils.kt b/library/src/main/java/com/pushpushgo/sdk/utils/PushTokenUtils.kt index 591edbb..f88dd73 100644 --- a/library/src/main/java/com/pushpushgo/sdk/utils/PushTokenUtils.kt +++ b/library/src/main/java/com/pushpushgo/sdk/utils/PushTokenUtils.kt @@ -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 -> diff --git a/library/src/main/java/com/pushpushgo/sdk/work/UploadDelegate.kt b/library/src/main/java/com/pushpushgo/sdk/work/UploadDelegate.kt index 5640cf3..acdd6ee 100644 --- a/library/src/main/java/com/pushpushgo/sdk/work/UploadDelegate.kt +++ b/library/src/main/java/com/pushpushgo/sdk/work/UploadDelegate.kt @@ -21,7 +21,7 @@ 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") } } @@ -29,11 +29,22 @@ internal class UploadDelegate { 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()) }