'Pusher: Trigger pusher events from android app
In an android app I was able to subscribe to a pusher channel and log out the event data. So now I want to be able to trigger events from the same application. I don't find documentation relating to this on the web.
Am I missing something because I also have a node express app in which I'm able to trigger and subscribe to events in the same app.
Below is my code for the Android app, I'm looking to trigger an event in the "responseAFunc" function which is wired to a button click.
I have also left out the all import statements and package statements.
public class MainActivity extends Activity {
private Button responseA;
private ActivityMainBinding binding;
private Pusher pusher;
private Channel channel;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
binding = ActivityMainBinding.inflate(getLayoutInflater());
setContentView(binding.getRoot());
responseA = binding.responseA;
PusherOptions options = new PusherOptions();
options.setCluster("eu");
pusher = new Pusher("*****************",options);
pusher.connect( new ConnectionEventListener() {
@Override
public void onConnectionStateChange(ConnectionStateChange change) {
Log.e("Pusher"," changed to " + change.getCurrentState());
}
@Override
public void onError(String message, String code, Exception e) {
Log.e("Pusher" ," connecting! msg:" + message + " " + code);
Log.e("Pusher", e.getMessage());
}
}, ConnectionState.ALL);
channel = pusher.subscribe("to-pusher");
channel.bind("message", new SubscriptionEventListener() {
@Override
public void onEvent(PusherEvent event) {
Log.i("Pusher", event.getData());
}
});
}
public void responseAFunc(View view){
// Trigger event
}
}
Hope this makes sense and HELLO from South Africa !!!
Solution 1:[1]
Hello from Amsterdam!
What you are looking for is "client events". Here are the docs: https://pusher.com/docs/channels/using_channels/events/#triggering-client-events
It's very easy: Once you got an instance for a channel you can just call the "trigger" method on it. Here are the docs for java: https://github.com/pusher/pusher-websocket-java#triggering-events
The harder part is that it will only work on private and presence channels. And then you will need to implement authentication. https://pusher.com/docs/channels/server_api/authenticating-users/
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | marcel.corso |