Commit cf48262f authored by Leonardo Aramaki's avatar Leonardo Aramaki

Add support to push messages not containing a an ejson field and thus having...

Add support to push messages not containing a an ejson field and thus having no more than a message an a title. This way, a simple notification should be shown and not attached to any real channel list of messages
parent ca99f416
......@@ -26,6 +26,7 @@ import chat.rocket.android.server.domain.siteName
import chat.rocket.android.server.ui.changeServerIntent
import chat.rocket.common.model.RoomType
import chat.rocket.common.model.roomTypeOf
import chat.rocket.common.util.ifNull
import com.squareup.moshi.Json
import com.squareup.moshi.Moshi
import kotlinx.coroutines.experimental.runBlocking
......@@ -48,7 +49,8 @@ class PushManager @Inject constructor(
private val getSettingsInteractor: GetSettingsInteractor,
private val context: Context
) {
private val randomizer = Random()
private val random = Random()
/**
* Handles a receiving push by creating and displaying an appropriate notification based
......@@ -59,7 +61,7 @@ class PushManager @Inject constructor(
val message = data["message"] as String?
val ejson = data["ejson"] as String?
val title = data["title"] as String?
val notId = data["notId"] as String? ?: randomizer.nextInt().toString()
val notId = data["notId"] as String? ?: random.nextInt().toString()
val image = data["image"] as String?
val style = data["style"] as String?
val summaryText = data["summaryText"] as String?
......@@ -67,9 +69,13 @@ class PushManager @Inject constructor(
try {
val adapter = moshi.adapter<PushInfo>(PushInfo::class.java)
val info = adapter.fromJson(ejson)
val pushMessage = PushMessage(title!!, message!!, info!!, image, count, notId, summaryText, style)
val pushMessage = if (ejson != null) {
val info = adapter.fromJson(ejson)
PushMessage(title!!, message!!, info!!, image, count, notId, summaryText, style)
} else {
PushMessage(title!!, message!!, PushInfo.EMPTY, image, count, notId, summaryText, style)
}
Timber.d("Received push message: $pushMessage")
......@@ -82,13 +88,17 @@ class PushManager @Inject constructor(
@SuppressLint("NewApi")
suspend fun showNotification(pushMessage: PushMessage) {
if (!hasAccount(pushMessage.info.host)) {
Timber.d("ignoring push message: $pushMessage")
val notId = pushMessage.notificationId.toInt()
val host = pushMessage.info.host
if (!hasAccount(host)) {
createSingleNotification(pushMessage)?.let {
NotificationManagerCompat.from(context).notify(notId, it)
}
Timber.d("ignoring push message: $pushMessage (maybe a test notification?)")
return
}
val notId = pushMessage.notificationId.toInt()
val host = pushMessage.info.host
val groupTuple = getGroupForHost(host)
groupTuple.second.incrementAndGet()
......@@ -103,7 +113,7 @@ class PushManager @Inject constructor(
val pushMessageList = groupedPushes.hostToPushMessageList[host]
notification?.let {
manager.notify(notId, notification)
manager.notify(notId, it)
}
pushMessageList?.let {
......@@ -182,7 +192,7 @@ class PushManager @Inject constructor(
if (style == null || "inbox" == style) {
val pushMessageList = groupedPushes.hostToPushMessageList.get(host)
pushMessageList?.let {
if (pushMessageList != null) {
val userMessages = pushMessageList.filter {
it.notificationId == pushMessage.notificationId
}
......@@ -206,6 +216,12 @@ class PushManager @Inject constructor(
.bigText(message.fromHtml())
builder.setStyle(bigTextStyle)
}
} else {
// We don't know which kind of push is this - maybe a test push, so just show it
val bigTextStyle = NotificationCompat.BigTextStyle()
.bigText(message.fromHtml())
builder.setStyle(bigTextStyle)
return builder.build()
}
} else {
val bigTextStyle = NotificationCompat.BigTextStyle()
......@@ -213,8 +229,7 @@ class PushManager @Inject constructor(
builder.setStyle(bigTextStyle)
}
return builder.addReplyAction(pushMessage)
.build()
return builder.addReplyAction(pushMessage).build()
}
}
......@@ -236,12 +251,23 @@ class PushManager @Inject constructor(
.setMessageNotification()
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
val channel = NotificationChannel(host, host, NotificationManager.IMPORTANCE_HIGH)
val channelId: String
val channelName: String
if (host.isEmpty()) {
builder.setContentIntent(deleteIntent)
channelName = "Test Notification"
channelId = "test-channel"
} else {
channelName = host
channelId = host
}
val channel = NotificationChannel(channelId, channelName, NotificationManager.IMPORTANCE_HIGH)
channel.lockscreenVisibility = Notification.VISIBILITY_PUBLIC
channel.enableLights(false)
channel.enableVibration(true)
channel.setShowBadge(true)
manager.createNotificationChannel(channel)
builder.setChannelId(channelId)
}
//TODO: Get Site_Name PublicSetting from cache
......@@ -276,7 +302,7 @@ class PushManager @Inject constructor(
/*if (!grouped) {
notificationIntent.putExtra(EXTRA_ROOM_ID, pushMessage.info.roomId)
}*/
return PendingIntent.getActivity(context, randomizer.nextInt(), notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT)
return PendingIntent.getActivity(context, random.nextInt(), notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT)
}
// CharSequence extensions
......@@ -318,14 +344,14 @@ class PushManager @Inject constructor(
return if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
PendingIntent.getBroadcast(
context,
randomizer.nextInt(),
random.nextInt(),
replyIntent,
PendingIntent.FLAG_UPDATE_CURRENT
)
} else {
PendingIntent.getActivity(
context,
randomizer.nextInt(),
random.nextInt(),
replyIntent,
PendingIntent.FLAG_UPDATE_CURRENT
)
......@@ -359,6 +385,7 @@ data class PushMessage(
val summaryText: String? = null,
val style: String? = null
) : Parcelable {
constructor(parcel: Parcel) : this(
parcel.readString(),
parcel.readString(),
......@@ -438,6 +465,9 @@ data class PushInfo @KotshiConstructor constructor(
}
companion object CREATOR : Parcelable.Creator<PushInfo> {
val EMPTY = PushInfo(hostname = "", roomId = "", type = RoomType.CHANNEL, name = "",
sender = null)
override fun createFromParcel(parcel: Parcel): PushInfo {
return PushInfo(parcel)
}
......
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