diff --git a/Habitica/src/main/java/com/habitrpg/android/habitica/helpers/notifications/HabiticaFirebaseMessagingService.kt b/Habitica/src/main/java/com/habitrpg/android/habitica/helpers/notifications/HabiticaFirebaseMessagingService.kt index 99ea9d490..32cc85184 100644 --- a/Habitica/src/main/java/com/habitrpg/android/habitica/helpers/notifications/HabiticaFirebaseMessagingService.kt +++ b/Habitica/src/main/java/com/habitrpg/android/habitica/helpers/notifications/HabiticaFirebaseMessagingService.kt @@ -16,12 +16,15 @@ class HabiticaFirebaseMessagingService : FirebaseMessagingService() { internal lateinit var pushNotificationManager: PushNotificationManager override fun onMessageReceived(remoteMessage: RemoteMessage) { - userComponent?.inject(this) - if (this::pushNotificationManager.isInitialized) { - pushNotificationManager.displayNotification(remoteMessage) + try { + userComponent?.inject(this) + } catch (_: java.lang.IllegalStateException) { + } + PushNotificationManager.displayNotification(remoteMessage, applicationContext) - if (remoteMessage.data["identifier"]?.contains(PushNotificationManager.WON_CHALLENGE_PUSH_NOTIFICATION_KEY) == true) { - // userRepository.retrieveUser(true).subscribe({}, ExceptionHandler.rx()) + if (remoteMessage.data["identifier"]?.contains(PushNotificationManager.WON_CHALLENGE_PUSH_NOTIFICATION_KEY) == true) { + if (this::userRepository.isInitialized) { + // userRepository.retrieveUser(true).subscribe({}, RxErrorHandler.handleEmptyError()) } } } diff --git a/Habitica/src/main/java/com/habitrpg/android/habitica/helpers/notifications/PushNotificationManager.kt b/Habitica/src/main/java/com/habitrpg/android/habitica/helpers/notifications/PushNotificationManager.kt index 05d8a5c94..8fac75a74 100644 --- a/Habitica/src/main/java/com/habitrpg/android/habitica/helpers/notifications/PushNotificationManager.kt +++ b/Habitica/src/main/java/com/habitrpg/android/habitica/helpers/notifications/PushNotificationManager.kt @@ -34,24 +34,28 @@ class PushNotificationManager( } fun addPushDeviceUsingStoredToken() { - FirebaseMessaging.getInstance().token.addOnCompleteListener { - if (!it.isSuccessful) { - return@addOnCompleteListener + if (refreshedToken.isNotBlank()) { + addRefreshToken() + } else { + FirebaseMessaging.getInstance().token.addOnCompleteListener { + refreshedToken = it.result + addRefreshToken() } - this.refreshedToken = it.result - if (this.refreshedToken.isEmpty() || this.user == null || this.userHasPushDevice() || !this.userIsSubscribedToNotifications()) { - return@addOnCompleteListener - } - - val pushDeviceData = HashMap() - pushDeviceData["regId"] = this.refreshedToken - pushDeviceData["type"] = "android" - apiClient.addPushDevice(pushDeviceData).subscribe({ }, ExceptionHandler.rx()) } } + private fun addRefreshToken() { + if (this.refreshedToken.isEmpty() || this.user == null || this.userHasPushDevice()) { + return + } + val pushDeviceData = HashMap() + pushDeviceData["regId"] = this.refreshedToken + pushDeviceData["type"] = "android" + apiClient.addPushDevice(pushDeviceData).subscribe({ }, ExceptionHandler.rx()) + } + fun removePushDeviceUsingStoredToken() { - if (this.refreshedToken.isEmpty()) { + if (this.refreshedToken.isEmpty() || !userHasPushDevice()) { return } apiClient.deletePushDevice(this.refreshedToken).subscribe({ }, ExceptionHandler.rx()) @@ -66,31 +70,6 @@ class PushNotificationManager( return this.user?.pushDevices == null } - fun displayNotification(remoteMessage: RemoteMessage) { - val remoteMessageIdentifier = remoteMessage.data["identifier"] - - val notificationFactory = HabiticaLocalNotificationFactory() - val notification = notificationFactory.build(remoteMessageIdentifier, context) - if (userIsSubscribedToNotificationType(remoteMessageIdentifier)) { - if (remoteMessage.data.containsKey("sendAnalytics")) { - val additionalData = HashMap() - additionalData["identifier"] = remoteMessageIdentifier ?: "" - AmplitudeManager.sendEvent( - "receive notification", - AmplitudeManager.EVENT_CATEGORY_BEHAVIOUR, - AmplitudeManager.EVENT_HITTYPE_EVENT, - additionalData - ) - } - notification.setExtras(remoteMessage.data) - notification.notifyLocally(remoteMessage.data["title"], remoteMessage.data["body"], remoteMessage.data) - } - } - - private fun userIsSubscribedToNotifications(): Boolean { - return sharedPreferences.getBoolean("pushNotifications", true) - } - private fun userIsSubscribedToNotificationType(type: String?): Boolean { val key = when { type == PARTY_INVITE_PUSH_NOTIFICATION_KEY -> "preference_push_invited_to_party" @@ -122,5 +101,28 @@ class PushNotificationManager( const val GROUP_ACTIVITY_NOTIFICATION_KEY = "groupActivity" const val G1G1_PROMO_KEY = "g1g1Promo" private const val DEVICE_TOKEN_PREFERENCE_KEY = "device-token-preference" + + fun displayNotification(remoteMessage: RemoteMessage, context: Context, pushNotificationManager: PushNotificationManager? = null) { + val remoteMessageIdentifier = remoteMessage.data["identifier"] + + val notificationFactory = HabiticaLocalNotificationFactory() + val notification = notificationFactory.build(remoteMessageIdentifier, + context + ) + if (pushNotificationManager?.userIsSubscribedToNotificationType(remoteMessageIdentifier) != false) { + if (remoteMessage.data.containsKey("sendAnalytics")) { + val additionalData = HashMap() + additionalData["identifier"] = remoteMessageIdentifier ?: "" + AmplitudeManager.sendEvent( + "receive notification", + AmplitudeManager.EVENT_CATEGORY_BEHAVIOUR, + AmplitudeManager.EVENT_HITTYPE_EVENT, + additionalData + ) + } + notification.setExtras(remoteMessage.data) + notification.notifyLocally(remoteMessage.data["title"], remoteMessage.data["body"], remoteMessage.data) + } + } } }