集成极光推送
1,首先肯定是注册,添加应用
2,开始自动集成比手动集成简单第一步 在 build.gradle
defaultConfig {
multiDexEnabledtrue
applicationId rootProject.ext.cfg.applicationId
minSdkVersionrootProject.ext.cfg.minSdkVersion
targetSdkVersionrootProject.ext.cfg.targetSdkVersion
versionCode rootProject.ext.pkg.versionCode
versionName rootProject.ext.pkg.versionName
testInstrumentationRunner"android.support.test.runner.AndroidJUnitRunner"
vectorDrawables.useSupportLibrarytrue
jackOptions {
enabledtrue
}
// dex突破65535的限制
multiDexEnabledtrue
//默认是umeng的渠道
manifestPlaceholders = [UMENG_CHANNEL_VALUE:"umeng"]
ndk {
//选择要添加的对应cpu类型的.so库(不需要的删除即可)。
abiFilters'armeabi','armeabi-v7a','armeabi-v8a','x86','x86_64','mips','mips64'
}
manifestPlaceholders= [
JPUSH_PKGNAME:applicationId,
JPUSH_APPKEY:"your appkey",//JPush上注册的包名对应的appkey(*换成你的*)
JPUSH_CHANNEL:"developer-default",//暂时填写默认值即可.
]
}
第二步
还是Module的build.gradle文件中哦
compile'cn.jiguang:jpush:2.1.8'// 此处以SDK 2.1.8版本为例
编译一下
如果报错的话,需要在Project的gradle.properties文件中添加下面的代码:
android.useDeprecatedNdk=true第三步 在MyApplication继承 Application中onCreat()方法中
初始化sdk
JPushInterface.setDebugMode(true);//正式版的时候设置false,关闭调试
JPushInterface.init(this);
//建议添加tag标签,发送消息的之后就可以指定tag标签来发送了
Set set =newHashSet<>();
set.add("xunChang");//名字任意,可多添加几个
set.add("Home");
set.add("Expand");
set.add("Mine");
JPushInterface.setTags(this,set, null);//设置标签第四步 自定义广播
packagecom.etcxc.android.base;
importandroid.content.BroadcastReceiver;
importandroid.content.Context;
importandroid.content.Intent;
importandroid.os.Bundle;
importandroid.text.TextUtils;
importandroid.util.Log;
importorg.json.JSONException;
importorg.json.JSONObject;
importjava.util.Iterator;
importcn.jpush.android.api.JPushInterface;
/**
*自定义接收器
*
*如果不定义这个Receiver,则:
* 1)默认用户会打开主界面
* 2)接收不到自定义消息
*/
public classMyReceiverextendsBroadcastReceiver{
private static finalStringTAG="JIGUANG-Example";
@Override
public voidonReceive(Context context,Intent intent) {
try{
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));
processCustomMessage(context,bundle);
}else if(JPushInterface.ACTION_NOTIFICATION_RECEIVED.equals(intent.getAction())) {
Log.d(TAG,"[MyReceiver]接收到推送下来的通知");
intnotifactionId = 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]用户点击打开了通知");
// //打开自定义的Activity
// Intent i = new Intent(context, TestActivity.class);
// i.putExtras(bundle);
// //i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
// i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP );
// 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())) {
booleanconnected = 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());
}
}catch(Exception e){
}
}
//打印所有的intent extra数据
private staticStringprintBundle(Bundle bundle) {
StringBuilder sb =newStringBuilder();
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(TextUtils.isEmpty(bundle.getString(JPushInterface.EXTRA_EXTRA))) {
Log.i(TAG,"This message has no Extra data");
continue;
}
try{
JSONObject json =newJSONObject(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));
}
}
returnsb.toString();
}
//send msg to MainActivity
private voidprocessCustomMessage(Context context,Bundle bundle) {
// if (MainActivity.isForeground) {
// String message = bundle.getString(JPushInterface.EXTRA_MESSAGE);
// String extras = bundle.getString(JPushInterface.EXTRA_EXTRA);
// Intent msgIntent = new Intent(MainActivity.MESSAGE_RECEIVED_ACTION);
// msgIntent.putExtra(MainActivity.KEY_MESSAGE, message);
// if (!ExampleUtil.isEmpty(extras)) {
// try {
// JSONObject extraJson = new JSONObject(extras);
// if (extraJson.length() > 0) {
// msgIntent.putExtra(MainActivity.KEY_EXTRAS, extras);
// }
// } catch (JSONException e) {
//
// }
//
// }
// LocalBroadcastManager.getInstance(context).sendBroadcast(msgIntent);
// }
}
}第五步 注册广播 AndroidManifest.xml 并添加相应权限 ,看官网demo添加
AndroidManifest.xml
第六步、混淆
在ProGuard文件(即proguard-rules.pro)中加入混淆代码:
-dontoptimize
-dontpreverify
-dontwarn cn.jpush.**
-keepclasscn.jpush.** { *;}最后 去测试吧~~~~~~