Commit fd798148 authored by Leonardo Aramaki's avatar Leonardo Aramaki

Minor refactoring

parent 76e7a2db
...@@ -42,14 +42,8 @@ object PushManager { ...@@ -42,14 +42,8 @@ object PushManager {
val style = data["style"] as String val style = data["style"] as String
val summaryText = data["summaryText"] as String val summaryText = data["summaryText"] as String
val count = data["count"] as String val count = data["count"] as String
val pushMessage = PushMessage(data["title"] as String, val title = data["title"] as String
message, val pushMessage = PushMessage(title, message, image, ejson, count, notificationId, summaryText, style)
image,
ejson,
count,
notificationId,
summaryText,
style)
// We should use Timber here // We should use Timber here
if (BuildConfig.DEBUG) { if (BuildConfig.DEBUG) {
...@@ -65,6 +59,39 @@ object PushManager { ...@@ -65,6 +59,39 @@ object PushManager {
val notificationManager: NotificationManager = val notificationManager: NotificationManager =
context.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager context.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
val groupNotification = createGroupNotification(context, pushMessage, data, smallIcon)
val groupId = groupMap.get(pushMessage.host)
val notification: Notification
if (isAndroidVersionAtLeast(Build.VERSION_CODES.O)) {
notification = createNotificationForOreoAndAbove(appContext, pushMessage, smallIcon, data)
if (groupId != null) {
notificationManager.notify(groupId, notification)
}
notificationManager.notify(notificationId.toInt(), notification)
} else {
notification = createCompatNotification(appContext, pushMessage, smallIcon, data)
if (groupId != null) {
NotificationManagerCompat.from(appContext).notify(groupId, groupNotification)
}
NotificationManagerCompat.from(appContext).notify(notificationId.toInt(), notification)
}
}
private fun isAndroidVersionAtLeast(minVersion: Int) = Build.VERSION.SDK_INT >= minVersion
private fun addGroupToBundle(host: String) {
val size = groupMap.size
if (groupMap.get(host) == null) {
groupMap.put(host, size + 1)
}
}
fun clearMessageStack(notificationId: Int) {
messageStack.delete(notificationId)
}
private fun createGroupNotification(context: Context, pushMessage: PushMessage, data: Bundle, smallIcon: Int): Notification {
// Create notification group. // Create notification group.
addGroupToBundle(pushMessage.host) addGroupToBundle(pushMessage.host)
val id = pushMessage.notificationId.toInt() val id = pushMessage.notificationId.toInt()
...@@ -89,26 +116,7 @@ object PushManager { ...@@ -89,26 +116,7 @@ object PushManager {
notGroupBuilder.setSubText(subText) notGroupBuilder.setSubText(subText)
} }
val notification: Notification return notGroupBuilder.build()
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
notification = createNotificationForOreoAndAbove(appContext, pushMessage, smallIcon, data)
notificationManager.notify(notificationId.toInt(), notification)
} else {
notification = createCompatNotification(appContext, pushMessage, smallIcon, data)
NotificationManagerCompat.from(appContext).notify(groupMap.get(pushMessage.host)!!, notGroupBuilder.build())
NotificationManagerCompat.from(appContext).notify(notificationId.toInt(), notification)
}
}
private fun addGroupToBundle(host: String) {
val size = groupMap.size
if (groupMap.get(host) == null) {
groupMap.put(host, size + 1)
}
}
fun clearMessageStack(notificationId: Int) {
messageStack.delete(notificationId)
} }
private fun createCompatNotification(context: Context, pushMessage: PushMessage, smallIcon: Int, data: Bundle): Notification { private fun createCompatNotification(context: Context, pushMessage: PushMessage, smallIcon: Int, data: Bundle): Notification {
...@@ -176,22 +184,27 @@ object PushManager { ...@@ -176,22 +184,27 @@ object PushManager {
val deleteIntent = getDismissIntent(context, id) val deleteIntent = getDismissIntent(context, id)
val channel = NotificationChannel(notificationId, sender.username, NotificationManager.IMPORTANCE_HIGH) val channel = NotificationChannel(notificationId, sender.username, NotificationManager.IMPORTANCE_HIGH)
val notification = Notification.Builder(context, pushMessage.rid) val notificationBuilder = Notification.Builder(context, pushMessage.rid)
.setAutoCancel(true) .setAutoCancel(true)
.setShowWhen(true) .setShowWhen(true)
.setWhen(createdAt) .setWhen(createdAt)
.setContentTitle(title.fromHtml()) .setContentTitle(title.fromHtml())
.setContentText(message.fromHtml()) .setContentText(message.fromHtml())
.setNumber(count.toInt()) .setNumber(count.toInt())
.setGroup(host)
.setSmallIcon(smallIcon) .setSmallIcon(smallIcon)
.setDeleteIntent(deleteIntent) .setDeleteIntent(deleteIntent)
.setContentIntent(contentIntent) .setContentIntent(contentIntent)
.build()
val subText = RocketChatCache(context).getHostSiteName(pushMessage.host)
if (subText.isNotEmpty()) {
notificationBuilder.setSubText(subText)
}
channel.enableLights(true) channel.enableLights(true)
channel.enableVibration(true) channel.enableVibration(true)
notificationManager.createNotificationChannel(channel) notificationManager.createNotificationChannel(channel)
return notification return notificationBuilder.build()
} }
} }
...@@ -224,7 +237,7 @@ object PushManager { ...@@ -224,7 +237,7 @@ object PushManager {
return PendingIntent.getActivity(context, randomizer.nextInt(), notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT) return PendingIntent.getActivity(context, randomizer.nextInt(), notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT)
} }
data class PushMessage(val title: String, private data class PushMessage(val title: String,
val message: String, val message: String,
val image: String?, val image: String?,
val ejson: String, val ejson: String,
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment