Android——快速集成極光推送

集成極光推送

1,首先肯定是注冊(cè),添加應(yīng)用

2,開(kāi)始自動(dòng)集成比手動(dòng)集成簡(jiǎn)單?

第一步 在 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

//默認(rèn)是umeng的渠道

manifestPlaceholders = [UMENG_CHANNEL_VALUE:"umeng"]

ndk {

//選擇要添加的對(duì)應(yīng)cpu類(lèi)型的.so庫(kù)(不需要的刪除即可)。

abiFilters'armeabi','armeabi-v7a','armeabi-v8a','x86','x86_64','mips','mips64'

}

manifestPlaceholders= [

JPUSH_PKGNAME:applicationId,

JPUSH_APPKEY:"your appkey",//JPush上注冊(cè)的包名對(duì)應(yīng)的appkey(*換成你的*)

JPUSH_CHANNEL:"developer-default",//暫時(shí)填寫(xiě)默認(rèn)值即可.

]

}


第二步

還是Module的build.gradle文件中哦

compile'cn.jiguang:jpush:2.1.8'// 此處以SDK 2.1.8版本為例

編譯一下

如果報(bào)錯(cuò)的話(huà),需要在Project的gradle.properties文件中添加下面的代碼:

android.useDeprecatedNdk=true

第三步 ? ?在MyApplication繼承 Application中onCreat()方法中

初始化sdk

JPushInterface.setDebugMode(true);//正式版的時(shí)候設(shè)置false,關(guān)閉調(diào)試

JPushInterface.init(this);

//建議添加tag標(biāo)簽,發(fā)送消息的之后就可以指定tag標(biāo)簽來(lái)發(fā)送了

Set set =newHashSet<>();

set.add("xunChang");//名字任意,可多添加幾個(gè)

set.add("Home");

set.add("Expand");

set.add("Mine");

JPushInterface.setTags(this,set, null);//設(shè)置標(biāo)簽

第四步 ? 自定義廣播

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;

/**

*自定義接收器

*

*如果不定義這個(gè)Receiver,則:

* 1)默認(rèn)用戶(hù)會(huì)打開(kāi)主界面

* 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]接收到推送下來(lái)的自定義消息: "+ bundle.getString(JPushInterface.EXTRA_MESSAGE));

processCustomMessage(context,bundle);

}else if(JPushInterface.ACTION_NOTIFICATION_RECEIVED.equals(intent.getAction())) {

Log.d(TAG,"[MyReceiver]接收到推送下來(lái)的通知");

intnotifactionId = bundle.getInt(JPushInterface.EXTRA_NOTIFICATION_ID);

Log.d(TAG,"[MyReceiver]接收到推送下來(lái)的通知的ID: "+ notifactionId);

}else if(JPushInterface.ACTION_NOTIFICATION_OPENED.equals(intent.getAction())) {

Log.d(TAG,"[MyReceiver]用戶(hù)點(diǎn)擊打開(kāi)了通知");

//? ? ? ? ? //打開(kāi)自定義的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]用戶(hù)收到到RICH PUSH CALLBACK: "+ bundle.getString(JPushInterface.EXTRA_EXTRA));

//在這里根據(jù)JPushInterface.EXTRA_EXTRA的內(nèi)容處理代碼,比如打開(kāi)新的Activity, 打開(kāi)一個(gè)網(wǎng)頁(yè)等..

}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數(shù)據(jù)

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);

//? ? }

}

}

第五步 注冊(cè)廣播? AndroidManifest.xml 并添加相應(yīng)權(quán)限 ,看官網(wǎng)demo添加

AndroidManifest.xml


第六步、混淆

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

-dontoptimize

-dontpreverify

-dontwarn cn.jpush.**

-keepclasscn.jpush.** { *;}

最后 ?去測(cè)試吧~~~~~~

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀(guān)點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

推薦閱讀更多精彩內(nèi)容