Reputation: 152
I have developed a chatting app Using SignalR. Currently I am moving that code to WebAPI for an android application,
When a user sends message to an android user I want to show push notifications on android mobiles. Is its possible to use SignalR as the client in an android app.
I'm seen some of online references like google GCm, ShaperChat but I'm not understanding them.
Upvotes: 3
Views: 8790
Reputation: 4969
Yes, you can use SignalR in your Android application. However, it’s best to configure your app to use SignalR when it is in the foreground and switch to Firebase push notifications when it’s in the background. This is because the SignalR connection can be disconnected when the app is not active. Using Firebase for background notifications is more reliable, as it relies on Google Play Services, which always run on the device.
A simple signalR implementation https://github.com/ngocchung/SimpleSignalRClient
Upvotes: 13
Reputation: 1723
Yes, you can. first of all make sure your SignalR service is working fine and noted down your token and hub connection, group name from which you can subscribe those calls by invoking them.
1) Made one singleton class
public class SignalRSingleton {
private static SignalRSingleton mInstance = null;
public HubConnection mHubConnection;
public HubProxy mHubProxy;
public static SignalRSingleton getInstance(){
if(mInstance == null)
{
mInstance = new SignalRSingleton();
}
return mInstance;
}
public void setmHubConnection()
{
String serverUrl = "http://192.168.1.5:8089/XM/";
//String serverUrl = "http://192.168.1.184/test";
mHubConnection = new HubConnection(serverUrl);
}
public void setHubProxy()
{
/* Credentials credentials = new Credentials() {
@Override
public void prepareRequest(Request request) {
request.addHeader("User-Name", MainActivity.unm);
}
};*/
//mHubConnection.setCredentials(credentials);
String SERVER_HUB_CHAT = "messages";
//String SERVER_HUB_CHAT = "Chat";
mHubProxy = mHubConnection.createHubProxy(SERVER_HUB_CHAT);
}
/**
* method for clients (activities)
*/
public void sendMessage(String name , String message) {
String str = "{'RequestMessage':{'PID':'lr1','Password':'GIefhSIC5iBCnxioufbwEw == '},'RequestType':'Login'}";
String SERVER_METHOD_SEND = "getMessage";
//String SERVER_METHOD_SEND = "Send";
mHubProxy.invoke(SERVER_METHOD_SEND,str);
}
}
2) Then implement service
public class SignalRService extends Service {
//private HubConnection mHubConnection;
//private HubProxy mHubProxy;
private Handler mHandler; // to display Toast message
private final IBinder mBinder = new LocalBinder(); // Binder given to clients
private SignalRSingleton mInstance;
final static String MY_ACTION = "MY_ACTION";
public SignalRService() {
}
@Override
public void onCreate() {
super.onCreate();
mInstance = SignalRSingleton.getInstance();
mHandler = new Handler(Looper.getMainLooper());
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
int result = super.onStartCommand(intent, flags, startId);
startSignalR();
return result;
}
@Override
public void onDestroy() {
mInstance.mHubConnection.stop();
super.onDestroy();
}
@Override
public IBinder onBind(Intent intent) {
// Return the communication channel to the service.
startSignalR();
return mBinder;
}
/**
* Class used for the client Binder. Because we know this service always
* runs in the same process as its clients, we don't need to deal with IPC.
*/
public class LocalBinder extends Binder {
public SignalRService getService() {
// Return this instance of SignalRService so clients can call public methods
return SignalRService.this;
}
}
/**
* method for clients (activities)
*/
private void startSignalR() {
Platform.loadPlatformComponent(new AndroidPlatformComponent());
mInstance.setmHubConnection();
mInstance.setHubProxy();
ClientTransport clientTransport = new ServerSentEventsTransport(mInstance.mHubConnection.getLogger());
SignalRFuture<Void> signalRFuture = mInstance.mHubConnection.start(clientTransport);
try {
signalRFuture.get();
} catch (InterruptedException | ExecutionException e) {
Log.e("SimpleSignalR", e.toString());
return;
}
mInstance.sendMessage(MainActivity.unm,"Hello All!");
String CLIENT_METHOD_BROADAST_MESSAGE = "recievedMessage";
//String CLIENT_METHOD_BROADAST_MESSAGE = "messageReceived";
mInstance.mHubProxy.on(CLIENT_METHOD_BROADAST_MESSAGE,
new SubscriptionHandler2<String,LoginInfo>() {
@Override
public void run(final String msg,final LoginInfo loginInfo) {
final String finalMsg = loginInfo.FullName + " says " + loginInfo.Password;
Intent intent = new Intent();
intent.setAction(MY_ACTION);
intent.putExtra("DATAPASSED", finalMsg);
sendBroadcast(intent);
}
}
, String.class,LoginInfo.class);
}
}
After you will get calls from Service Hope you will get what you need.
Upvotes: 2