171 lines
7.5 KiB
Plaintext
171 lines
7.5 KiB
Plaintext
|
package com.example.administrator.ximengjianyu.mqtt.push;
|
|||
|
|
|||
|
import android.app.Activity;
|
|||
|
import android.app.Dialog;
|
|||
|
import android.app.DialogFragment;
|
|||
|
import android.app.Notification;
|
|||
|
import android.app.NotificationManager;
|
|||
|
import android.app.PendingIntent;
|
|||
|
import android.content.Context;
|
|||
|
import android.content.ContextWrapper;
|
|||
|
import android.content.Intent;
|
|||
|
import android.os.Build;
|
|||
|
import android.os.Handler;
|
|||
|
import android.support.annotation.RequiresApi;
|
|||
|
import android.util.Log;
|
|||
|
import android.view.View;
|
|||
|
import android.widget.Toast;
|
|||
|
|
|||
|
import com.cm.utils.net.HttpUtils;
|
|||
|
import com.cm.utils.net.response.GsonResponseHandler;
|
|||
|
import com.example.administrator.ximengjianyu.R;
|
|||
|
import com.example.administrator.ximengjianyu.activity.LoginActivity;
|
|||
|
import com.example.administrator.ximengjianyu.activity.home.HomeActivity;
|
|||
|
import com.example.administrator.ximengjianyu.beans.GuanDuanPhoneBean;
|
|||
|
import com.example.administrator.ximengjianyu.beans.TuiChuBean;
|
|||
|
import com.example.administrator.ximengjianyu.beans.WebCTIStatus;
|
|||
|
import com.example.administrator.ximengjianyu.global.Constants;
|
|||
|
import com.example.administrator.ximengjianyu.global.UserInfo;
|
|||
|
import com.example.administrator.ximengjianyu.utils.MDialog;
|
|||
|
import org.json.JSONException;
|
|||
|
import org.json.JSONObject;
|
|||
|
|
|||
|
import org.eclipse.paho.client.mqttv3.MqttCallback;
|
|||
|
import org.eclipse.paho.client.mqttv3.MqttDeliveryToken;
|
|||
|
import org.eclipse.paho.client.mqttv3.MqttMessage;
|
|||
|
import org.eclipse.paho.client.mqttv3.MqttTopic;
|
|||
|
|
|||
|
import java.util.HashMap;
|
|||
|
import java.util.Map;
|
|||
|
|
|||
|
public class PushCallback implements MqttCallback {
|
|||
|
|
|||
|
public Context mActivity;
|
|||
|
private ContextWrapper context;
|
|||
|
private Map<String, String> tuichumap;
|
|||
|
public Activity mactivity;
|
|||
|
|
|||
|
public Handler handler;
|
|||
|
public PushCallback(Activity activity, Handler handler) {
|
|||
|
this.mactivity = activity;
|
|||
|
this.handler=handler;
|
|||
|
}
|
|||
|
public PushCallback(ContextWrapper context) {
|
|||
|
this.context = context;
|
|||
|
}
|
|||
|
|
|||
|
@Override
|
|||
|
public void connectionLost(Throwable cause) {
|
|||
|
//We should reconnect here
|
|||
|
}
|
|||
|
|
|||
|
|
|||
|
|
|||
|
@RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN)
|
|||
|
@SuppressWarnings("deprecation")
|
|||
|
@Override
|
|||
|
public void messageArrived(MqttTopic topic, MqttMessage message) throws Exception {
|
|||
|
final NotificationManager notificationManager = (NotificationManager)
|
|||
|
context.getSystemService(Context.NOTIFICATION_SERVICE);
|
|||
|
final Notification.Builder notification = new Notification.Builder(context);
|
|||
|
notification.setContentTitle("消息");
|
|||
|
notification.setContentText("收到消息:"+new String(message.getPayload()));
|
|||
|
notification.setSmallIcon(R.drawable.snow);
|
|||
|
final Intent intent = new Intent(context, HomeActivity.class);
|
|||
|
final PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, 0);
|
|||
|
notification .setContentIntent(pendingIntent);
|
|||
|
Notification build = notification.build();
|
|||
|
notificationManager.notify(124, build);
|
|||
|
String tag = new String(message.getPayload());
|
|||
|
Log.e("tag", "messageArrived: " + new String(message.getPayload(), "utf-8"));
|
|||
|
MessageBean bean = parse(tag);
|
|||
|
String messageFlag = bean.getMessageFlag();
|
|||
|
String sextensionState = bean.getSextensionState();
|
|||
|
String extensionNumber = bean.getExtensionNumber();
|
|||
|
if("1".equals(messageFlag) && extensionNumber.equals(UserInfo.getExtensionNumber(mActivity)) ) {
|
|||
|
if(WebCTIStatus.IDLE.equals(sextensionState) && bean.getTitle().equals(UserInfo.getTel(mActivity)) ) {
|
|||
|
Log.e("--PushCallback--",sextensionState);
|
|||
|
Map<String,String> map=new HashMap<>();
|
|||
|
map.put("token",UserInfo.getToken(mActivity));
|
|||
|
map.put("phone", UserInfo.getTel(mActivity));
|
|||
|
HttpUtils.getInstance().post(Constants.GUADUANPHONE, map, new GsonResponseHandler<GuanDuanPhoneBean>() {
|
|||
|
@Override
|
|||
|
public void onSuccess(int statusCode, GuanDuanPhoneBean response) {
|
|||
|
Log.e("==挂断成功==", response.toString());
|
|||
|
if ("200".equals(response.getState())) {
|
|||
|
handler.sendEmptyMessage(1);
|
|||
|
Toast.makeText(mActivity, "剩余次数" + response.getData().getSurplusCount(), Toast.LENGTH_SHORT).show();
|
|||
|
//停止服务
|
|||
|
Intent intent = new Intent(mactivity, MQTTService.class);
|
|||
|
mactivity.stopService(intent);
|
|||
|
//跳转登录页面.
|
|||
|
tuichulogin();
|
|||
|
} else if ("400".equals(response.getState())) {
|
|||
|
Toast.makeText(mActivity, response.getMsg(), Toast.LENGTH_SHORT).show();
|
|||
|
}else if ("202".equals(response.getState())) { //跳转到登录页面,并关闭其他activity
|
|||
|
//token会话失效,需要进入登录页面重新登录
|
|||
|
Intent intent = new Intent(mactivity, LoginActivity.class);
|
|||
|
mactivity.startActivity(intent);
|
|||
|
mactivity.finish();
|
|||
|
// judgeHasClickedSave();
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
@Override
|
|||
|
public void onFailure(int statusCode, String error_msg) {
|
|||
|
|
|||
|
}
|
|||
|
});
|
|||
|
}
|
|||
|
}
|
|||
|
Log.e("messageFlag",messageFlag);
|
|||
|
Log.e("PushCallbackbean",bean.toString());
|
|||
|
}
|
|||
|
|
|||
|
private MessageBean parse(String tag) throws JSONException {
|
|||
|
MessageBean bean = new MessageBean();
|
|||
|
JSONObject obj = new JSONObject(tag);
|
|||
|
bean.setContent(obj.getString("content"));
|
|||
|
bean.setExtensionNumber(obj.getString("extensionNumber"));
|
|||
|
bean.setMessageFlag(obj.getString("messageFlag"));
|
|||
|
bean.setRemark(obj.getString("remark"));
|
|||
|
bean.setTitle(obj.getString("title"));
|
|||
|
bean.setSendTime(obj.getString("sendTime"));
|
|||
|
bean.setSextensionState(obj.getString("sextensionState"));
|
|||
|
return bean;
|
|||
|
}
|
|||
|
|
|||
|
@Override
|
|||
|
public void deliveryComplete(MqttDeliveryToken token) {
|
|||
|
//We do not need this because we do not publish
|
|||
|
}
|
|||
|
public void tuichulogin() {
|
|||
|
tuichumap = new HashMap<String, String>();
|
|||
|
tuichumap.put("token", UserInfo.getToken(mActivity));
|
|||
|
HttpUtils.getInstance().post(mActivity, Constants.TUICHUDENGLU, tuichumap, new GsonResponseHandler<TuiChuBean>() {
|
|||
|
@Override
|
|||
|
public void onSuccess(int statusCode, TuiChuBean response) {
|
|||
|
if ("200".equals(response.getState())) {
|
|||
|
Intent intent = new Intent(mactivity, LoginActivity.class);
|
|||
|
mactivity.startActivity(intent);
|
|||
|
mactivity.finish();
|
|||
|
} else if ("400".equals(response.getState())) {
|
|||
|
Toast.makeText(mActivity, response.getMsg(), Toast.LENGTH_SHORT).show();
|
|||
|
}else if ("202".equals(response.getState())) { //跳转到登录页面,并关闭其他activity
|
|||
|
//token会话失效,需要进入登录页面重新登录
|
|||
|
Intent intent = new Intent(mactivity, LoginActivity.class);
|
|||
|
mactivity.startActivity(intent);
|
|||
|
mactivity.finish();
|
|||
|
// judgeHasClickedSave();
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
@Override
|
|||
|
public void onFailure(int statusCode, String error_msg) {
|
|||
|
|
|||
|
}
|
|||
|
});
|
|||
|
}
|
|||
|
|
|||
|
}
|