mirror of
https://github.com/sudoxnym/habitica-android.git
synced 2026-04-14 11:46:32 +00:00
Improve push notification handling
# Conflicts: # Habitica/src/main/java/com/habitrpg/android/habitica/helpers/notifications/HabiticaFirebaseMessagingService.kt # Habitica/src/main/java/com/habitrpg/android/habitica/helpers/notifications/PushNotificationManager.kt # version.properties
This commit is contained in:
parent
adfe3ac280
commit
8fe9de1223
2 changed files with 48 additions and 43 deletions
|
|
@ -16,12 +16,15 @@ class HabiticaFirebaseMessagingService : FirebaseMessagingService() {
|
||||||
internal lateinit var pushNotificationManager: PushNotificationManager
|
internal lateinit var pushNotificationManager: PushNotificationManager
|
||||||
|
|
||||||
override fun onMessageReceived(remoteMessage: RemoteMessage) {
|
override fun onMessageReceived(remoteMessage: RemoteMessage) {
|
||||||
userComponent?.inject(this)
|
try {
|
||||||
if (this::pushNotificationManager.isInitialized) {
|
userComponent?.inject(this)
|
||||||
pushNotificationManager.displayNotification(remoteMessage)
|
} catch (_: java.lang.IllegalStateException) {
|
||||||
|
}
|
||||||
|
PushNotificationManager.displayNotification(remoteMessage, applicationContext)
|
||||||
|
|
||||||
if (remoteMessage.data["identifier"]?.contains(PushNotificationManager.WON_CHALLENGE_PUSH_NOTIFICATION_KEY) == true) {
|
if (remoteMessage.data["identifier"]?.contains(PushNotificationManager.WON_CHALLENGE_PUSH_NOTIFICATION_KEY) == true) {
|
||||||
// userRepository.retrieveUser(true).subscribe({}, ExceptionHandler.rx())
|
if (this::userRepository.isInitialized) {
|
||||||
|
// userRepository.retrieveUser(true).subscribe({}, RxErrorHandler.handleEmptyError())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -34,24 +34,28 @@ class PushNotificationManager(
|
||||||
}
|
}
|
||||||
|
|
||||||
fun addPushDeviceUsingStoredToken() {
|
fun addPushDeviceUsingStoredToken() {
|
||||||
FirebaseMessaging.getInstance().token.addOnCompleteListener {
|
if (refreshedToken.isNotBlank()) {
|
||||||
if (!it.isSuccessful) {
|
addRefreshToken()
|
||||||
return@addOnCompleteListener
|
} 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<String, String>()
|
|
||||||
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<String, String>()
|
||||||
|
pushDeviceData["regId"] = this.refreshedToken
|
||||||
|
pushDeviceData["type"] = "android"
|
||||||
|
apiClient.addPushDevice(pushDeviceData).subscribe({ }, ExceptionHandler.rx())
|
||||||
|
}
|
||||||
|
|
||||||
fun removePushDeviceUsingStoredToken() {
|
fun removePushDeviceUsingStoredToken() {
|
||||||
if (this.refreshedToken.isEmpty()) {
|
if (this.refreshedToken.isEmpty() || !userHasPushDevice()) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
apiClient.deletePushDevice(this.refreshedToken).subscribe({ }, ExceptionHandler.rx())
|
apiClient.deletePushDevice(this.refreshedToken).subscribe({ }, ExceptionHandler.rx())
|
||||||
|
|
@ -66,31 +70,6 @@ class PushNotificationManager(
|
||||||
return this.user?.pushDevices == null
|
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<String, Any>()
|
|
||||||
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 {
|
private fun userIsSubscribedToNotificationType(type: String?): Boolean {
|
||||||
val key = when {
|
val key = when {
|
||||||
type == PARTY_INVITE_PUSH_NOTIFICATION_KEY -> "preference_push_invited_to_party"
|
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 GROUP_ACTIVITY_NOTIFICATION_KEY = "groupActivity"
|
||||||
const val G1G1_PROMO_KEY = "g1g1Promo"
|
const val G1G1_PROMO_KEY = "g1g1Promo"
|
||||||
private const val DEVICE_TOKEN_PREFERENCE_KEY = "device-token-preference"
|
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<String, Any>()
|
||||||
|
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)
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue