我开发了一个使用SignalR的聊天应用程序。目前我正在将该代码移动到WebAPI以获取Android应用程序,
当用户向Android用户发送消息时,我想在Android手机上显示推送通知。可以使用SignalR作为Android应用程序中的客户端。
我看过谷歌GCm,ShaperChat等在线参考文献,但我不理解它们。
答案 0 :(得分:3)
是的,您可以在Android应用程序中使用signalR。但是,将应用程序配置为在前台使用signalR然后在应用程序在后台运行时使用firebase推送通知是值得的,因为您的signalR连接可能会断开,然后您的signalR将无法工作。因此,当应用程序运行时,您应该使用signalR,否则使用firebase通知系统,因为firebase使用google play服务,该服务将始终在设备上运行。
一个简单的signalR实现 https://github.com/ngocchung/SimpleSignalRClient
答案 1 :(得分:1)
是的,你可以。 首先要确保你的SignalR服务工作正常并记下你的令牌和集线器连接,组名,你可以通过调用它们来订阅这些电话。
1)制作一个单身课程
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)然后实施服务
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);
}
}
您将收到来自服务的电话 希望你能得到你需要的东西。