android app 集成 信鸽推送
生活随笔
收集整理的這篇文章主要介紹了
android app 集成 信鸽推送
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
推送其實(shí)挺中意小米推送的,并經(jīng)用戶群占比還是比較大的,奈何拗不過php后端哥們的選型,就只好用信鴿推送了,期間接入過程中也是遇到不少問題,所以記錄下來,以后如果還是用信鴿推送的話,估計(jì)看看以前的博客,也能少踩點(diǎn)坑。
因?yàn)樽钚掳姹臼?.43,所以按照2.43的引入為準(zhǔn)
1,導(dǎo)入jar包和so文件:
文件夾為信鴿推送必須的so文件:
2,針對so文件,gradle文件進(jìn)行配置,生成第一張圖里面的native_libs2的jar文件:
3,AndroidManifest.xml文件的配置,這個(gè)基本按照官網(wǎng)demo文件里面寫的就行:
<!--(信鴿推送相關(guān))--><!-- 【必須】 (2.30及以上版新增)展示通知的activity --><activityandroid:name="com.tencent.android.tpush.XGPushActivity"android:theme="@android:style/Theme.Translucent"android:exported="false"><intent-filter><!-- 若使用AndroidStudio,請?jiān)O(shè)置android:name="android.intent.action"--><action android:name="android.intent.action"/></intent-filter></activity><!-- 【必須】 信鴿receiver廣播接收 --><receiverandroid:name="com.tencent.android.tpush.XGPushReceiver"android:process=":xg_service_v2"><intent-filter android:priority="0x7fffffff"><!-- 【必須】 信鴿SDK的內(nèi)部廣播 --><action android:name="com.tencent.android.tpush.action.SDK"/><action android:name="com.tencent.android.tpush.action.INTERNAL_PUSH_MESSAGE"/><!-- 【必須】 系統(tǒng)廣播:網(wǎng)絡(luò)切換 --><action android:name="android.net.conn.CONNECTIVITY_CHANGE"/><!-- 【可選】 系統(tǒng)廣播:開屏 --><action android:name="android.intent.action.USER_PRESENT"/><!-- 【可選】 一些常用的系統(tǒng)廣播,增強(qiáng)信鴿service的復(fù)活機(jī)會,請根據(jù)需要選擇。當(dāng)然,你也可以添加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相關(guān)的系統(tǒng)廣播,增強(qiáng)信鴿service的復(fù)活機(jī)會,請根據(jù)需要添加 --><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 --><serviceandroid:name="com.tencent.android.tpush.service.XGPushService"android:exported="true"android:persistent="true"android:process=":xg_service_v2"/><!-- 【必須】 通知service,其中android:name部分要改為當(dāng)前包名 --><serviceandroid:name="com.tencent.android.tpush.rpc.XGRemoteService"android:exported="true"><intent-filter><!-- 【必須】 請修改為當(dāng)前APP名包.PUSH_ACTION,如demo的包名為:com.qq.xgdemo --><action android:name="你的包名.PUSH_ACTION"/></intent-filter></service><!-- 【可選】APP實(shí)現(xiàn)的Receiver,用于接收消息透傳和操作結(jié)果的回調(diào),請根據(jù)需要添加 --><!-- YOUR_PACKAGE_PATH.CustomPushReceiver需要改為自己的Receiver: --><receiverandroid:name=".push.MessageReceiver"android:exported="false"><intent-filter><!-- 接收消息透傳 --><action android:name="com.tencent.android.tpush.action.PUSH_MESSAGE"/><!-- 監(jiān)聽注冊、反注冊、設(shè)置/刪除標(biāo)簽、通知被點(diǎn)擊等處理結(jié)果 --><action android:name="com.tencent.android.tpush.action.FEEDBACK"/></intent-filter></receiver> <!-- 【必須】 請修改為APP的AccessId,“21”開頭的10位數(shù)字,中間沒空格 --><meta-dataandroid:name="XG_V2_ACCESS_ID"android:value="你的AccessId"/><!-- 【必須】 請修改為APP的AccessKey,“A”開頭的12位字符串,中間沒空格 --><meta-dataandroid:name="XG_V2_ACCESS_KEY"android:value="你的AccessKey"/>還有就是相關(guān)權(quán)限:
<!-- 【必須】 信鴿SDK所需權(quán)限 --> <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所需權(quán)限 --> <uses-permission android:name="android.permission.BLUETOOTH" /> <uses-permission android:name="android.permission.BATTERY_STATS" />雖然很多,但是沒辦法,畢竟是中國特色,推送只能這樣,才能勉強(qiáng)能夠保證推送到達(dá)率。
4,相關(guān)代碼的集成:
如上其實(shí)都是可以從demo文件里面拷貝出來的,其中messageReceiver是最重要的類,因?yàn)橹饕耐扑托畔⑹菑倪@個(gè)類里面獲取的。
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();//}// 通知展示,主要控制推送來的信息在狀態(tài)欄的展示,當(dāng)然如果想自定義可以修改這個(gè)方法 @Overridepublic 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); }@Overridepublic void onSetTagResult(Context context, int errorCode, String tagName) {KLog.i("onSetTagResult");if (context == null) {return;}String text = "";if (errorCode == XGPushBaseReceiver.SUCCESS) {text = "\"" + tagName + "\"設(shè)置成功";}else {text = "\"" + tagName + "\"設(shè)置失敗,錯(cuò)誤碼:" + errorCode;}Log.d(LogTag, text);//show(context, text); }@Overridepublic 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 + "\"刪除失敗,錯(cuò)誤碼:" + errorCode;}Log.d(LogTag, text);//show(context, text); }// 通知點(diǎn)擊回調(diào) actionType=1為該消息被清除,actionType=0為該消息被點(diǎn)擊 @Overridepublic void onNotifactionClickedResult(Context context, XGPushClickedResult message) {KLog.i("onNotifactionClickedResult");if (context == null || message == null) {return;}String text = "";if (message.getActionType() ==XGPushClickedResult.NOTIFACTION_CLICKED_TYPE) {// 通知在通知欄被點(diǎn)擊啦。。。。。// APP自己處理點(diǎn)擊的相關(guān)動作// 這個(gè)動作可以在activity的onResume也能監(jiān)聽,請看第3點(diǎn)相關(guān)內(nèi)容text = "通知被打開 :" + message;KLog.i(text);KLog.i(message.getActivityName());// 獲取自定義key-value,我們的app主要的根據(jù)這塊的內(nèi)容進(jìn)行控制的,所以主要處理這塊的代碼String customContent = message.getCustomContent();if (!StringUtil.isEmpty(customContent)) {KLog.i("customContent", customContent);PushResult pushResult = Json.get().toObject(customContent,PushResult.class);switch (pushResult.getIndex()) { //......你的業(yè)務(wù)處理代碼default:break;}}}else if (message.getActionType() ==XGPushClickedResult.NOTIFACTION_DELETED_TYPE) {// 通知被清除啦。。。。// APP自己處理通知被清除后的相關(guān)動作text = "通知被清除 :" + message;KLog.i(text);}}@Overridepublic void onRegisterResult(Context context, int errorCode, XGPushRegisterResult message) {KLog.i("onRegisterResult");// TODO Auto-generated method stubif (context == null || message == null) {return;}String text = "";if (errorCode == XGPushBaseReceiver.SUCCESS) {text = message + "注冊成功";// 在這里拿tokenString token = message.getToken();}else {text = message + "注冊失敗,錯(cuò)誤碼:" + errorCode;}Log.d(LogTag, text);//show(context, text); }// 消息透傳 @Overridepublic void onTextMessage(Context context, XGPushTextMessage message) {KLog.json(Json.get().toJson(message));// TODO Auto-generated method stubString text = "收到消息:" + message.toString();// 獲取自定義key-valueString customContent = message.getCustomContent();if (customContent != null && customContent.length() != 0) {try {JSONObject obj = new JSONObject(customContent);// key1為前臺配置的keyif (!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); } }?
總結(jié)
以上是生活随笔為你收集整理的android app 集成 信鸽推送的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 有点小激动-Index Conditio
- 下一篇: 使用lvs搭建负载均衡集群