-
Notifications
You must be signed in to change notification settings - Fork 115
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
When subscribed to a new_topic, send endpoints to all apps #70
base: main
Are you sure you want to change the base?
Conversation
io.heckel.ntfy.up.BroadcastReceiver.sendRegistration(baseContext, connectionId.baseUrl, topic) | ||
// TODO is that the right context - looks like it works??? |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should that work? Or do I need to make a whole chain of contexts being passed down and stored? Not really sure what context is used for when creating the repository downstream. It seems to work as is.
/* We don't send the endpoint here anymore, the foreground service will do that after | ||
registering with the push server. This avoids a race condition where the application server | ||
is rejected before ntfy even establishes that this topic exists. | ||
This is fine from an application perspective, because other distributors can't even register | ||
without a connection to the push server. | ||
Unless the app sends registration twice. Then it'll get the endpoint.*/ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
^ Pointing this out, hopefully is fine
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actually, assuming the Android app is pushed out before the server changes, I'll have to leave the endpoint sending in for now. Otherwise, the new Android app will break for old servers.
|
||
// TODO Where's the best place to put this function? This seems to be the only place | ||
// with the access to the locks, but also globally accessible | ||
// but also, broadcast receiver is for *receiving Android broadcasts* | ||
public fun sendRegistration(context: Context, baseUrl : String, topic : String) { | ||
val app = context.applicationContext as Application | ||
val repository = app.repository | ||
val distributor = Distributor(app) | ||
GlobalScope.launch(Dispatchers.IO) { | ||
// We're doing all of this inside a critical section, because of possible races. | ||
// See https://github.com/binwiederhier/ntfy/issues/230 for details. | ||
|
||
mutex.withLock { | ||
val existingSubscription = repository.getSubscription(baseUrl, topic) ?: return@launch | ||
val appId = existingSubscription.upAppId ?: return@launch | ||
val connectorToken = existingSubscription.upConnectorToken ?: return@launch | ||
val endpoint = topicUrlUp(existingSubscription.baseUrl, existingSubscription.topic) | ||
distributor.sendEndpoint(appId, connectorToken, endpoint) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Where should I put this function?
Also needs cleaning and organization, but I'm opening this because I'm very excited to finally have something that works. So we previously discussed that I would do it using just timing data on the app because that would be simpler, but it really was not. Keeping track of the connection state was a whole thing and the worst thing is that it didn't solve the problem of losing the connection on server restart. This implementation only sends out new_endpoint after subscribing to the topic on the ntfy server.
Goes along with binwiederhier/ntfy#979