转载请标明出处
http://blog.csdn.net/mohan6/article/details/74133186
本文作者:【默寒的博客】
前言
前段时间针对集成极光推送写了篇文章( Android集成极光推送和踩过的坑),后来提测以后发现了各种问题。一直没时间总结一下,趁着周末有点时间,赶紧把这段时间里针对Push这块儿遇到的问题梳理一下。并且对上篇文章 《Android集成极光推送和踩过的坑》中一些错误进行更正,因需求变更出现的一些连带的问题的处理方法做一下总结。
一、跳转逻辑的更正
上篇文章中我用的以下方法判断的前后台,遍历正在运行的所有的进程,看list里第一个正在运行的进程是否是我们自己的进程,是就return true,不是就return false。进而判断我们的进程是否处于前台。
/** * 判断进程是否在后台 * * @param context * @return */ public static boolean isBackground(Context context) { ActivityManager activityManager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE); List<RunningAppProcessInfo> appProcesses = activityManager.getRunningAppProcesses(); for (RunningAppProcessInfo appProcess : appProcesses) { if (appProcess.processName.equals(context.getPackageName())) { LogUtil.i("ym", appProcess.processName + "前台"); return false; } else { LogUtil.i("ym", appProcess.processName + "后台"); return true; } } return false; }
提测以后发现在Android7.0的系统上会出现前后台判断误差。Android7.0是多任务处理机制,home键以后,会出现前台进程会有多个的情况,只拿第一个去判断我们的进程是否在前台变的不可靠。后台我们的需求变更了,要求“打开应用”,如果进处于后台,之前是什么页面就是什么页面,而不是每次都打开"首页”。那么问题就来了,我怎么知道按home键的时候的activity是哪个activity。期初以为通过intent标记就可以做到,尝试以后发现不起作用。后来去请教了大神,大神给我提供了一种方法获取当前栈顶的activity,废话不多说,直接上代码。
/** * Created by ym on 2017/5/27. * 自定义极光推送的广播接受者(v1.3.0新增) * 2017.6.30 v2.0.0修改:删v1.3.0前后台判断统一处理,新增议价消息跳转刷新逻辑 ym */ public class MyJPushReceiver extends BroadcastReceiver { private static final String TAG = "JPush"; @Override public void onReceive(Context context, Intent intent) { Bundle bundle = intent.getExtras(); LogUtil.e(TAG, "[MyReceiver] onReceive - " + intent.getAction() + ", extras: " + printBundle(bundle)); if (JPushInterface.ACTION_REGISTRATION_ID.equals(intent.getAction())) { String regId = bundle.getString(JPushInterface.EXTRA_REGISTRATION_ID); LogUtil.e(TAG, "[MyReceiver] 接收Registration Id : " + regId); } else if (JPushInterface.ACTION_MESSAGE_RECEIVED.equals(intent.getAction())) { LogUtil.e(TAG, "[MyReceiver] 接收到推送下来的自定义消息: " + bundle.getString(JPushInterface.EXTRA_MESSAGE)); // processCustomMessage(context, bundle); } else if (JPushInterface.ACTION_NOTIFICATION_RECEIVED.equals(intent.getAction())) { LogUtil.e(TAG, "[MyReceiver] 接收到推送下来的通知"); int notificationId = bundle.getInt(JPushInterface.EXTRA_NOTIFICATION_ID); LogUtil.e(TAG, "[MyReceiver] 接收到推送下来的通知的ID: " + notificationId); } else if (JPushInterface.ACTION_NOTIFICATION_OPENED.equals(intent.getAction())) { LogUtil.e(TAG, "[MyReceiver] 用户点击打开了通知"); SharePreferenceUtil share = new SharePreferenceUtil(context); //解析json String string = bundle.getString(JPushInterface.EXTRA_EXTRA);//json串 LogUtil.e(TAG, "=====###########" + string); try { JSONObject jsonObject = new JSONObject(string); String type = jsonObject.getString("type"); LogUtil.e(TAG, "type:" + type); Activity ac = com.carspass.common.util.ActivityManager.getAppManager().currentActivity(); switch (type) { case "1"://打开应用 Intent i = new Intent(); if (ac != null) {//前后/后台---之前的界面 i.setComponent(ac.getComponentName()); } else {//杀死进程--重启 i.setClass(context, ACT_Main.class); } i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | FLAG_ACTIVITY_SINGLE_TOP); context.startActivity(i); break; case "2"://打开创建订单页 String sourse_id = jsonObject.getString("sourse_id"); if (!TextUtils.isEmpty(sourse_id)) { Intent intentOrder = new Intent(context, ACT_PlaceOrder.class); intentOrder.putExtra("id", Integer.parseInt(sourse_id)); intentOrder.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); if (ac != null) {//前台/后台---之前的界面+创建订单页 context.startActivity(intentOrder); } else {//杀死进程--重启-首页+创建订单页 Intent intentMain = new Intent(context, ACT_Main.class); intentMain.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP); Intent[] intents = {intentMain, intentOrder}; context.startActivities(intents); } } break; case "3"://打开品牌 String brand_id = jsonObject.getString("brand_id"); String bra_name = jsonObject.getString("bra_name"); if (!TextUtils.isEmpty(brand_id)) { Intent intentBrand = new Intent(context, ACT_BrandCarList.class); intentBrand.putExtra("id", brand_id); intentBrand.putExtra("title", bra_name); intentBrand.setFlags(Intent.FLAG_ACTIVITY