极光推送集成

之前一直好奇项目中的推送是怎么显示到通知栏里的,于是现在有时间就自己阅读了一下文档,集成了一下Jpush 。

Jpush提供了4中类型的消息:通知、自定义消息、富媒体和本地通知

Jpush 的通知类型的消息就是一种notification,可以直接显示到Android手机的通知栏上,通过查阅文档发现是否显示到手机的通知栏和显示的样式都是可以修改

详细的可以查看(https://docs.jiguang.cn/jpush/server/push/rest_api_v3_push/#notification)


集成过程:之前自己做过手动集成,发现比较繁琐,这次就选择了 jcenter自动集成的方法

1、确保project的主gradle中配置了jcenter的集成

buildscript {
    repositories {
        jcenter()
    }
    ......
}

allprojects {
    repositories {
        jcenter()
    }
}

2、在 module的gradle中添加配置文件的配置变量和依赖

  1. defaultConfig {  
  2.         //极光推送  
  3.         ndk {  
  4.             //选择要添加的对应cpu类型的.so库。  
  5.             abiFilters 'armeabi''armeabi-v7a''arm64-v8a'  
  6.             // 还可以添加 'x86', 'x86_64', 'mips', 'mips64'  
  7.         }  
  8.         manifestPlaceholders = [  
  9.                 JPUSH_PKGNAME: "集成推送的包名",  
  10.                 JPUSH_APPKEY : "申请的appkey"//JPush上注册的包名对应的appkey.  
  11.                 JPUSH_CHANNEL: "developer-default"//用户渠道统计的渠道名称  
  12.         ]  
  13.     } 
  14. dependencies{
  15. compile 'cn.jiguagn.sdk:jpush:3.0.5'
  16. compile 'cn.jiguang.sdk.jcore:1.1.2'
  17. }


3、配置后build_gradle,如果出现这个问题

NDK integration is deprecated in the current plugin......在gradle_properties中添加

android.usdDeprecatedNdk=true

4、配置清单文件

empty

5、在app的application中初始化jpush

JpushInterface.setDebugMode(true);

JpushInterface.init(this);


6、设置别名(个人认为是后台推送消息区分用户的标识)

public class App extends Application {

    private static final int MSG_SET_ALIAS = 1001;
    
    public static SharedPreferences preference; 
    
    public static SharedPreferences.Editor editor;

    @Override
    public void onCreate() {
        super.onCreate();
        
        preference = getSharedPreferences("config", Context.MODE_PRIVATE);
        editor = preference.edit();
        JPushInterface.setDebugMode(true);
        JPushInterface.init(this);
      
        
    }
    
    /**
     * 设置别名
     */
    private void setAlies(String alies) {
        // 调用 Handler 来异步设置别名
        mHandler.sendMessage(mHandler.obtainMessage(MSG_SET_ALIAS, alies));
    }
    private final TagAliasCallback mAliasCallback = new TagAliasCallback() {
        @Override
        public void gotResult(int code, String alias, Set
   
   
    
     tags) {
            String logs ;
            switch (code) {
                case 0:
                    logs = "Set tag and alias success";
                    Log.i("tag", logs);
                    editor.putBoolean("jpushAlias", true).commit();
                    break;
                case 6002:
                    logs = "Failed to set alias and tags due to timeout. Try again after 60s.";
                    Log.i("tag", logs);
                    // 延迟 60 秒来调用 Handler 设置别名
                    mHandler.sendMessageDelayed(mHandler.obtainMessage(MSG_SET_ALIAS, alias), 1000 * 60);
                    break;
                default:
                    logs = "Failed with errorCode = " + code;
                    Log.e("tag", logs);
            }
//            ExampleUtil.showToast(logs, getApplicationContext());
        }
    };
   
    private final Handler mHandler = new Handler() {
        @Override
        public void handleMessage(android.os.Message msg) {
            super.handleMessage(msg);
            switch (msg.what) {
                case MSG_SET_ALIAS:
                    Log.d("tag", "Set alias in handler.");
                    // 调用 JPush 接口来设置别名。
                    JPushInterface.setAliasAndTags(getApplicationContext(),
                            (String) msg.obj,
                            null,
                            mAliasCallback);
                    break;
                default:
                    Log.i("tag", "Unhandled msg - " + msg.what);
            }
        }
    };
}
   
   

7、处理接收到的推送、比如点击通知栏跳转到指定的activity(自选)

public class MyReceiver extends BroadcastReceiver {
    private static final String TAG = "JPush";

    @Override
    public void onReceive(Context context, Intent intent) {
        Bundle bundle = intent.getExtras();
        Log.d(TAG, "[MyReceiver] 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, "[MyReceiver] 接收Registration Id : " + regId);
            //send the Registration Id to your server...
        } else if (JPushInterface.ACTION_MESSAGE_RECEIVED.equals(intent.getAction())) {
            Log.d(TAG, "[MyReceiver] 接收到推送下来的自定义消息: " + bundle.getString(JPushInterface.EXTRA_MESSAGE));
         // 处理 处理自定义 
        } else if (JPushInterface.ACTION_NOTIFICATION_RECEIVED.equals(intent.getAction())) {
            Log.d(TAG, "[MyReceiver] 接收到推送下来的通知");
            int notifactionId = bundle.getInt(JPushInterface.EXTRA_NOTIFICATION_ID);
            Log.d(TAG, "[MyReceiver] 接收到推送下来的通知的ID: " + notifactionId);

        } else if (JPushInterface.ACTION_NOTIFICATION_OPENED.equals(intent.getAction())) {
            Log.d(TAG, "[MyReceiver] 用户点击打开了通知");
            String result = bundle.getString(JPushInterface.EXTRA_EXTRA);
            com.alibaba.fastjson.JSONObject obj = JSON.parseObject(result);
            String str = obj.getString("type");
            if ("oa".equals(str)) {
                if (MyApplication.getInstance().getUser() != null &&
                        !TextUtils.isEmpty(MyApplication.getInstance().getUser().get_id())) {
                    //打开自定义的Activity
                    Intent i = new Intent(context, Activity_Test.class);
                    i.putExtras(bundle);
                    //在APP外部打开内部activity需添加flag
                    i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                    i.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                    context.startActivity(i);
                } else {
                    Intent i = new Intent(context, Activity_login.class);
                    context.startActivity(i);
                }
            }
        } else if (JPushInterface.ACTION_RICHPUSH_CALLBACK.equals(intent.getAction())) {
            Log.d(TAG, "[MyReceiver] 用户收到到RICH PUSH CALLBACK: " + bundle.getString(JPushInterface.EXTRA_EXTRA));
            //在这里根据 JPushInterface.EXTRA_EXTRA 的内容处理代码,比如打开新的Activity, 打开一个网页等..

        } else if (JPushInterface.ACTION_CONNECTION_CHANGE.equals(intent.getAction())) {
            boolean connected = intent.getBooleanExtra(JPushInterface.EXTRA_CONNECTION_CHANGE, false);
            Log.w(TAG, "[MyReceiver]" + intent.getAction() + " connected state change to " + connected);
        } else {
            Log.d(TAG, "[MyReceiver] Unhandled intent - " + intent.getAction());
        }
    }

    // 打印所有的 intent extra 数据
    private static String printBundle(Bundle bundle) {
        StringBuilder sb = new StringBuilder();
        for (String key : bundle.keySet()) {
            if (key.equals(JPushInterface.EXTRA_NOTIFICATION_ID)) {
                sb.append("\nkey:" + key + ", value:" + bundle.getInt(key));
            } else if (key.equals(JPushInterface.EXTRA_CONNECTION_CHANGE)) {
                sb.append("\nkey:" + key + ", value:" + bundle.getBoolean(key));
            } else if (key.equals(JPushInterface.EXTRA_EXTRA)) {
                if (bundle.getString(JPushInterface.EXTRA_EXTRA).isEmpty()) {
                    Log.i(TAG, "This message has no Extra data");
                    continue;
                }

                try {
                    JSONObject json = new JSONObject(bundle.getString(JPushInterface.EXTRA_EXTRA));
                    Iterator
   
   
    
     it = json.keys();

                    while (it.hasNext()) {
                        String myKey = it.next().toString();
                        sb.append("\nkey:" + key + ", value: [" +
                                myKey + " - " + json.optString(myKey) + "]");
                    }
                } catch (JSONException e) {
                    Log.e(TAG, "Get message extra JSON error!");
                }

            } else {
                sb.append("\nkey:" + key + ", value:" + bundle.getString(key));
            }
        }
        return sb.toString();
    }

   
   











  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值