Android 实现极光推送 以及页面的跳转

     当初说坚持更新博客(两天一更),然而没有坚持下来, 一晃一年过去了。。。岁月是把杀猪刀啊,言归正传   

     今天来记录一下第三方应用(极光推送), 之前做过然后这次项目中 需要接入极光推送, 然后我就打开了之前的项目,

然后寻思吧之前的代码拿来,这样岂不是可以省去很多时间,然而并不是。。。。  理想是没美好的,现实是残酷的。因为第三方平台也是在更新, 所以,我就从新打开了极光的官方去看了文档, 然而也并不是很理想,反正有很多的坑,  我对极光做了一次封装,还有页面的跳转 ,这样我们在对接 使用的时候 可以通俗易懂,好了, 接下来就来说一下遇到的一些问题。

  好了 实现第一步 就是添加依赖。

  

    //************************************  极光推送  *************************************************
    implementation 'cn.jiguang.sdk.plugin:xiaomi:3.2.0'
    implementation 'cn.jiguang.sdk.plugin:huawei:3.2.0'
    implementation 'cn.jiguang.sdk.plugin:oppo:3.2.0'
//    implementation 'cn.jiguang.sdk.plugin:meizu:3.2.0'

    //此版本插件仅支持JPushSDK3.1.8及以上版本
    implementation 'cn.jiguang.sdk:jpush:3.2.0'
    implementation 'cn.jiguang.sdk:jcore:1.2.7'

    //************************************  极光推送 end *************************************************

          build.gradle 中的配置, 推送那款机型去那个平台申请 Key  极光也不例外

   

 manifestPlaceholders = [
                JPUSH_PKGNAME : applicationId,
                JPUSH_APPKEY  : "",//JPush上注册的包名对应的appkey.
                JPUSH_CHANNEL : "",//暂时填写默认值即可.
                XIAOMI_APPKEY : "",//小米平台注册的appkey
                XIAOMI_APPID  : "",//小米平台注册的appid
                HUAWEI_APPID  : "",//华为平台注册的appid
                OPPO_APPKEY   : "", // OP
                // PO平台注册的appkey
                OPPO_APPID    : "", // OPPO平台注册的appid
                OPPO_APPSECRET: "",//OPPO平台注册的appsecret
//                MEIZU_APPKEY  : "",//魅族平台注册的appkey
//                MEIZU_APPID   : "",//魅族平台注册的appid
        ]

        配置完成以后 重点来了,  就是 极光的广播服务  和 跳转 页面

      清单文件中配置


    
         
        
         
         
         
         
        
    

    
        

        
    
Application中初始化
    /**
     * 初始化友盟统计
     */
    private void initYouMeng() {
//        //上下文、appkey、下载渠道、设备类型、推送密匙
//        UMConfigure.init(mContext, YMAppkey, "ggb", UMConfigure.DEVICE_TYPE_PHONE, null);
//        MobclickAgent.setScenarioType(mContext, MobclickAgent.EScenarioType.E_UM_NORMAL);

        UMConfigure.init(mContext, UMConfigure.DEVICE_TYPE_PHONE, "");
        UMConfigure.setLogEnabled(isLog);

        // 选用AUTO页面采集模式
        MobclickAgent.setPageCollectionMode(MobclickAgent.PageMode.AUTO);
    }

 

     广播的实现代码

package com.ggmall.ggb.personal.receiver;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;

import com.google.gson.Gson;
import com.orhanobut.logger.Logger;

import cn.jpush.android.api.JPushInterface;

public class JPushReceiver extends BroadcastReceiver {
    private static final String TAG = "JIGUANG---";

    @Override
    public void onReceive(Context context, Intent intent) {
        try {

            Bundle bundle = intent.getExtras();
            Logger.e("极光>>>>>>+"+intent.getExtras());

            if (JPushInterface.ACTION_REGISTRATION_ID.equals(intent.getAction())) {
//                String regId = bundle.getString(JPushInterface.EXTRA_REGISTRATION_ID);
//                Logger.e(TAG + "接收Registration Id : " + regId);

            } else if (JPushInterface.ACTION_MESSAGE_RECEIVED.equals(intent.getAction())) {
                //不走厂商通道
                Logger.e(TAG + "接收到推送下来的自定义消息(内容为): " + bundle.getString(JPushInterface.EXTRA_MESSAGE));


            } else if (JPushInterface.ACTION_NOTIFICATION_RECEIVED.equals(intent.getAction())) {
                Gson gson = new Gson();
                //华为、oppo、vivo厂商通道不走该方法;魅族非运行状态不会触发
                Logger.e(TAG + "接收到推送下来的通知");
                Logger.e(TAG + "接收到推送下来的标题:"+bundle.getString(JPushInterface.EXTRA_TITLE));
                Logger.e(TAG + "接收到推送下来的内容:"+bundle.getString(JPushInterface.EXTRA_MESSAGE));
                Logger.e(TAG + "接收到推送下来的json:"+bundle.getString(gson.toJson(JPushInterface.EXTRA_EXTRA)));

            } else if (JPushInterface.ACTION_NOTIFICATION_OPENED.equals(intent.getAction())) {
                Logger.e(TAG + "用户点击打开了通知");

                //打开自定义的Activity
                Intent i = new Intent(context, PushToActivity.class);
                i.putExtras(bundle);
                i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP);
                context.startActivity(i);

            } else {
                Logger.e(TAG + "Unhandled intent - " + intent.getAction());
            }

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}

 跳转页面的代码

  

package com.ggmall.ggb.personal.receiver;

import android.os.Bundle;
import android.text.TextUtils;

import androidx.appcompat.app.AppCompatActivity;

import com.ggmall.ggb.personal.utils.IntentConfig;
import com.ggmall.ggb.personal.utils.IntentKeyConfig;
import com.google.gson.Gson;
import com.orhanobut.logger.Logger;

import java.util.Map;

import cn.jpush.android.api.JPushInterface;

public class PushToActivity extends AppCompatActivity {


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
//        setContentView(R.layout.activity_push_to);


            String actionType;
            Map map;
            String data;

            /**
             * 华为厂商使用getIntent().getData()
             * 其他厂商使用getIntent().getExtras()
             */
            if (getIntent().getData() == null) {
                Bundle bundle = getIntent().getExtras();
                if (bundle == null) return;

                //oppo通道数据
                data = bundle.getString("JMessageExtra");

                if (TextUtils.isEmpty(data)) {
                    //oppo通道数据为空,其他手机获取数据方式
                    data = bundle.getString(JPushInterface.EXTRA_EXTRA);
                    Logger.e("JIGUANG---data--" + data);
                    NExtrasBean bean = new Gson().fromJson(data, NExtrasBean.class);

                    actionType = bean.getActionType();
                    map = bean.getActionParam();

                } else {
                    //oppo通道数据不为空
                    Logger.e("JIGUANG---oppo--" + data);
                    PushOtherBean bean = new Gson().fromJson(data, PushOtherBean.class);

                    actionType = bean.getN_extras().getActionType();
                    map = bean.getN_extras().getActionParam();
                }

            } else {
                //华为获取数据方式
                data = getIntent().getData().toString();
                Logger.e("JIGUANG---hw--" + data);
                PushOtherBean bean = new Gson().fromJson(data, PushOtherBean.class);

                actionType = bean.getN_extras().getActionType();
                map = bean.getN_extras().getActionParam();
            }


            startA(map, actionType);

//            startB(actionType);

    }


    public void startA(Map map, String actionType)  {
        Logger.e("进入隐式跳转>>");
        try {
            IntentKeyConfig.startIntentImplicitJump(this, map, actionType);
        } catch (Exception e) {
            e.printStackTrace();
        }
        onBackPressed();
    }



    public void startB(String actionType) throws Exception {
        Logger.e("对应type跳转>>");
        IntentConfig.startIntentImplicitJump(this, actionType);
        onBackPressed();
    }

}

  下面就是跳转的封装  (包含隐式跳转和显示跳转)

 

package com.ggmall.ggb.personal.utils;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;

import com.alibaba.baichuan.trade.biz.login.AlibcLogin;
import com.alibaba.baichuan.trade.biz.login.AlibcLoginCallback;
import com.ggmall.ggb.personal.app.App;
import com.ggmall.ggb.personal.base.BaseEntry;
import com.ggmall.ggb.personal.base.BaseObserver;
import com.ggmall.ggb.personal.base.bean.SerializableMapBean;
import com.ggmall.ggb.personal.sp.SPKey;
import com.ggmall.ggb.personal.ui.X5WebViewAcitivty;
import com.ggmall.ggb.personal.ui.home.activity.AlbcWebActivity;
import com.ggmall.ggb.personal.ui.home.bean.ChecktbkauthbindBean;
import com.ggmall.ggb.personal.ui.home.bean.GoodsBuyBean;
import com.ggmall.ggb.personal.ui.home.homeModel.HomeModel;
import com.ggmall.ggb.personal.ui.home.homeModel.JumpLinkTransfer;
import com.orhanobut.logger.Logger;

import java.util.HashMap;
import java.util.Map;

/**
 * Intent传参 put key值
 * 与隐式跳转
 * Created by ${ShinnyYang} on 2020/2/18.
 */
public class IntentKeyConfig {
    public static final String DATA = "data"; //"actionParam"字段专用,其他字段不要用
    public static final String ACTION_TYPE = "actionType";  //"actionType"字段专用,其他字段不要用

    public static final String ARGS_PAGE = "args_page";

    public static final String POSITION = "position";


    /**
     * 隐式跳转intent
     *
     * @param mContext
     * @param map        所传参数map
     * @param actionType 页面action
     */
    public static void startIntentImplicitJump(Context mContext, Map map, String actionType) throws Exception {
        Logger.e("mainAdapter跳转参数==>" + actionType + "===>" + map);

        //处理请求参数
        Map m = new HashMap<>();
        m = ActionTypeMapUtil.getActionTypeMap(map);
        String token = App.userInfoSp.getString(SPKey.UserInfoSp.token, "");
        switch (actionType) {
   
            case "taobao_web":
                
                break;

            default:
                SerializableMapBean bean = new SerializableMapBean();
                bean.setMap(map);

                Intent intent = new Intent(mContext.getPackageName() + actionType);
                Bundle args = new Bundle();
                args.putSerializable(DATA, bean);
                args.putString(ACTION_TYPE, actionType);

                intent.putExtras(args);
                mContext.startActivity(intent);
                break;
        }
    }

    /**
     * 显示跳转intent
     *
     * @param mContext
     * @param map      所传参数map
     */
    public static void startIntentJump(Context mContext, Map map, Class activity) {
        SerializableMapBean bean = new SerializableMapBean();
        bean.setMap(map);

        Intent intent = new Intent(mContext, activity);
        Bundle args = new Bundle();
        args.putSerializable(DATA, bean);
        intent.putExtras(args);
        mContext.startActivity(intent);
    }

    /**
     * 显示跳转intent
     *
     * @param mContext
     * @param map         所传参数map
     * @param requestCode
     */
    public static void startIntentJump(Context mContext, Map map,
                                       Class activity, int requestCode) {
        SerializableMapBean bean = new SerializableMapBean();
        bean.setMap(map);

        Intent intent = new Intent(mContext, activity);
        Bundle args = new Bundle();
        args.putSerializable(DATA, bean);
        intent.putExtras(args);
        ((Activity) mContext).startActivityForResult(intent, requestCode);
    }

    /**
     * 阿里百川登陆组件
     *
     * @param mContext
     * @param url
     */
    public static void albcLogin(Context mContext, String url) {
        AlibcLogin.getInstance().showLogin(new AlibcLoginCallback() {
            @Override
            public void onSuccess(int loginResult, String openId, String userNick) {
                Intent intent = new Intent(mContext, AlbcWebActivity.class);
                intent.putExtra(AlbcWebActivity.URLS, url);
                mContext.startActivity(intent);
            }

            @Override
            public void onFailure(int code, String msg) {
            }
        });
    }


}

        完成!!!!!!

     

你可能感兴趣的:(Android 实现极光推送 以及页面的跳转)