Skip to content

Commit

Permalink
Remove broken 'hideNotification' for now
Browse files Browse the repository at this point in the history
Signed-off-by: mueller-ma <[email protected]>
  • Loading branch information
mueller-ma committed May 27, 2024
1 parent 66d9c13 commit 4762d7a
Show file tree
Hide file tree
Showing 6 changed files with 10 additions and 58 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,6 @@ object NotificationPoller {

newMessages.forEach { message ->
notifHelper.showNotification(
message.id.hashCode(),
message,
null,
null
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,12 +40,8 @@ object CloudMessagingHelper {
}
}

fun onNotificationSelected(context: Context, intent: Intent) {
val notificationId = intent.getIntExtra(NotificationHelper.EXTRA_NOTIFICATION_ID, -1)
if (notificationId >= 0) {
FcmRegistrationWorker.scheduleHideNotification(context, notificationId)
}
}
@Suppress("UNUSED_PARAMETER")
fun onNotificationSelected(context: Context, intent: Intent) {}

fun isPollingBuild() = false

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@ class FcmMessageListenerService : FirebaseMessagingService() {
val data = message.data
Log.d(TAG, "onMessageReceived with data $data")
val messageType = data["type"] ?: return
val notificationId = data["notificationId"]?.toInt() ?: 1

when (messageType) {
"notification" -> {
Expand All @@ -55,20 +54,15 @@ class FcmMessageListenerService : FirebaseMessagingService() {
)

runBlocking {
val context = this@FcmMessageListenerService
notifHelper.showNotification(
notificationId,
cloudNotification,
FcmRegistrationWorker.createHideNotificationIntent(context, notificationId),
FcmRegistrationWorker.createHideNotificationIntent(
context,
NotificationHelper.SUMMARY_NOTIFICATION_ID
)
null,
null
)
}
}
"hideNotification" -> {
notifHelper.cancelNotification(notificationId)
notifHelper.cancelNotification(data["persistedId"].orEmpty().hashCode())
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ import androidx.work.WorkManager
import androidx.work.WorkRequest
import androidx.work.WorkerParameters
import com.google.firebase.messaging.FirebaseMessaging
import com.google.firebase.messaging.RemoteMessage
import java.io.IOException
import java.net.URLEncoder
import java.util.Locale
Expand Down Expand Up @@ -100,13 +99,6 @@ class FcmRegistrationWorker(private val context: Context, params: WorkerParamete
return retryOrFail()
}
}
ACTION_HIDE_NOTIFICATION -> {
val id = inputData.getInt(KEY_NOTIFICATION_ID, -1)
if (id >= 0) {
sendHideNotificationRequest(id, connection.messagingSenderId)
return Result.success()
}
}
else -> Log.e(TAG, "Invalid action '$action'")
}

Expand Down Expand Up @@ -142,15 +134,6 @@ class FcmRegistrationWorker(private val context: Context, params: WorkerParamete
}
}

private fun sendHideNotificationRequest(notificationId: Int, senderId: String) {
val fcm = FirebaseMessaging.getInstance()
val message = RemoteMessage.Builder("$senderId@gcm.googleapis.com")
.addData("type", "hideNotification")
.addData("notificationId", notificationId.toString())
.build()
fcm.send(message)
}

class ProxyReceiver : BroadcastReceiver() {
override fun onReceive(context: Context, intent: Intent) {
val actual = intent.parcelable<Intent>("intent") ?: return
Expand Down Expand Up @@ -181,7 +164,6 @@ class FcmRegistrationWorker(private val context: Context, params: WorkerParamete
private val TAG = FcmRegistrationWorker::class.java.simpleName

private const val ACTION_REGISTER = "org.openhab.habdroid.action.REGISTER_GCM"
private const val ACTION_HIDE_NOTIFICATION = "org.openhab.habdroid.action.HIDE_NOTIFICATION"
private const val KEY_ACTION = "action"
private const val KEY_NOTIFICATION_ID = "notificationId"

Expand All @@ -193,23 +175,6 @@ class FcmRegistrationWorker(private val context: Context, params: WorkerParamete
enqueueFcmWorker(context, data)
}

internal fun scheduleHideNotification(context: Context, notificationId: Int) {
val data = Data.Builder()
.putString(KEY_ACTION, ACTION_HIDE_NOTIFICATION)
.putInt(KEY_NOTIFICATION_ID, notificationId)
.build()

enqueueFcmWorker(context, data)
}

internal fun createHideNotificationIntent(context: Context, notificationId: Int): PendingIntent {
val intent = Intent(context, FcmRegistrationWorker::class.java)
.setAction(ACTION_HIDE_NOTIFICATION)
.putExtra(KEY_NOTIFICATION_ID, notificationId)

return ProxyReceiver.wrap(context, intent, notificationId)
}

private fun enqueueFcmWorker(context: Context, data: Data) {
val constraints = Constraints.Builder()
.setRequiredNetworkType(NetworkType.CONNECTED)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,16 +46,15 @@ class NotificationHelper(private val context: Context) {
private val notificationManager = context.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager

suspend fun showNotification(
notificationId: Int,
message: CloudNotification,
deleteIntent: PendingIntent?,
summaryDeleteIntent: PendingIntent?
) {
createChannelForSeverity(message.severity)

val n = makeNotification(message, notificationId, deleteIntent)
val n = makeNotification(message, message.idHash, deleteIntent)

notificationManager.notify(notificationId, n)
notificationManager.notify(message.idHash, n)

if (HAS_GROUPING_SUPPORT) {
val count = countCloudNotifications(notificationManager.activeNotifications)
Expand Down Expand Up @@ -218,7 +217,6 @@ class NotificationHelper(private val context: Context) {
val contentIntent = Intent(context, MainActivity::class.java).apply {
action = MainActivity.ACTION_NOTIFICATION_SELECTED
flags = Intent.FLAG_ACTIVITY_SINGLE_TOP or Intent.FLAG_ACTIVITY_CLEAR_TOP
putExtra(EXTRA_NOTIFICATION_ID, notificationId)
putExtra(MainActivity.EXTRA_PERSISTED_NOTIFICATION_ID, persistedId)
}
return PendingIntent.getActivity(
Expand Down Expand Up @@ -249,8 +247,6 @@ class NotificationHelper(private val context: Context) {
"severity-$severity"
}

@Suppress("MemberVisibilityCanBePrivate") // Used in full flavor
internal const val EXTRA_NOTIFICATION_ID = "notificationId"
internal const val SUMMARY_NOTIFICATION_ID = 0

// Notification grouping is only available on N or higher, as mentioned in
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,9 @@ data class CloudNotification internal constructor(
val createdTimestamp: Long,
val icon: IconResource?,
val severity: String?
) : Parcelable
) : Parcelable {
val idHash get() = id.hashCode()
}

@Throws(JSONException::class)
fun JSONObject.toCloudNotification(): CloudNotification {
Expand Down

0 comments on commit 4762d7a

Please sign in to comment.