package com.jiuqi.njt.receiver;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import cn.jpush.android.api.JPushInterface;
import com.jiuqi.mobile.nigo.comeclose.bean.app.news.CarTaskNewsBean;
import com.jiuqi.mobile.nigo.comeclose.bean.app.news.SupplyAndDemandTaskNewsBean;
import com.jiuqi.mobile.nigo.comeclose.bean.app.news.WorkTaskNewsBean;
import com.jiuqi.mobile.nigo.comeclose.bean.base.DataDictionaryBean;
import com.jiuqi.mobile.nigo.comeclose.bean.base.KindsOfCarBean;
import com.jiuqi.mobile.nigo.comeclose.json.JSONObject;
import com.jiuqi.njt.data.CheckStateInterface;
import com.jiuqi.njt.data.MyApp;
import com.jiuqi.njt.data.OptsharepreInterface;
import com.jiuqi.njt.service.UpPositionByTypeService;
import com.jiuqi.njt.ui.NxwPushInfoDetailActivity;
import com.jiuqi.njt.ui.QueryDetailActivity;
import com.jiuqi.njt.util.Constants;
import com.jiuqi.njt.util.TextToSpeechUtil;
import com.jiuqi.njt.util.UIUtil;
import com.jiuqi.njt.util.login.GetRoleAndUserModuleTask;
/**
* 自定义接收器
*
* 如果不定义这个 Receiver,则: 1) 默认用户会打开主界面 2) 接收不到自定义消息
*/
public class MyPushReceiver extends BroadcastReceiver {
private static final String TAG = "MyPushReceiver";
private String id = "";
private String title = "农机通";
private String msg = "";
private MyApp application;
private String type;
private String random;
final String UpPositionServiceName = "com.jiuqi.njt.service.UpPositionByTypeService";
private OptsharepreInterface sharePre;
private String pushRoleCode = "";
private String name = "";
private String flag;
private String message;
private String readMessage;
@Override
public void onReceive(Context context, Intent intent) {
Bundle bundle = intent.getExtras();
application = (MyApp) context.getApplicationContext();
sharePre = new OptsharepreInterface(context);
// Log.d(TAG, "onReceive - " + intent.getAction() + ", extras: "+
// printBundle(bundle));
if (JPushInterface.ACTION_REGISTRATION_ID.equals(intent.getAction())) {
String regId = bundle
.getString(JPushInterface.EXTRA_REGISTRATION_ID);
Log.d(TAG, "接收Registration Id : " + regId);
// send the Registration Id to your server...
} else if (JPushInterface.ACTION_UNREGISTER.equals(intent.getAction())) {
String regId = bundle
.getString(JPushInterface.EXTRA_REGISTRATION_ID);
Log.d(TAG, "接收UnRegistration Id : " + regId);
// send the UnRegistration Id to your server...
} else if (JPushInterface.ACTION_MESSAGE_RECEIVED.equals(intent
.getAction())) {
Log.e(TAG,
"接收到推送下来的自定义消息: "
+ bundle.getString(JPushInterface.EXTRA_MESSAGE));
msg = bundle.getString(JPushInterface.EXTRA_MESSAGE);
// String title =bundle.getString(JPushInterface.EXTRA_TITLE);
// 没有登录不提示信息
if (application.getIsLogin()) {
openNotification(context, bundle);
openNotificationToDriver(context, bundle);
openNotificationToModule(context, bundle);
if (!TextUtils.isEmpty(id)) {
msg = bundle.getString(JPushInterface.EXTRA_TITLE);
// title = bundle.getString(JPushInterface.EXTRA_TITLE);
send(context, Constants.NXW_MODULER_URL_DETAIL_BASE + id,
id);
} else if (!TextUtils.isEmpty(name)) {//久其合作社
msg = bundle.getString(JPushInterface.EXTRA_EXTRA);
sendByData(context);//解析Json数据
// ShareMethod shareMethod = new ShareMethod(context);
// shareMethod.getLocToService(LBSType.getLbsType(Integer.parseInt(type)),random);
}else if(!TextUtils.isEmpty(pushRoleCode)){
String code = sharePre.getPres("role");
if(pushRoleCode.equals(code)){
new GetRoleAndUserModuleTask(context, true).execute();
}
}else {
startUpPositionService(context);
}
}
} else if (JPushInterface.ACTION_NOTIFICATION_RECEIVED.equals(intent
.getAction())) {
Log.d(TAG, "接收到推送下来的通知");
int notifactionId = bundle
.getInt(JPushInterface.EXTRA_NOTIFICATION_ID);
Log.d(TAG, "接收到推送下来的通知的ID: " + notifactionId);
} else if (JPushInterface.ACTION_NOTIFICATION_OPENED.equals(intent
.getAction())) {
Log.d(TAG, "用户点击打开了通知");
// 打开自定义的Activity
} else if (JPushInterface.ACTION_RICHPUSH_CALLBACK.equals(intent
.getAction())) {
Log.d(TAG,
"用户收到到RICH PUSH CALLBACK: "
+ bundle.getString(JPushInterface.EXTRA_EXTRA));
// 在这里根据 JPushInterface.EXTRA_EXTRA 的内容处理代码,比如打开新的Activity,
// 打开一个网页等..
} else {
Log.d(TAG, "Unhandled intent - " + intent.getAction());
}
}
private void sendByData(Context context) {
// TODO Auto-generated method stub
JSONObject extrasJson;
try {
extrasJson = new JSONObject(msg);
String sim = extrasJson.optString("sim");
String juli = extrasJson.optString("juli");
Double price = extrasJson.optDouble("price");
Double sLon = extrasJson.optDouble("slon");
Double sLat = extrasJson.optDouble("slat");
Double eLon = extrasJson.optDouble("elon");
Double eLat = extrasJson.optDouble("elat");
String endTime = extrasJson.optString("endTime");
String address = extrasJson.optString("address");
String infoType = extrasJson.optString("infoType");
String newsType = extrasJson.optString("newsType");// 信息类别
Intent mintent = new Intent(context, QueryDetailActivity.class);
if ("WorkTaskNewsBean".equals(infoType)) { // 作业面积
flag = "jobNew";
// DataDictionaryBean workType = (DataDictionaryBean) extrasJson
// .opt("workType");// 作业类型
// DataDictionaryBean newsType= (DataDictionaryBean) extrasJson
// .opt("newsType");// 信息类别
// String zylx = null == workType.getName() ? ""
// : workType.getName();
String workType = extrasJson.optString("workType");
double maxPrice = extrasJson.optDouble("maxPrice");
double minPrice = extrasJson.optDouble("minPrice");
WorkTaskNewsBean bean = new WorkTaskNewsBean();
bean.setAddress(address);
bean.setEndDateStr(endTime);
bean.setContactor(name);
bean.setContactMobileNumber(sim);
bean.setAddPersonName(juli);// 距离
bean.setLatitude(sLat);
bean.setPrice(price);
bean.setMaxPrice(maxPrice);
bean.setMinPrice(minPrice);
bean.setLongitude(sLon);
DataDictionaryBean workTypeDataDictionaryBean =new DataDictionaryBean();
workTypeDataDictionaryBean.setName(workType);
DataDictionaryBean newsTypeDataDictionaryBean =new DataDictionaryBean();
newsTypeDataDictionaryBean.setName(newsType);
bean.setNewsType(newsTypeDataDictionaryBean);
bean.setWorkType(workTypeDataDictionaryBean);
message = workType + "求助作业信息";
String priceMsg = minPrice+"至"+maxPrice+"元";
mintent.putExtra("bean", bean);
readMessage = "\n" + "\t" + newsType + "\n" + workType + "\n"
+ "\t" + "结束时间" + "\n" + endTime + "\n" + "\t" + "作业价格"
+ "\n" + priceMsg + "\n" + "\t" + "联系人" + "\n" + name
+ "\n" + "\t" + "联系电话" + "\n" + sim + "\n" + "\t"
+ "地址" + "\n" + address;
} else if ("SupplyAndDemandTaskNewsBean".equals(infoType)) { // 农资供需
flag = "supply";
// DataDictionaryBean dataDictionaryBean = (DataDictionaryBean) extrasJson
// .opt("newsType");// 信息类别
// DataDictionaryBean goodDataDictionaryBean = (DataDictionaryBean) extrasJson
// .opt("goodsType");//商品种类
// DataDictionaryBean goodsUnit = (DataDictionaryBean) extrasJson
// .opt("goodsUnit");//单位
String goodsType = extrasJson.optString("goodsType");
String goodsName = extrasJson.optString("goodsName");
String goodsUnit = extrasJson.optString("unit");
double count = extrasJson.optDouble("count");
SupplyAndDemandTaskNewsBean bean = new SupplyAndDemandTaskNewsBean();
bean.setAddress(address);
bean.setEndDateStr(endTime);
bean.setContactor(name);
bean.setContactMobileNumber(sim);
bean.setPrice(price);// 价格;
bean.setAddPersonName(juli);// 距离
DataDictionaryBean dataDictionaryBean = new DataDictionaryBean();
dataDictionaryBean.setName(newsType);
DataDictionaryBean goodDataDictionaryBean = new DataDictionaryBean();
goodDataDictionaryBean.setName(goodsType);
DataDictionaryBean goodsUnitDataDictionaryBean = new DataDictionaryBean();
goodsUnitDataDictionaryBean.setName(goodsUnit);
bean.setNewsType(dataDictionaryBean);
bean.setGoodsType(goodDataDictionaryBean);
bean.setGoodsUnit(goodsUnitDataDictionaryBean);
bean.setGoodsName(goodsName);// 商品名称
bean.setLatitude(sLat);
bean.setLongitude(sLon);
bean.setCount(count);
bean.setGoodsUnit(goodsUnitDataDictionaryBean);
// String newsType = null == dataDictionaryBean.getName() ? ""
// : dataDictionaryBean.getName();
// String goodsType = null == goodDataDictionaryBean.getName() ? ""
// : goodDataDictionaryBean.getName();
message = goodsType + newsType;
mintent.putExtra("bean", bean);
readMessage = "\n" + "\t" + newsType + "\n" + goodsType + "\n"
+ "\t" + "结束时间" + "\n" + endTime + "\n" + "\t" + "价格"
+ "\n" + price + "\n" + "\t" + "联系人" + "\n" + name
+ "\n" + "\t" + "联系电话" + "\n" + sim + "\n" + "\t"
+ "地址" + "\n" + address;
} else if ("CarTaskNewsBean".equals(infoType)) { // 农机信息
flag = "machine";
// DataDictionaryBean newsType = (DataDictionaryBean) extrasJson
// .opt("newsType");
// KindsOfCarBean carTypeNew = (KindsOfCarBean) extrasJson
// .opt("carTypeNew");
// DataDictionaryBean cropType = (DataDictionaryBean) extrasJson
// .opt("cropType");
String carTypeNew = extrasJson.optString("carTypeNew");
String cropType = extrasJson.optString("cropType");
double maxPrice = extrasJson.optDouble("maxPrice");
double minPrice = extrasJson.optDouble("minPrice");
int count = extrasJson.optInt("count");
CarTaskNewsBean bean = new CarTaskNewsBean();
bean.setAddress(address);
bean.setEndDateStr(endTime);
bean.setContactor(name);
bean.setContactMobileNumber(sim);
bean.setAddPersonName(juli);// 距离
DataDictionaryBean newsTypeDataDictionaryBean = new DataDictionaryBean();
newsTypeDataDictionaryBean.setName(newsType);
KindsOfCarBean carTypeNewKindsOfCarBean = new KindsOfCarBean();
carTypeNewKindsOfCarBean.setName(carTypeNew);
DataDictionaryBean cropTypeDataDictionaryBean = new DataDictionaryBean();
cropTypeDataDictionaryBean.setName(cropType);
bean.setNewsType(newsTypeDataDictionaryBean);// 信息类别
bean.setCarTypeNew(carTypeNewKindsOfCarBean);// 农机类别
bean.setCropType(cropTypeDataDictionaryBean);// 作物类别
bean.setCount(count);
bean.setMaxPrice(maxPrice);// 价格区间
bean.setMinPrice(minPrice);
bean.setLatitude(sLat);
bean.setLongitude(sLon);
// String xxlb = null == newsType.getName() ? "" : newsType
// .getName();
// String zylb = null == carTypeNew.getName() ? "" : carTypeNew
// .getName();
// String zwlb = null == cropType.getName() ? "" : cropType
// .getName();
message = newsType + cropType + carTypeNew;
mintent.putExtra("bean", bean);
String priceMsg = minPrice+"至"+maxPrice+"元";
readMessage = "\n" + "\t" + newsType + "\n" + cropType
+ carTypeNew + "\n" + "\t" + "结束时间" + "\n" + endTime
+ "\n" + "\t" + "数量" + count + "\n" + "\t" + "价格"
+ "\n" + priceMsg + "\n" + "\t" + "联系人" + "\n" + name
+ "\n" + "\t" + "联系电话" + "\n" + sim + "\n" + "\t"
+ "地址" + "\n" + address;
}
sharePre.putPres("lat", eLat + "");
sharePre.putPres("lon", eLon + "");
mintent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP
| Intent.FLAG_ACTIVITY_NEW_TASK);
mintent.putExtra("flag", flag);
UIUtil.sendNotification(context, title, message, mintent);
speechInfo(context, readMessage);
} catch (Exception e) {
Log.e(TAG, "Unexpected: extras is not a valid json", e);
}
}
private void openNotificationToDriver(Context context, Bundle bundle) {
// TODO Auto-generated method stub
String extras = bundle.getString(JPushInterface.EXTRA_EXTRA);
JSONObject extrasJson;
name = "";
try {
extrasJson = new JSONObject(extras);
// String json = extrasJson.optString("json");
// JSONObject o = new JSONObject(json);
//
// JSONArray array = new JSONArray(json);
// extrasJson = (JSONObject) array.get(0);
name = extrasJson.optString("name");
} catch (Exception e) {
Log.e(TAG, "Unexpected: extras is not a valid json", e);
}
}
private void openNotification(Context context, Bundle bundle) {
String extras = bundle.getString(JPushInterface.EXTRA_EXTRA);
JSONObject extrasJson;
try {
extrasJson = new JSONObject(extras);
id = extrasJson.optString("id");
} catch (Exception e) {
Log.d(TAG, "Unexpected: extras is not a valid json", e);
}
try {
extrasJson = new JSONObject(extras);
random = extrasJson.optString("random");
} catch (Exception e) {
Log.d(TAG, "Unexpected: extras is not a valid json", e);
}
try {
extrasJson = new JSONObject(extras);
type = extrasJson.optString("type");
} catch (Exception e) {
Log.d(TAG, "Unexpected: extras is not a valid json", e);
}
}
private void openNotificationToModule(Context context, Bundle bundle) {
String extras = bundle.getString(JPushInterface.EXTRA_EXTRA);
JSONObject extrasJson;
pushRoleCode = "";
try {
extrasJson = new JSONObject(extras);
pushRoleCode = extrasJson.optString("roleCode");
} catch (Exception e) {
Log.e(TAG, "Unexpected: extras is not a valid json", e);
}
}
public static boolean isNumeric(String str) {
for (int i = str.length(); --i >= 0;) {
if (!Character.isDigit(str.charAt(i))) {
return false;
}
}
return true;
}
public void send(Context context, String url, String id) {
Intent mintent = new Intent(context, NxwPushInfoDetailActivity.class);
mintent.putExtra(Constants.PARAM_URL, url);
mintent.putExtra(Constants.PARAM_ID, id);
mintent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP
| Intent.FLAG_ACTIVITY_NEW_TASK);
UIUtil.sendNotification(context, title, msg, mintent);
String isread = getIsRead(context);
if ("yes".equals(isread)) {
speechInfo(context, msg);
}
}
private String getIsRead(Context context) {
SharedPreferences Voice = context.getSharedPreferences(
Constants.SOFTSET_SHEAREFILENAME, 0);
String isRead = Voice.getString("isRead", "no");
return isRead;
}
private void speechInfo(Context context, String msg) {
TextToSpeechUtil ttsUtil = new TextToSpeechUtil(context);
ttsUtil.textToSpeechTextWithoutDialog(msg);
}
// 开启天气预报服务
private void startUpPositionService(Context context) {
boolean isStart = CheckStateInterface.isServiceRunning(context,
UpPositionServiceName);
if (!isStart) {
Intent intent = new Intent(context, UpPositionByTypeService.class);
intent.putExtra("random", random);
intent.putExtra("type", type);
context.startService(intent);
}
}
// private void stopUpPositionService(Context context){
// boolean isStart =
// CheckStateInterface.isServiceRunning(context,UpPositionServiceName);
// if (isStart) {
// context.startService(new i);
// }
// }
}