-
Notifications
You must be signed in to change notification settings - Fork 14
Publish Subscribe on Android
Guillaume Gendre edited this page Jun 7, 2019
·
4 revisions
This page is part of the Cobalt Publish/Subscribe pages. Don't miss the others.
- Introduction to Cobalt PubSub messages
- Publish/Subscribe on the Web side
- Publish/Subscribe on iOS
- Web Lifecycle messages
first, import the Cobalt class :
import org.cobaltians.cobalt.Cobalt;
-
message
: (JSONObject) The message that will be sent in the channel. -
channel
: (string) The name of the channel in which you want to send your message.
JSONObject message = new JSONObject();
try {
message.put("myValue", 42);
} catch (JSONException e) {
e.printStackTrace();
}
Cobalt.publishMessage(message, "myChannel");
You can either create an interface on the fly, either add the interface to your current class and add specified method to catch the message.
To use the PubSubInterface, import it :
import org.cobaltians.cobalt.pubsub.PubSubInterface;
-
channel
: (string) The name of the channel you want to subscribe. -
pubSubInterface
: (PubSubInterface) A Cobalt PubSubInterface to handle the messages when they'll come. This PubSubInterface has one methodonMessageReceived
receiving message and channel.
This method is cool if you only subscribe to a few messages.
Cobalt.subscribeToChannel("myChannel", new PubSubInterface() {
@Override
public void onMessageReceived(@Nullable JSONObject message, @NonNull String channel) {
// Do something with the message
// In this case, you don't care about the channel
// because your interface is built only for this channel
}
});
This method is better if you subscribe to a lot of messages in your class.
public final class myClass implements PubSubInterface {
...
// later somewhere :
public void myFunction() {
Cobalt.subscribeToChannel("myChannel", this);
}
// later again :
@Override
public void onMessageReceived(@Nullable JSONObject message, @NonNull String channel)
{
switch(channel) {
case "myChannel":
if (message != null) {
// Do something with the message
}
break;
}
}
Unsubscribe a PubSubInterface to messages from the specified channel.
-
channel
: (string) The name of the channel you want to unsubscribe. -
pubSubInterface
: (PubSubInterface) The Cobalt PubSubInterface you created to handle the messages.
Cobalt.unsubscribeFromChannel("myChannel", this)
Cobalt is an Open-Source Hybrid Mobile Framework. Read more about it on the home page or in the documentation
- Introduction to Cobalt navigation
- The cobalt.json file
- native navigation bars
- Handling the Android back button
- Introduction to Cobalt messages
- Publish/Subscribe on the Web side
- Publish/Subscribe on Android
- Publish/Subscribe on iOS
- Web Lifecycle messages
- Pull-To-Refresh and Infinite Scroll
- Custom alerts and Toasts
- LocalStorage
- OpenExternalUrl
- PlatformInfos
- Ajax
- Removing the top bar
- Adding Cobalt to an existing project
- Customizing your hybrid views
- Handle multiple Cobalt webviews on the same screen (TODO)