improve sending error notifications (#2671)
* improve sending error notifications * rename draftNotification -> buildDraftNotification
This commit is contained in:
parent
edbc624625
commit
7a53bce439
3 changed files with 61 additions and 16 deletions
|
|
@ -1,5 +1,6 @@
|
|||
package com.keylesspalace.tusky.service
|
||||
|
||||
import android.app.Notification
|
||||
import android.app.NotificationChannel
|
||||
import android.app.NotificationManager
|
||||
import android.app.PendingIntent
|
||||
|
|
@ -12,9 +13,11 @@ import android.os.Build
|
|||
import android.os.IBinder
|
||||
import android.os.Parcelable
|
||||
import android.util.Log
|
||||
import androidx.annotation.StringRes
|
||||
import androidx.core.app.NotificationCompat
|
||||
import androidx.core.app.ServiceCompat
|
||||
import at.connyduck.calladapter.networkresult.fold
|
||||
import com.keylesspalace.tusky.MainActivity
|
||||
import com.keylesspalace.tusky.R
|
||||
import com.keylesspalace.tusky.appstore.EventHub
|
||||
import com.keylesspalace.tusky.appstore.StatusComposedEvent
|
||||
|
|
@ -184,14 +187,15 @@ class SendStatusService : Service(), Injectable {
|
|||
statusesToSend.remove(statusId)
|
||||
saveStatusToDrafts(statusToSend)
|
||||
|
||||
val builder = NotificationCompat.Builder(this@SendStatusService, CHANNEL_ID)
|
||||
.setSmallIcon(R.drawable.ic_notify)
|
||||
.setContentTitle(getString(R.string.send_post_notification_error_title))
|
||||
.setContentText(getString(R.string.send_post_notification_saved_content))
|
||||
.setColor(getColor(R.color.notification_color))
|
||||
val notification = buildDraftNotification(
|
||||
R.string.send_post_notification_error_title,
|
||||
R.string.send_post_notification_saved_content,
|
||||
statusToSend.accountId,
|
||||
statusId
|
||||
)
|
||||
|
||||
notificationManager.cancel(statusId)
|
||||
notificationManager.notify(errorNotificationId--, builder.build())
|
||||
notificationManager.notify(errorNotificationId--, notification)
|
||||
} else {
|
||||
// a network problem occurred, let's retry sending the status
|
||||
retrySending(statusId)
|
||||
|
|
@ -227,15 +231,18 @@ class SendStatusService : Service(), Injectable {
|
|||
|
||||
saveStatusToDrafts(statusToCancel)
|
||||
|
||||
val builder = NotificationCompat.Builder(this@SendStatusService, CHANNEL_ID)
|
||||
.setSmallIcon(R.drawable.ic_notify)
|
||||
.setContentTitle(getString(R.string.send_post_notification_cancel_title))
|
||||
.setContentText(getString(R.string.send_post_notification_saved_content))
|
||||
.setColor(getColor(R.color.notification_color))
|
||||
val notification = buildDraftNotification(
|
||||
R.string.send_post_notification_cancel_title,
|
||||
R.string.send_post_notification_saved_content,
|
||||
statusToCancel.accountId,
|
||||
statusId
|
||||
)
|
||||
|
||||
notificationManager.notify(statusId, builder.build())
|
||||
notificationManager.notify(statusId, notification)
|
||||
|
||||
delay(5000)
|
||||
|
||||
stopSelfWhenDone()
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -259,7 +266,41 @@ class SendStatusService : Service(), Injectable {
|
|||
private fun cancelSendingIntent(statusId: Int): PendingIntent {
|
||||
val intent = Intent(this, SendStatusService::class.java)
|
||||
intent.putExtra(KEY_CANCEL, statusId)
|
||||
return PendingIntent.getService(this, statusId, intent, NotificationHelper.pendingIntentFlags(false))
|
||||
return PendingIntent.getService(
|
||||
this,
|
||||
statusId,
|
||||
intent,
|
||||
NotificationHelper.pendingIntentFlags(false)
|
||||
)
|
||||
}
|
||||
|
||||
private fun buildDraftNotification(
|
||||
@StringRes title: Int,
|
||||
@StringRes content: Int,
|
||||
accountId: Long,
|
||||
statusId: Int
|
||||
): Notification {
|
||||
|
||||
val intent = Intent(this, MainActivity::class.java)
|
||||
intent.putExtra(NotificationHelper.ACCOUNT_ID, accountId)
|
||||
intent.putExtra(MainActivity.OPEN_DRAFTS, true)
|
||||
|
||||
val pendingIntent = PendingIntent.getActivity(
|
||||
this,
|
||||
statusId,
|
||||
intent,
|
||||
NotificationHelper.pendingIntentFlags(false)
|
||||
)
|
||||
|
||||
return NotificationCompat.Builder(this@SendStatusService, CHANNEL_ID)
|
||||
.setSmallIcon(R.drawable.ic_notify)
|
||||
.setContentTitle(getString(title))
|
||||
.setContentText(getString(content))
|
||||
.setColor(getColor(R.color.notification_color))
|
||||
.setAutoCancel(true)
|
||||
.setOngoing(false)
|
||||
.setContentIntent(pendingIntent)
|
||||
.build()
|
||||
}
|
||||
|
||||
override fun onDestroy() {
|
||||
|
|
@ -279,7 +320,6 @@ class SendStatusService : Service(), Injectable {
|
|||
private var sendingNotificationId = -1 // use negative ids to not clash with other notis
|
||||
private var errorNotificationId = Int.MIN_VALUE // use even more negative ids to not clash with other notis
|
||||
|
||||
@JvmStatic
|
||||
fun sendStatusIntent(
|
||||
context: Context,
|
||||
statusToSend: StatusToSend
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue