jcenter 自動(dòng)集成步驟
好處:不需要在項(xiàng)目中添加jar和so,jcenter會(huì)自動(dòng)完成依賴;簡(jiǎn)化了之前繁瑣的配置步驟
1. Module的build.gradle
android {
......
defaultConfig {
applicationId "com.xxx.xxx" //JPush上注冊(cè)的包名.
......
ndk {
//選擇要添加的對(duì)應(yīng)cpu類型的.so庫(kù)。
abiFilters 'armeabi', 'armeabi-v7a', 'arm64-v8a'
// 還可以添加 'x86', 'x86_64', 'mips', 'mips64'
}
manifestPlaceholders = [
JPUSH_PKGNAME : applicationId,
JPUSH_APPKEY : "你的appkey", //JPush上注冊(cè)的包名對(duì)應(yīng)的appkey.
JPUSH_CHANNEL : "自定義渠道名稱", //用戶渠道統(tǒng)計(jì)的渠道名稱
]
......
}
......
}
dependencies {
......
compile 'cn.jiguang.sdk:jpush:3.0.5' // 此處以JPush 3.0.5 版本為例。
compile 'cn.jiguang.sdk:jcore:1.1.2' // 此處以JCore 1.1.2 版本為例。
......
}
注 : 如果在添加以上 abiFilter 配置之后android Studio出現(xiàn)以下提示:
NDK integration is deprecated in the current plugin. Consider trying the new experimental plugin.
則在 Project 根目錄的gradle.properties文件中添加:
android.useDeprecatedNdk=true
說明:若沒有res/drawable-xxxx/jpush_notification_icon這個(gè)資源默認(rèn)使用應(yīng)用圖標(biāo)作為通知icon,在5.0以上系統(tǒng)將應(yīng)用圖標(biāo)作為statusbar icon可能顯示不正常,用戶可定義沒有陰影和漸變色的icon替換這個(gè)文件,文件名不要變。
這里建議將應(yīng)用icon設(shè)置為通知顯示圖標(biāo),這里設(shè)置有點(diǎn)問題,我嘗試在初始化之后調(diào)用以下代碼設(shè)置失敗。
BasicPushNotificationBuilder builder = new BasicPushNotificationBuilder(this);
builder.statusBarDrawable = R.drawable.jpush_notification_icon;
2. 初始化sdk
JPushInterface.setDebugMode(true);//正式版的時(shí)候設(shè)置false,關(guān)閉調(diào)試
JPushInterface.init(this);
3. 混淆
-dontoptimize
-dontpreverify
-dontwarn cn.jpush.**
-keep class cn.jpush.** { *; }
-dontwarn cn.jiguang.**
-keep class cn.jiguang.** { *; }
4. 創(chuàng)建自定義廣播接收器
JPush SDK 收到推送,通過廣播的方式,轉(zhuǎn)發(fā)給開發(fā)者App,我們可以自定義廣播接受到推送消息。
/**
* 自定義接收器
*
* 如果不定義這個(gè) Receiver,則:
* 1) 默認(rèn)用戶會(huì)打開主界面
* 2) 接收不到自定義消息
*/
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));
processCustomMessage(context, bundle);
} 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] 用戶點(diǎn)擊打開了通知");
//
// //打開自定義的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));
//在這里根據(jù) JPushInterface.EXTRA_EXTRA 的內(nèi)容處理代碼,比如打開新的Activity, 打開一個(gè)網(wǎng)頁(yè)等..
} 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 數(shù)據(jù)
private static String printBundle(Bundle bundle) {
...//省略了
}
//send msg to MainActivity
private void processCustomMessage(Context context, Bundle bundle) {
...//省略了
}
}
別忘了在清單文件中注冊(cè)哦:
<!-- User defined. For test only 用戶自定義的廣播接收器-->
<receiver
android:name="com.example.jpushdemo.MyReceiver" //自己寫的Receiver
android:exported="false"
android:enabled="true">
<intent-filter>
<action android:name="cn.jpush.android.intent.REGISTRATION" /> <!--Required 用戶注冊(cè)SDK的intent-->
<action android:name="cn.jpush.android.intent.MESSAGE_RECEIVED" /> <!--Required 用戶接收SDK消息的intent-->
<action android:name="cn.jpush.android.intent.NOTIFICATION_RECEIVED" /> <!--Required 用戶接收SDK通知欄信息的intent-->
<action android:name="cn.jpush.android.intent.NOTIFICATION_OPENED" /> <!--Required 用戶打開自定義通知欄的intent-->
<action android:name="cn.jpush.android.intent.CONNECTION" /><!-- 接收網(wǎng)絡(luò)變化 連接/斷開 since 1.6.3 -->
<category android:name="${applicationId}" /> //包名
</intent-filter>
</receiver>
到這里一般就可以正常收到推送了。
5. 推送到單個(gè)用戶或一組用戶
-
單個(gè)推送
對(duì)應(yīng)別名 alias:為安裝了應(yīng)用程序的用戶,取個(gè)別名來標(biāo)識(shí)。以后給該用戶 Push 消息時(shí),就可以用此別名來指定。
每個(gè)用戶只能指定一個(gè)別名。
Method - setAlias:新的調(diào)用會(huì)覆蓋之前的設(shè)置 -
分組推送
對(duì)應(yīng)標(biāo)簽 tag:為安裝了應(yīng)用程序的用戶,打上標(biāo)簽。其目的主要是方便開發(fā)者根據(jù)標(biāo)簽,來批量下發(fā) Push 消息。
可為每個(gè)用戶打多個(gè)標(biāo)簽。
Method - setTags:新的調(diào)用會(huì)覆蓋之前的設(shè)置
</br> -
設(shè)置別名和tag
JPushInterface.setAliasAndTags(getApplicationContext(),alias, Tags);
6. 其他:
- 極光統(tǒng)計(jì):所有的 Activity 的 onResume / onPause 方法里調(diào)用
代碼示例
@Override
protected void onResume() {
super.onResume();
JPushInterface.onResume(this);
}
@Override
protected void onPause() {
super.onPause();
JPushInterface.onPause(this);
}
>極光推送的設(shè)備唯一性標(biāo)識(shí) RegistrationID:
</br>
RegistrationID 生成規(guī)則解析:
Android 上因?yàn)閲?guó)內(nèi)存在大量山寨設(shè)備的原因,正常的 IMEI, Mac Address, AndroidID 這些可以考慮用作唯一標(biāo)識(shí)的值,都是不可以用的,因?yàn)檫@些值在一批設(shè)備中可能都是同一個(gè)值。極光的基本思路是:
生成一個(gè) DeviceID 保存到 Settings, External Storage。依賴本地存儲(chǔ),應(yīng)用被卸載后重新安裝這些存儲(chǔ)里的 DeviceID 還在的話,就是同一個(gè)設(shè)備。這一條理論上解決 90% 的不變性問題。
DeviceID 之外增加補(bǔ)充規(guī)則:綜合根據(jù) IMEI, MAC Address, AndroidID, Serial_Number 這幾個(gè)值來判斷,是否可能是老設(shè)備。