Commit 1c73d226 authored by Leonardo Aramaki's avatar Leonardo Aramaki

Separate code in funs

parent 75ba17fa
...@@ -9,6 +9,7 @@ import android.content.Context ...@@ -9,6 +9,7 @@ import android.content.Context
import android.content.Intent import android.content.Intent
import android.os.Build import android.os.Build
import android.os.Bundle import android.os.Bundle
import android.support.annotation.RequiresApi
import android.support.v4.app.NotificationCompat import android.support.v4.app.NotificationCompat
import android.support.v4.app.NotificationManagerCompat import android.support.v4.app.NotificationManagerCompat
import android.text.Html import android.text.Html
...@@ -21,16 +22,22 @@ object PushManager { ...@@ -21,16 +22,22 @@ object PushManager {
val messageStack = SparseArray<ArrayList<String>>() val messageStack = SparseArray<ArrayList<String>>()
fun handle(context: Context, data: Bundle) { fun handle(context: Context, data: Bundle) {
val now = System.currentTimeMillis()
val appContext = context.applicationContext val appContext = context.applicationContext
val message = data["message"] as String
val image = data["image"] as String
val ejson = data["ejson"] as String
val notificationId = data["notId"] as String val notificationId = data["notId"] as String
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 pushMessage = PushMessage(data["title"] as String,
data["message"] as String, message,
data["image"] as String, image,
data["ejson"] as String) ejson,
count,
notificationId,
summaryText,
style)
val res = appContext.resources val res = appContext.resources
...@@ -38,48 +45,43 @@ object PushManager { ...@@ -38,48 +45,43 @@ object PushManager {
stackMessage(notificationId.toInt(), pushMessage.message) stackMessage(notificationId.toInt(), pushMessage.message)
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
val notificationManager: NotificationManager = val notificationManager: NotificationManager =
context.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager context.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
with(pushMessage) { val notification: Notification
val channel = NotificationChannel(notificationId, sender.username, NotificationManager.IMPORTANCE_HIGH) if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
val notification = Notification.Builder(appContext, pushMessage.rid) notification = createNotificationForOreoAndAbove(appContext, pushMessage, smallIcon)
.setAutoCancel(true)
.setShowWhen(true)
.setWhen(now)
.setContentTitle(title.fromHtml())
.setContentText(message.fromHtml())
.setNumber(count.toInt())
.setSmallIcon(smallIcon)
.build()
channel.enableLights(true)
channel.enableVibration(true)
notificationManager.createNotificationChannel(channel)
notificationManager.notify(notificationId.toInt(), notification) notificationManager.notify(notificationId.toInt(), notification)
}
} else { } else {
notification = createCompatNotification(appContext, pushMessage, smallIcon)
NotificationManagerCompat.from(appContext).notify(notificationId.toInt(), notification)
}
}
fun clearStack(notificationId: Int) {
messageStack.delete(notificationId)
}
private fun createCompatNotification(context: Context, pushMessage: PushMessage, smallIcon: Int): Notification {
with(pushMessage) { with(pushMessage) {
val notificationBuilder = NotificationCompat.Builder(appContext) val notificationBuilder = NotificationCompat.Builder(context)
.setAutoCancel(true) .setAutoCancel(true)
.setShowWhen(true) .setShowWhen(true)
.setDefaults(Notification.DEFAULT_ALL) .setDefaults(Notification.DEFAULT_ALL)
.setWhen(now) .setWhen(createdAt)
.setContentTitle(title.fromHtml()) .setContentTitle(title.fromHtml())
.setContentText(message.fromHtml()) .setContentText(message.fromHtml())
.setNumber(count.toInt()) .setNumber(count.toInt())
.setSmallIcon(smallIcon) .setSmallIcon(smallIcon)
.setDeleteIntent(getDismissIntent(appContext, notificationId.toInt())) .setDeleteIntent(getDismissIntent(context, notificationId.toInt()))
if ("inbox" == style) { if ("inbox" == style) {
val messages = messageStack.get(notificationId.toInt()) val messages = chat.rocket.android.push.PushManager.messageStack.get(notificationId.toInt())
val messageCount = messages.size val messageCount = messages.size
if (messageCount > 1) { if (messageCount > 1) {
val summary = summaryText.replace("%n%", messageCount.toString()) val summary = summaryText.replace("%n%", messageCount.toString())
.fromHtml() .fromHtml()
val inbox = NotificationCompat.InboxStyle() val inbox = android.support.v4.app.NotificationCompat.InboxStyle()
.setBigContentTitle(title.fromHtml()) .setBigContentTitle(title.fromHtml())
.setSummaryText(summary) .setSummaryText(summary)
messages.forEach { msg -> messages.forEach { msg ->
...@@ -87,7 +89,7 @@ object PushManager { ...@@ -87,7 +89,7 @@ object PushManager {
} }
notificationBuilder.setStyle(inbox) notificationBuilder.setStyle(inbox)
} else { } else {
val bigText = NotificationCompat.BigTextStyle() val bigText = android.support.v4.app.NotificationCompat.BigTextStyle()
.bigText(message.fromHtml()) .bigText(message.fromHtml())
.setBigContentTitle(title.fromHtml()) .setBigContentTitle(title.fromHtml())
notificationBuilder.setStyle(bigText) notificationBuilder.setStyle(bigText)
...@@ -96,14 +98,32 @@ object PushManager { ...@@ -96,14 +98,32 @@ object PushManager {
notificationBuilder.setContentText(message.fromHtml()) notificationBuilder.setContentText(message.fromHtml())
} }
val notification = notificationBuilder.build() return notificationBuilder.build()
NotificationManagerCompat.from(appContext).notify(notificationId.toInt(), notification)
}
} }
} }
fun clearStack(notificationId: Int) { @RequiresApi(Build.VERSION_CODES.O)
messageStack.delete(notificationId) private fun createNotificationForOreoAndAbove(context: Context, pushMessage: PushMessage, smallIcon: Int): Notification {
val notificationManager: NotificationManager =
context.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
with(pushMessage) {
val channel = NotificationChannel(notificationId, sender.username, NotificationManager.IMPORTANCE_HIGH)
val notification = Notification.Builder(context, pushMessage.rid)
.setAutoCancel(true)
.setShowWhen(true)
.setWhen(createdAt)
.setContentTitle(title.fromHtml())
.setContentText(message.fromHtml())
.setNumber(count.toInt())
.setSmallIcon(smallIcon)
.build()
channel.enableLights(true)
channel.enableVibration(true)
notificationManager.createNotificationChannel(channel)
return notification
}
} }
private fun stackMessage(id: Int, message: String) { private fun stackMessage(id: Int, message: String) {
...@@ -124,12 +144,20 @@ object PushManager { ...@@ -124,12 +144,20 @@ object PushManager {
return PendingIntent.getBroadcast(context, notificationId, deleteIntent, 0) return PendingIntent.getBroadcast(context, notificationId, deleteIntent, 0)
} }
data class PushMessage(val title: String, val message: String, val image: String?, val ejson: String) { data class PushMessage(val title: String,
val message: String,
val image: String?,
val ejson: String,
val count: String,
val notificationId: String,
val summaryText: String,
val style: String) {
val host: String val host: String
val rid: String val rid: String
val type: String val type: String
val name: String? val name: String?
val sender: Sender val sender: Sender
val createdAt: Long
init { init {
val json = JSONObject(ejson) val json = JSONObject(ejson)
...@@ -138,6 +166,7 @@ object PushManager { ...@@ -138,6 +166,7 @@ object PushManager {
type = json.getString("type") type = json.getString("type")
name = json.optString("name") name = json.optString("name")
sender = Sender(json.getString("sender")) sender = Sender(json.getString("sender"))
createdAt = System.currentTimeMillis()
} }
data class Sender(val sender: String) { data class Sender(val sender: 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