Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,9 @@
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
<receiver
android:name=".service.NotificationActionReceiver"
android:exported="false" />
</application>

</manifest>
Original file line number Diff line number Diff line change
Expand Up @@ -229,5 +229,6 @@ internal object NotificationSupport {
object ID {
const val FOREGROUND = -1
const val GROUPED = -2
const val GROUP_SUMMARY = -5
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
package com.github.gotify.service

import android.app.Notification
import android.app.NotificationManager
import android.content.BroadcastReceiver
import android.content.Context
import android.content.Intent
import com.github.gotify.NotificationSupport
import com.github.gotify.Settings
import com.github.gotify.Utils
import com.github.gotify.api.Callback
import com.github.gotify.api.ClientFactory
import com.github.gotify.client.api.MessageApi
import org.tinylog.kotlin.Logger

internal class NotificationActionReceiver : BroadcastReceiver() {
override fun onReceive(context: Context, intent: Intent) {
val messageId = intent.getLongExtra(EXTRA_MESSAGE_ID, INVALID_MESSAGE_ID)
if (messageId <= 0) {
Logger.warn("Ignoring delete notification action with invalid message id")
return
}

val settings = Settings(context)
if (!settings.tokenExists()) {
Logger.warn("Cannot delete notification because no client token is configured")
return
}

val pendingResult = goAsync()
ClientFactory.clientToken(settings)
.createService(MessageApi::class.java)
.deleteMessage(messageId)
.enqueue(
Callback.call(
onSuccess = {
try {
val notificationManager =
context.getSystemService(
Context.NOTIFICATION_SERVICE
) as NotificationManager
notificationManager.cancel(Utils.longToInt(messageId))
val hasMessageNotifications =
notificationManager.activeNotifications.any { statusBarNotification ->
val notification = statusBarNotification.notification
notification.group == NotificationSupport.Group.MESSAGES &&
(notification.flags and
Notification.FLAG_GROUP_SUMMARY) == 0
}
if (!hasMessageNotifications) {
notificationManager.cancel(
NotificationSupport.ID.GROUP_SUMMARY
)
}
} finally {
pendingResult.finish()
}
},
onError = {
pendingResult.finish()
}
)
)
}

companion object {
const val EXTRA_MESSAGE_ID = "com.github.gotify.MESSAGE_ID"
private const val INVALID_MESSAGE_ID = -1L
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -420,6 +420,22 @@ internal class WebSocketService : Service() {
b.setContentText(newMessage ?: message)
b.setStyle(NotificationCompat.BigTextStyle().bigText(formattedMessage))

if (id > 0) {
val deleteIntent = Intent(this, NotificationActionReceiver::class.java)
.putExtra(NotificationActionReceiver.EXTRA_MESSAGE_ID, id)
val deletePendingIntent = PendingIntent.getBroadcast(
this,
Utils.longToInt(id),
deleteIntent,
PendingIntent.FLAG_UPDATE_CURRENT or PendingIntent.FLAG_IMMUTABLE
)
b.addAction(
R.drawable.ic_delete,
getString(R.string.delete_message),
deletePendingIntent
)
}

val notificationImageUrl = Extras.getNestedValue(
String::class.java,
extras,
Expand Down Expand Up @@ -470,6 +486,6 @@ internal class WebSocketService : Service() {
.setContentIntent(contentIntent)

val notificationManager = this.getSystemService(NOTIFICATION_SERVICE) as NotificationManager
notificationManager.notify(-5, builder.build())
notificationManager.notify(NotificationSupport.ID.GROUP_SUMMARY, builder.build())
}
}