极光推送 android 最新,Android——快速集成极光推送-Go语言中文社区

集成极光推送

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添加

b0a2b1799bd191ff486c321e87a476f6.png

AndroidManifest.xml

第六步、混淆

在ProGuard文件(即proguard-rules.pro)中加入混淆代码:

-dontoptimize

-dontpreverify

-dontwarn cn.jpush.**

-keepclasscn.jpush.** { *;}最后  去测试吧~~~~~~

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值