推送其實挺中意小米推送的,並經用戶群占比還是比較大的,奈何拗不過php后端哥們的選型,就只好用信鴿推送了,期間接入過程中也是遇到不少問題,所以記錄下來,以后如果還是用信鴿推送的話,估計看看以前的博客,也能少踩點坑。
因為最新版本是2.43,所以按照2.43的引入為准
1,導入jar包和so文件:
文件夾為信鴿推送必須的so文件:
2,針對so文件,gradle文件進行配置,生成第一張圖里面的native_libs2的jar文件:
3,AndroidManifest.xml文件的配置,這個基本按照官網demo文件里面寫的就行:
<!--(信鴿推送相關)--> <!-- 【必須】 (2.30及以上版新增)展示通知的activity --> <activity android:name="com.tencent.android.tpush.XGPushActivity" android:theme="@android:style/Theme.Translucent" android:exported="false"> <intent-filter> <!-- 若使用AndroidStudio,請設置android:name="android.intent.action"--> <action android:name="android.intent.action"/> </intent-filter> </activity> <!-- 【必須】 信鴿receiver廣播接收 --> <receiver android:name="com.tencent.android.tpush.XGPushReceiver" android:process=":xg_service_v2"> <intent-filter android:priority="0x7fffffff"> <!-- 【必須】 信鴿SDK的內部廣播 --> <action android:name="com.tencent.android.tpush.action.SDK"/> <action android:name="com.tencent.android.tpush.action.INTERNAL_PUSH_MESSAGE"/> <!-- 【必須】 系統廣播:網絡切換 --> <action android:name="android.net.conn.CONNECTIVITY_CHANGE"/> <!-- 【可選】 系統廣播:開屏 --> <action android:name="android.intent.action.USER_PRESENT"/> <!-- 【可選】 一些常用的系統廣播,增強信鴿service的復活機會,請根據需要選擇。當然,你也可以添加APP自定義的一些廣播讓啟動service --> <action android:name="android.bluetooth.adapter.action.STATE_CHANGED"/> <action android:name="android.intent.action.ACTION_POWER_CONNECTED"/> <action android:name="android.intent.action.ACTION_POWER_DISCONNECTED"/> </intent-filter> <!-- 【可選】 usb相關的系統廣播,增強信鴿service的復活機會,請根據需要添加 --> <intent-filter android:priority="0x7fffffff"> <action android:name="android.intent.action.MEDIA_UNMOUNTED"/> <action android:name="android.intent.action.MEDIA_REMOVED"/> <action android:name="android.intent.action.MEDIA_CHECKING"/> <action android:name="android.intent.action.MEDIA_EJECT"/> <data android:scheme="file"/> </intent-filter> </receiver> <!-- 【必須】 信鴿service --> <service android:name="com.tencent.android.tpush.service.XGPushService" android:exported="true" android:persistent="true" android:process=":xg_service_v2"/> <!-- 【必須】 通知service,其中android:name部分要改為當前包名 --> <service android:name="com.tencent.android.tpush.rpc.XGRemoteService" android:exported="true"> <intent-filter> <!-- 【必須】 請修改為當前APP名包.PUSH_ACTION,如demo的包名為:com.qq.xgdemo --> <action android:name="你的包名.PUSH_ACTION"/> </intent-filter> </service> <!-- 【可選】APP實現的Receiver,用於接收消息透傳和操作結果的回調,請根據需要添加 --> <!-- YOUR_PACKAGE_PATH.CustomPushReceiver需要改為自己的Receiver: --> <receiver android:name=".push.MessageReceiver" android:exported="false"> <intent-filter> <!-- 接收消息透傳 --> <action android:name="com.tencent.android.tpush.action.PUSH_MESSAGE"/> <!-- 監聽注冊、反注冊、設置/刪除標簽、通知被點擊等處理結果 --> <action android:name="com.tencent.android.tpush.action.FEEDBACK"/> </intent-filter> </receiver> <!-- 【必須】 請修改為APP的AccessId,“21”開頭的10位數字,中間沒空格 --> <meta-data android:name="XG_V2_ACCESS_ID" android:value="你的AccessId"/> <!-- 【必須】 請修改為APP的AccessKey,“A”開頭的12位字符串,中間沒空格 --> <meta-data android:name="XG_V2_ACCESS_KEY" android:value="你的AccessKey"/>
還有就是相關權限:
<!-- 【必須】 信鴿SDK所需權限 --> <uses-permission android:name="android.permission.INTERNET" /> <uses-permission android:name="android.permission.READ_PHONE_STATE" /> <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" /> <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /> <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" /> <uses-permission android:name="android.permission.RESTART_PACKAGES" /> <uses-permission android:name="android.permission.BROADCAST_STICKY" /> <uses-permission android:name="android.permission.WRITE_SETTINGS" /> <uses-permission android:name="android.permission.RECEIVE_USER_PRESENT" /> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> <uses-permission android:name="android.permission.WAKE_LOCK" /> <uses-permission android:name="android.permission.KILL_BACKGROUND_PROCESSES" /> <uses-permission android:name="android.permission.GET_TASKS" /> <uses-permission android:name="android.permission.READ_LOGS" /> <uses-permission android:name="android.permission.VIBRATE" /> <!-- 【可選】 信鴿SDK所需權限 --> <uses-permission android:name="android.permission.BLUETOOTH" /> <uses-permission android:name="android.permission.BATTERY_STATS" />
雖然很多,但是沒辦法,畢竟是中國特色,推送只能這樣,才能勉強能夠保證推送到達率。
4,相關代碼的集成:
如上其實都是可以從demo文件里面拷貝出來的,其中messageReceiver是最重要的類,因為主要的推送信息是從這個類里面獲取的。
public class MessageReceiver extends XGPushBaseReceiver { private Intent intent = new Intent( "com.qq.xgdemo.activity.UPDATE_LISTVIEW"); public static final String LogTag = "TPushReceiver"; //private void show(Context context, String text) { // Toast.makeText(context, text, Toast.LENGTH_SHORT).show(); //} // 通知展示,主要控制推送來的信息在狀態欄的展示,當然如果想自定義可以修改這個方法 @Override public void onNotifactionShowedResult(Context context, XGPushShowedResult notifiShowedRlt) { if (context == null || notifiShowedRlt == null) { return; } XGNotification notific = new XGNotification(); notific.setMsg_id(notifiShowedRlt.getMsgId()); notific.setTitle(notifiShowedRlt.getTitle()); notific.setContent(notifiShowedRlt.getContent()); // notificationActionType==1為Activity,2為url,3為intent notific.setNotificationActionType( notifiShowedRlt.getNotificationActionType()); // Activity,url,intent都可以通過getActivity()獲得 notific.setActivity(notifiShowedRlt.getActivity()); notific.setUpdate_time( new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format( Calendar.getInstance().getTime())); NotificationService.getInstance(context).save(notific); context.sendBroadcast(intent); String customContent = notifiShowedRlt.getCustomContent(); KLog.json(customContent); } //反注冊,注意在你用的activity里面的ondestory里面反注冊 @Override public void onUnregisterResult(Context context, int errorCode) { KLog.i("onUnregisterResult"); if (context == null) { return; } String text = ""; if (errorCode == XGPushBaseReceiver.SUCCESS) { text = "反注冊成功"; } else { text = "反注冊失敗" + errorCode; } KLog.i(LogTag, text); //show(context, text); } @Override public void onSetTagResult(Context context, int errorCode, String tagName) { KLog.i("onSetTagResult"); if (context == null) { return; } String text = ""; if (errorCode == XGPushBaseReceiver.SUCCESS) { text = "\"" + tagName + "\"設置成功"; } else { text = "\"" + tagName + "\"設置失敗,錯誤碼:" + errorCode; } Log.d(LogTag, text); //show(context, text); } @Override public void onDeleteTagResult(Context context, int errorCode, String tagName) { KLog.i("onDeleteTagResult"); if (context == null) { return; } String text = ""; if (errorCode == XGPushBaseReceiver.SUCCESS) { text = "\"" + tagName + "\"刪除成功"; } else { text = "\"" + tagName + "\"刪除失敗,錯誤碼:" + errorCode; } Log.d(LogTag, text); //show(context, text); } // 通知點擊回調 actionType=1為該消息被清除,actionType=0為該消息被點擊 @Override public void onNotifactionClickedResult(Context context, XGPushClickedResult message) { KLog.i("onNotifactionClickedResult"); if (context == null || message == null) { return; } String text = ""; if (message.getActionType() == XGPushClickedResult.NOTIFACTION_CLICKED_TYPE) { // 通知在通知欄被點擊啦。。。。。 // APP自己處理點擊的相關動作 // 這個動作可以在activity的onResume也能監聽,請看第3點相關內容 text = "通知被打開 :" + message; KLog.i(text); KLog.i(message.getActivityName()); // 獲取自定義key-value,我們的app主要的根據這塊的內容進行控制的,所以主要處理這塊的代碼 String customContent = message.getCustomContent(); if (!StringUtil.isEmpty(customContent)) { KLog.i("customContent", customContent); PushResult pushResult = Json.get() .toObject(customContent, PushResult.class); switch (pushResult.getIndex()) { //......你的業務處理代碼default: break; } } } else if (message.getActionType() == XGPushClickedResult.NOTIFACTION_DELETED_TYPE) { // 通知被清除啦。。。。 // APP自己處理通知被清除后的相關動作 text = "通知被清除 :" + message; KLog.i(text); } } @Override public void onRegisterResult(Context context, int errorCode, XGPushRegisterResult message) { KLog.i("onRegisterResult"); // TODO Auto-generated method stub if (context == null || message == null) { return; } String text = ""; if (errorCode == XGPushBaseReceiver.SUCCESS) { text = message + "注冊成功"; // 在這里拿token String token = message.getToken(); } else { text = message + "注冊失敗,錯誤碼:" + errorCode; } Log.d(LogTag, text); //show(context, text); } // 消息透傳 @Override public void onTextMessage(Context context, XGPushTextMessage message) { KLog.json(Json.get().toJson(message)); // TODO Auto-generated method stub String text = "收到消息:" + message.toString(); // 獲取自定義key-value String customContent = message.getCustomContent(); if (customContent != null && customContent.length() != 0) { try { JSONObject obj = new JSONObject(customContent); // key1為前台配置的key if (!obj.isNull("key")) { String value = obj.getString("key"); Log.d(LogTag, "get custom value:" + value); } // ... } catch (JSONException e) { e.printStackTrace(); } } // APP自主處理消息的過程... Log.d(LogTag, text); //show(context, text); } }