diff --git a/app/src/main/java/com/github/gotify/service/Constants.kt b/app/src/main/java/com/github/gotify/service/Constants.kt index 59bf7e2..40b6da8 100644 --- a/app/src/main/java/com/github/gotify/service/Constants.kt +++ b/app/src/main/java/com/github/gotify/service/Constants.kt @@ -1,14 +1,22 @@ package com.github.gotify.service /** - * Constants found here: - * https://github.com/UnifiedPush/UP-lib/blob/main/src/main/java/org/unifiedpush/connector/Constants.kt + * Constants as defined on the specs + * https://github.com/UnifiedPush/UP-spec/blob/main/specifications.md */ -const val NEW_ENDPOINT = "org.unifiedpush.android.connector.NEW_ENDPOINT" -const val UNREGISTERED = "org.unifiedpush.android.connector.UNREGISTERED" -const val MESSAGE = "org.unifiedpush.android.connector.MESSAGE" +const val ACTION_NEW_ENDPOINT = "org.unifiedpush.android.connector.NEW_ENDPOINT" +const val ACTION_REGISTRATION_FAILED = "org.unifiedpush.android.connector.REGISTRATION_FAILED" +const val ACTION_REGISTRATION_REFUSED = "org.unifiedpush.android.connector.REGISTRATION_REFUSED" +const val ACTION_UNREGISTERED = "org.unifiedpush.android.connector.UNREGISTERED" +const val ACTION_MESSAGE = "org.unifiedpush.android.connector.MESSAGE" -const val REGISTER = "org.unifiedpush.android.distributor.REGISTER" -const val UNREGISTER = "org.unifiedpush.android.distributor.UNREGISTER" -const val MESSAGE_ACK = "org.unifiedpush.android.distributor.MESSAGE_ACK" \ No newline at end of file +const val ACTION_REGISTER = "org.unifiedpush.android.distributor.REGISTER" +const val ACTION_UNREGISTER = "org.unifiedpush.android.distributor.UNREGISTER" +const val ACTION_MESSAGE_ACK = "org.unifiedpush.android.distributor.MESSAGE_ACK" + +const val EXTRA_APPLICATION = "application" +const val EXTRA_TOKEN = "token" +const val EXTRA_ENDPOINT = "endpoint" +const val EXTRA_MESSAGE = "message" +const val EXTRA_MESSAGE_ID = "id" diff --git a/app/src/main/java/com/github/gotify/service/PushNotification.kt b/app/src/main/java/com/github/gotify/service/PushNotification.kt index cb5dc0a..43aac43 100644 --- a/app/src/main/java/com/github/gotify/service/PushNotification.kt +++ b/app/src/main/java/com/github/gotify/service/PushNotification.kt @@ -13,9 +13,9 @@ fun sendMessage(context: Context, application: String, message: String){ val token = getToken(context,application)!! val broadcastIntent = Intent() broadcastIntent.`package` = application - broadcastIntent.action = MESSAGE - broadcastIntent.putExtra("token", token) - broadcastIntent.putExtra("message", message) + broadcastIntent.action = ACTION_MESSAGE + broadcastIntent.putExtra(EXTRA_TOKEN, token) + broadcastIntent.putExtra(EXTRA_MESSAGE, message) context.sendBroadcast(broadcastIntent) } @@ -23,18 +23,36 @@ fun sendEndpoint(context: Context, application: String, endpoint: String) { val token = getToken(context,application)!! val broadcastIntent = Intent() broadcastIntent.`package` = application - broadcastIntent.action = NEW_ENDPOINT - broadcastIntent.putExtra("token", token) - broadcastIntent.putExtra("endpoint", endpoint) + broadcastIntent.action = ACTION_NEW_ENDPOINT + broadcastIntent.putExtra(EXTRA_TOKEN, token) + broadcastIntent.putExtra(EXTRA_ENDPOINT, endpoint) context.sendBroadcast(broadcastIntent) } fun sendUnregistered(context: Context, application: String, _token: String?){ - val token = _token?: getToken(context,application)!! + val token = _token?: getToken(context, application)!! val broadcastIntent = Intent() broadcastIntent.`package` = application - broadcastIntent.action = UNREGISTERED - broadcastIntent.putExtra("token", token) + broadcastIntent.action = ACTION_UNREGISTERED + broadcastIntent.putExtra(EXTRA_TOKEN, token) + context.sendBroadcast(broadcastIntent) +} + +fun sendRegistrationFailed(context: Context, application: String, token: String, message: String){ + val broadcastIntent = Intent() + broadcastIntent.`package` = application + broadcastIntent.action = ACTION_REGISTRATION_FAILED + broadcastIntent.putExtra(EXTRA_TOKEN, token) + broadcastIntent.putExtra(EXTRA_MESSAGE, message) + context.sendBroadcast(broadcastIntent) +} + +fun sendRegistrationRefused(context: Context, application: String, token: String, message: String) { + val broadcastIntent = Intent() + broadcastIntent.`package` = application + broadcastIntent.action = ACTION_REGISTRATION_REFUSED + broadcastIntent.putExtra(EXTRA_TOKEN, token) + broadcastIntent.putExtra(EXTRA_MESSAGE, message) context.sendBroadcast(broadcastIntent) } diff --git a/app/src/main/java/com/github/gotify/service/RegisterBroadcastReceiver.kt b/app/src/main/java/com/github/gotify/service/RegisterBroadcastReceiver.kt index 67021f3..7029978 100644 --- a/app/src/main/java/com/github/gotify/service/RegisterBroadcastReceiver.kt +++ b/app/src/main/java/com/github/gotify/service/RegisterBroadcastReceiver.kt @@ -30,7 +30,7 @@ class RegisterBroadcastReceiver: BroadcastReceiver() { } } - private fun registerApp(db: MessagingDatabase, application: String, connector_token: String) { + private fun registerApp(context: Context?, db: MessagingDatabase, application: String, connector_token: String) { if (application.isBlank()) { Log.w("RegisterService","Trying to register an app without packageName") return @@ -46,12 +46,16 @@ class RegisterBroadcastReceiver: BroadcastReceiver() { // User should unregister this app manually // to avoid an app to impersonate another one if (db.isRegistered(application)) { - Log.w("RegisterService","$application already registered with a different token") + val message = "$application already registered with a different token" + Log.w("RegisterService",message) + sendRegistrationRefused(context!!,application,connector_token,message) return } val app = createApp(application) if(app == null){ - Log.w("RegisterService","Cannot create a new app to register") + val message = "Cannot create a new app to register" + Log.w("RegisterService", message) + sendRegistrationFailed(context!!,application,connector_token,message) return } db.registerApp(application, app.id, app.token, connector_token) @@ -86,13 +90,13 @@ class RegisterBroadcastReceiver: BroadcastReceiver() { override fun onReceive(context: Context?, intent: Intent?) { settings = Settings(context) when (intent!!.action) { - REGISTER ->{ + ACTION_REGISTER ->{ val db = MessagingDatabase(context!!) Log.i("Register","REGISTER") - val internalToken = intent.getStringExtra("token")?: "" - val application = intent.getStringExtra("application")?: "" + val internalToken = intent.getStringExtra(EXTRA_TOKEN)?: "" + val application = intent.getStringExtra(EXTRA_APPLICATION)?: "" thread(start = true) { - registerApp(db, application, internalToken) + registerApp(context, db, application, internalToken) Log.i("RegisterService","Registration is finished") }.join() val token = db.getGotifyToken(application, false) @@ -101,10 +105,10 @@ class RegisterBroadcastReceiver: BroadcastReceiver() { sendEndpoint(context,application,endpoint) db.close() } - UNREGISTER ->{ + ACTION_UNREGISTER ->{ Log.i("Register","UNREGISTER") - val token = intent.getStringExtra("token")?: "" - val application = intent.getStringExtra("application")?: "" + val token = intent.getStringExtra(EXTRA_TOKEN)?: "" + val application = intent.getStringExtra(EXTRA_APPLICATION)?: "" thread(start = true) { val db = MessagingDatabase(context!!) unregisterApp(db,application, token)