remove deep links

This commit is contained in:
Philipp Heckel 2021-12-12 20:03:53 -05:00
parent 459e1ff84b
commit 56665c8cc0
4 changed files with 10 additions and 68 deletions

View file

@ -41,16 +41,6 @@
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value=".ui.MainActivity"/>
<!-- Open https://ntfy.sh links with the app -->
<intent-filter android:label="@string/app_name">
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.BROWSABLE" />
<data android:scheme="@string/app_base_scheme"
android:host="@string/app_base_host"
android:pathPattern="/..*" /> <!-- This is awful, but it's the only way in Android -->
</intent-filter>
</activity>
<!-- Subscriber foreground service for hosts other than ntfy.sh -->

View file

@ -12,6 +12,10 @@ import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.launch
/**
* The broadcast service is responsible for sending and receiving broadcasted intents
* in order to facilitate taks app integrations.
*/
class BroadcastService(private val ctx: Context) {
fun send(subscription: Subscription, notification: Notification, muted: Boolean) {
val intent = Intent()
@ -19,12 +23,14 @@ class BroadcastService(private val ctx: Context) {
intent.putExtra("id", notification.id)
intent.putExtra("base_url", subscription.baseUrl)
intent.putExtra("topic", subscription.topic)
intent.putExtra("time", notification.timestamp.toInt())
intent.putExtra("title", notification.title)
intent.putExtra("message", notification.message)
intent.putExtra("tags", notification.tags)
intent.putExtra("tags_map", joinTagsMap(splitTags(notification.tags)))
intent.putExtra("priority", notification.priority)
intent.putExtra("muted", muted)
intent.putExtra("muted_str", muted.toString())
Log.d(TAG, "Sending intent broadcast: $intent")
ctx.sendBroadcast(intent)
@ -40,9 +46,9 @@ class BroadcastService(private val ctx: Context) {
private fun send(ctx: Context, intent: Intent) {
val api = ApiService()
val baseUrl = intent.getStringExtra("base_url") ?: ctx.getString(R.string.app_base_url)
val topic = intent.getStringExtra("topic") ?: return
val message = intent.getStringExtra("message") ?: return
val baseUrl = intent.getStringExtra("base_url") ?: ctx.getString(R.string.app_base_url)
val title = intent.getStringExtra("title") ?: ""
val tags = intent.getStringExtra("tags") ?: ""
val priority = if (intent.getStringExtra("priority") != null) {

View file

@ -201,7 +201,7 @@ class AddFragment : DialogFragment() {
activity?.let {
it.runOnUiThread {
if (subscription != null) {
if (subscription != null || DISALLOWED_TOPICS.contains(topic)) {
subscribeButton.isEnabled = false
} else if (useAnotherServerCheckbox.isChecked) {
subscribeButton.isEnabled = topic.isNotBlank()
@ -226,5 +226,6 @@ class AddFragment : DialogFragment() {
companion object {
const val TAG = "NtfyAddFragment"
private val DISALLOWED_TOPICS = listOf("docs", "static")
}
}

View file

@ -79,63 +79,8 @@ class DetailActivity : AppCompatActivity(), ActionMode.Callback, NotificationFra
// Show 'Back' button
supportActionBar?.setDisplayHomeAsUpEnabled(true)
// Handle direct deep links to topic "https://ntfy.sh/..."
val url = intent?.data
if (intent?.action == ACTION_VIEW && url != null) {
val topic = url.pathSegments.first()
title = topicShortUrl(appBaseUrl!!, topic) // We assume the app base URL
maybeSubscribeAndLoadView(topic)
} else {
loadView()
}
}
private fun maybeSubscribeAndLoadView(topic: String) {
lifecycleScope.launch(Dispatchers.IO) {
val baseUrl = appBaseUrl!!
var subscription = repository.getSubscription(baseUrl, topic)
if (subscription == null) {
subscription = Subscription(
id = Random.nextLong(),
baseUrl = baseUrl,
topic = topic,
instant = false,
mutedUntil = 0,
totalCount = 0,
newCount = 0,
lastActive = Date().time/1000
)
repository.addSubscription(subscription)
// Subscribe to Firebase topic if ntfy.sh (even if instant, just to be sure!)
Log.d(MainActivity.TAG, "Subscribing to Firebase")
messenger.subscribe(topic)
// Fetch cached messages
try {
val notifications = api.poll(subscription.id, subscription.baseUrl, subscription.topic)
notifications.forEach { notification -> repository.addNotification(notification) }
} catch (e: Exception) {
Log.e(MainActivity.TAG, "Unable to fetch notifications: ${e.stackTrace}")
}
runOnUiThread {
val message = getString(R.string.detail_deep_link_subscribed_toast_message, topicShortUrl(baseUrl, topic))
Toast.makeText(this@DetailActivity, message, Toast.LENGTH_LONG).show()
}
}
intent.putExtra(MainActivity.EXTRA_SUBSCRIPTION_ID, subscription.id)
intent.putExtra(MainActivity.EXTRA_SUBSCRIPTION_BASE_URL, subscription.baseUrl)
intent.putExtra(MainActivity.EXTRA_SUBSCRIPTION_TOPIC, subscription.topic)
intent.putExtra(MainActivity.EXTRA_SUBSCRIPTION_INSTANT, subscription.instant)
intent.putExtra(MainActivity.EXTRA_SUBSCRIPTION_MUTED_UNTIL, subscription.mutedUntil)
runOnUiThread {
loadView()
}
}
}
private fun loadView() {
// Get extras required for the return to the main activity