Use failed reasons

This commit is contained in:
sim 2024-11-11 16:35:00 +00:00
parent 3655b28496
commit 544bb2438e
4 changed files with 33 additions and 10 deletions

View file

@ -44,12 +44,13 @@ class BroadcastReceiver : android.content.BroadcastReceiver() {
Log.d(TAG, "REGISTER received for app $appId (connectorToken=$connectorToken)")
if (!repository.getUnifiedPushEnabled()) {
Log.w(TAG, "Refusing registration because 'EnableUP' is disabled")
distributor.sendRegistrationFailed(appId, connectorToken, "UnifiedPush is disabled in ntfy")
// Action required: tell the app to not try again before an action as be done manually
// by the user
distributor.sendRegistrationFailed(appId, connectorToken, FailedReason.ACTION_REQUIRED)
return
}
if (appId.isBlank()) {
Log.w(TAG, "Refusing registration: Empty application")
distributor.sendRegistrationFailed(appId, connectorToken, "Empty application string")
return
}
GlobalScope.launch(Dispatchers.IO) {
@ -65,7 +66,8 @@ class BroadcastReceiver : android.content.BroadcastReceiver() {
distributor.sendEndpoint(appId, connectorToken, endpoint)
} else {
Log.d(TAG, "Subscription with connectorToken $connectorToken exists for a different app. Refusing registration.")
distributor.sendRegistrationFailed(appId, connectorToken, "Connector token already exists")
// Internal_error: try again with a new token
distributor.sendRegistrationFailed(appId, connectorToken, FailedReason.INTERNAL_ERROR)
}
return@launch
}
@ -103,7 +105,8 @@ class BroadcastReceiver : android.content.BroadcastReceiver() {
SubscriberServiceManager.refresh(app)
} catch (e: Exception) {
Log.w(TAG, "Failed to add subscription", e)
distributor.sendRegistrationFailed(appId, connectorToken, e.message)
// Try again when there is network
distributor.sendRegistrationFailed(appId, connectorToken, FailedReason.NETWORK)
}
// Add to log scrubber

View file

@ -17,5 +17,5 @@ const val EXTRA_APPLICATION = "application"
const val EXTRA_PI = "pi"
const val EXTRA_TOKEN = "token"
const val EXTRA_ENDPOINT = "endpoint"
const val EXTRA_MESSAGE = "message"
const val EXTRA_FAILED_REASON = "reason"
const val EXTRA_BYTES_MESSAGE = "bytesMessage"

View file

@ -15,7 +15,6 @@ class Distributor(val context: Context) {
broadcastIntent.`package` = app
broadcastIntent.action = ACTION_MESSAGE
broadcastIntent.putExtra(EXTRA_TOKEN, connectorToken)
broadcastIntent.putExtra(EXTRA_MESSAGE, String(message)) // UTF-8
broadcastIntent.putExtra(EXTRA_BYTES_MESSAGE, message)
context.sendBroadcast(broadcastIntent)
}
@ -39,15 +38,13 @@ class Distributor(val context: Context) {
context.sendBroadcast(broadcastIntent)
}
fun sendRegistrationFailed(app: String, connectorToken: String, message: String?) {
fun sendRegistrationFailed(app: String, connectorToken: String, reason: FailedReason) {
Log.d(TAG, "Sending REGISTRATION_FAILED to $app (token=$connectorToken)")
val broadcastIntent = Intent()
broadcastIntent.`package` = app
broadcastIntent.action = ACTION_REGISTRATION_FAILED
broadcastIntent.putExtra(EXTRA_TOKEN, connectorToken)
if (message != null) {
broadcastIntent.putExtra(EXTRA_MESSAGE, message)
}
broadcastIntent.putExtra(EXTRA_FAILED_REASON, reason)
context.sendBroadcast(broadcastIntent)
}

View file

@ -0,0 +1,23 @@
package io.heckel.ntfy.up
/**
* A registration request may fail for different reasons.
*/
enum class FailedReason {
/**
* This is a generic error type, you can try to register again directly.
*/
INTERNAL_ERROR,
/**
* The registration failed because of missing network connection, try again when network is back.
*/
NETWORK,
/**
* The distributor requires a user action to work. For instance, the distributor may be log out of the push server and requires the user to log in. The user must interact with the distributor or sending a new registration will fail again.
*/
ACTION_REQUIRED,
/*
* The distributor requires a VAPID key and the app didn't provide one during registration.
VAPID_REQUIRED,
*/
}