package com.partynetwork.iparty.push;
import org.json.JSONException;
import org.json.JSONObject;
import android.app.AlertDialog;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Handler;
import com.baidu.android.pushservice.PushConstants;
import com.baidu.android.pushservice.PushManager;
import com.lidroid.xutils.util.LogUtils;
import com.partynetwork.dataprovider.util.ApiKeyUtil;
import com.partynetwork.dataprovider.util.L;
import com.partynetwork.dataprovider.util.NetUtil;
import com.partynetwork.dataprovider.util.T;
import com.partynetwork.dataprovider.util.Util;
import com.partynetwork.iparty.R;
import com.partynetwork.iparty.app.AppConfig;
import com.partynetwork.iparty.flipmenu.Main;
/**
* Push消息处理receiver
*/
public class PushMessageReceiver extends BroadcastReceiver {
AlertDialog.Builder builder;
/**
* @param context
* Context
* @param intent
* 接收的intent
*/
@Override
public void onReceive(final Context context, Intent intent) {
L.d(">>> Receive intent: \r\n" + intent);
if (intent.getAction().equals(PushConstants.ACTION_MESSAGE)) {
// 获取消息内容
String message = intent.getExtras().getString(
PushConstants.EXTRA_PUSH_MESSAGE_STRING);
// 消息的用户自定义内容读取方式
L.i("onMessage: " + message);
// 自定义内容的json串
L.d("EXTRA_EXTRA = "
+ intent.getStringExtra(PushConstants.EXTRA_EXTRA));
Util.showMsg(context, message);
// 在此自定义处理消息,暂时先不做任何处理,要处理时再次添加方法
} else if (intent.getAction().equals(PushConstants.ACTION_RECEIVE)) {
// 处理绑定等方法的返回数据
// PushManager.startWork()的返回值通过PushConstants.METHOD_BIND得到
// 获取方法
final String method = intent
.getStringExtra(PushConstants.EXTRA_METHOD);
// 方法返回错误码。若绑定返回错误(非0),则应用将不能正常接收消息。
// 绑定失败的原因有多种,如网络原因,或access token过期。
// 请不要在出错时进行简单的startWork调用,这有可能导致死循环。
// 可以通过限制重试次数,或者在其他时机重新调用来解决。
int errorCode = intent.getIntExtra(PushConstants.EXTRA_ERROR_CODE,
PushConstants.ERROR_SUCCESS);
String content = "";
if (intent.getByteArrayExtra(PushConstants.EXTRA_CONTENT) != null) {
// 返回内容
content = new String(
intent.getByteArrayExtra(PushConstants.EXTRA_CONTENT));
}
// 在此自定义处理消息,暂时先不做任何处理,要处理时再次添加方法
LogUtils.i("onMessage: method : " + method);
LogUtils.i("onMessage: result : " + errorCode);
LogUtils.i("onMessage: content : " + content);
paraseContent(context, errorCode, content);// 处理消息
// 可选。通知用户点击事件处理
} else if (intent.getAction().equals(
PushConstants.ACTION_RECEIVER_NOTIFICATION_CLICK)) {
L.d("intent=" + intent.toUri(0));
// 自定义内容的json串
L.d("EXTRA_EXTRA = "
+ intent.getStringExtra(PushConstants.EXTRA_EXTRA));
Intent aIntent = new Intent();
aIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
aIntent.setClass(context, Main.class);
context.startActivity(aIntent);
}
}
/**
* 处理登录结果
*
* @param errorCode
* @param content
*/
private void paraseContent(final Context context, int errorCode,
String content) {
// TODO Auto-generated method stub
if (errorCode == 0) {
String appid = "";
String channelid = "";
String userid = "";
try {
JSONObject jsonContent = new JSONObject(content);
JSONObject params = jsonContent
.getJSONObject("response_params");
appid = params.getString("appid");
channelid = params.getString("channel_id");
userid = params.getString("user_id");
} catch (JSONException e) {
L.e("Parse bind json infos error: " + e);
}
AppConfig config = AppConfig.getAppConfig(context);
config.setBaiduUserId(userid);
config.setBaiduChannel(channelid);
LogUtils.i("baiduid:" + userid);
LogUtils.i("channelid:" + channelid);
} else {
if (NetUtil.isNetConnected(context)) {
if (errorCode == 30607) {
T.showLong(context, "账号已过期,请重新登录");
// 跳转到重新登录的界面
} else {
T.showLong(context, "启动失败,正在重试...");
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
// TODO Auto-generated method stub
PushManager
.startWork(context,
PushConstants.LOGIN_TYPE_API_KEY,
ApiKeyUtil.getMetaValue(context,
"api_key"));
}
}, 2000);// 两秒后重新开始验证
}
} else {
T.showLong(context, R.string.net_error_tip);
}
}
}
}