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