/**
* 连接socket 和心跳
*/
public class SocketService extends Service {
private static addNewOrderInterface mInterface; private SocketBinder socketBinder = new SocketBinder(); public static WebSocketClient client;
public static String address = "ws://192.168.0.120:8080/websocket/"; private static int count = 0;
// 发送心跳包
private static Handler timeHandler = new Handler();
private static Handler mHandler = new Handler();
private static Runnable heartBeatRunnable = new Runnable() {
@Override
public void run() {
mHandler.postDelayed(this, 15 * 1000);
sendMsg("@heart");
// socketBinder.sendMsg("@heart"); }
};
private static Runnable timeRunnable = new Runnable() {
@Override
public void run() {
timeHandler.postDelayed(this, 1000);
count++;
if (count >= 30) {
//socket 连接断开
try {
closeConnect();
L.e(SharedUtils.getInstance().getToken() + "连接socket的token");
initSocketClient(SharedUtils.getInstance().getToken());
} catch (URISyntaxException e) {
e.printStackTrace();
}
connect();
}
}
}; public static String message; static String tokenMsg; @Override
public void onCreate() {
super.onCreate(); try {
L.e(SharedUtils.getInstance().getToken() + "连接socket的token");
initSocketClient(SharedUtils.getInstance().getToken());
} catch (URISyntaxException e) {
e.printStackTrace();
} } @Override
public int onStartCommand(Intent intent, int flags, int startId) {
L.e("执行了onStartCommand()");
// String orderNo = intent.getStringExtra("orderNo");
// if (orderNo != null)
// getOrder(orderNo);
// if (client.isClosed())
connect();
return super.onStartCommand(intent, flags, startId);
} @Override
public void onDestroy() {
super.onDestroy();
L.e("执行了onDestory()");
} @Nullable
@Override
public IBinder onBind(Intent intent) {
return socketBinder;
} @Override
public boolean onUnbind(Intent intent) {
L.e("绑定服务");
return super.onUnbind(intent);
} @Override
public void unbindService(ServiceConnection conn) {
// closeConnect();
L.e("解绑服务");
super.unbindService(conn);
} public static void receiverMessage(String msg) { if (mInterface != null) {
if (msg.indexOf("heart") != -1) { } else {
mInterface.addNewData(msg);
}
}
} public static class SocketBinder extends Binder { public void service_connect_Activity() {
mHandler.post(heartBeatRunnable);
timeHandler.post(timeRunnable);
L.e("Service关联了Activity,并在Activity执行了Service的方法"); } public void addNewOrderInterface(addNewOrderInterface anInterface) {
mInterface = anInterface;
} } //断开连接
public static void closeConnect() {
try {
client.close();
} catch (Exception e) {
e.printStackTrace();
} finally {
client = null;
} L.e("断开 socket"); } /**
* 发送消息
*/
public static void sendMsg(String msg) { if (!SharedUtils.getInstance().getLogin()) {
return;
} L.e("socket发送给服务端的消息- - >" + msg);
if (client == null)
return;
try {
client.send(msg);
} catch (WebsocketNotConnectedException e) {
if (tokenMsg == null) { L.e("socket----->WebsocketNotConnectedException异常");
e.printStackTrace();
closeConnect();
try {
initSocketClient(SharedUtils.getInstance().getToken());
} catch (URISyntaxException ee) {
ee.printStackTrace();
}
connect();
}
}
} public static void initSocketClient(String token) throws URISyntaxException { if (client == null) {
client = new WebSocketClient(new URI(address + token)) { @Override
public void onOpen(ServerHandshake serverHandshake) {
//连接成功
L.e("socket连接成功");
count = 0;
} @Override
public void onMessage(String s) {
L.e("服务端发的消息:" + s);
if (s.equals("token验证失败!")) {
tokenMsg = s;
SharedUtils.getInstance().setLogin(false);
//token 过期 或者 验证失败 停止心跳相关
mHandler.removeCallbacks(heartBeatRunnable);
timeHandler.removeCallbacks(timeRunnable); return;
} else {
tokenMsg = null;
}
if (s.indexOf("heart") != -1) {
count = 0;
} message = s;
// if (s.length() > 10)
receiverMessage(s);
if (s.length() > 5) {
// disposeData(s);
// newOrder.addNewData(s);
// OrderReceiveFragment.getInstance().addNewData(s);
}
} @Override
public void onClose(int i, String s, boolean remote) {
//连接断开,remote判定是客户端断开还是服务端断开
L.e("Connection closed by " + (remote ? "remote peer---断开??" : "us") + ", info=" + s);
//
} @Override
public void onError(Exception e) {
L.e("error:" + e);
}
};
}
} //连接
public synchronized static void connect() { new Thread() {
@Override
public void run() {
if (client != null) {
try {
client.connect();
} catch (IllegalStateException e) {
L.e(e.toString());
}
}
L.e("socket连接");
}
}.start(); } }
信鸽推送自定义提示音
信鸽后台管理 高级设置 设置自定义提示音 值为mp3文件名
MP3文件放在raw下 服务器后台设置这些
/**
* 这样做本以为会没有问题的了,结果内测还是没有声音,懵逼了。好不容易知道官方有方法可以实现了,居然不行,就继续百度吧,百啊百啊百到谷歌去了,还是那些东西啊。去找客服了,还好客服说部分机型不适配,成功甩锅。
* coding还得继续啊,下了个官方的demo,远远本本的复制了都不行,沮丧,问大佬去吧。大佬简单明了,几句话就指出问题了,他说你们都不踩8.0的坑,我傻了吧唧的就用默认的8.0了,换到7.0的和demo一个版本,好了,好了
*/