我使用 SignalR 开发了一个聊天应用程序。目前我正在将该代码移至 Android 应用程序的 WebAPI,
当用户向 Android 用户发送消息时,我想在 Android 手机上显示推送通知。是否可以在 Android 应用程序中使用 SignalR 作为客户端?
我看到了一些在线参考资料,例如 google GCm、ShaperChat,但我不理解它们。
是的,您可以在 Android 应用程序中使用 SignalR。但是,最好将您的应用配置为在前台时使用 SignalR,并在后台时切换到 Firebase 推送通知。这是因为当应用程序不活动时,SignalR 连接可能会断开。使用 Firebase 进行后台通知更可靠,因为它依赖于始终在设备上运行的 Google Play 服务。
一个简单的 signalR 实现 https://github.com/ngocchung/SimpleSignalRClient
是的,可以。 首先,确保您的 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);
}
}
您将接到服务人员的电话后 希望您能得到您需要的东西。