Android百度云推送接入,附完整代码
生活随笔
收集整理的這篇文章主要介紹了
Android百度云推送接入,附完整代码
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
1、創建應用獲取api_key?百度云推送:http://push.baidu.com/
SDK下載:http://push.baidu.com/sdk/push_client_sdk_for_android
?
2、添加jar到libs文件下
?
compile files('libs/galaxy.jar') implementation files('libs/pushservice-6.1.1.21.jar') implementation files('libs/ufosdk-android-1.7.13.jar')3、權限配置 ,主要包名改為當前項目包名------>
com.baidu.push.example 改為當前項目包名 com.tianxin.mapclient.liteapp 改為當前項目包名?
<!-- Push service 運行需要的權限 --> <uses-permission android:name="android.permission.INTERNET" /> <uses-permission android:name="android.permission.READ_PHONE_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.WRITE_SETTINGS" /> <uses-permission android:name="android.permission.VIBRATE" /> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> <uses-permission android:name="android.permission.ACCESS_DOWNLOAD_MANAGER" /> <uses-permission android:name="android.permission.DOWNLOAD_WITHOUT_NOTIFICATION" /> <uses-permission android:name="android.permission.DISABLE_KEYGUARD" /> <!-- 富媒體需要聲明的權限 --> <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" /> <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" /> <uses-permission android:name="android.permission.EXPAND_STATUS_BAR" /><!-- 適配Android N系統必需的ContentProvider寫權限聲明,寫權限包含應用包名 --> <uses-permissionandroid:name="baidu.push.permission.WRITE_PUSHINFOPROVIDER.com.baidu.push.example" /> <permission android:name="baidu.push.permission.WRITE_PUSHINFOPROVIDER.com.baidu.push.example"android:protectionLevel="normal" />4、資源類配置,。主要包名改為當前項目包名。api_key改為當前項目申請的api_key
項目包名后面,注意要加上.bdpush
?
<!-- push富媒體,使用富媒體推送必須聲明 --> <activity android:name="com.baidu.android.pushservice.richmedia.MediaViewActivity"android:configChanges="orientation|keyboardHidden" android:label="MediaViewActivity" /> <activity android:name="com.baidu.android.pushservice.richmedia.MediaListActivity"android:configChanges="orientation|keyboardHidden" android:label="MediaListActivity"android:launchMode="singleTask" /> <!-- push富媒體結束 --><!-- push應用定義消息receiver聲明 --> <receiver android:name=".MyPushMessageReceiver"><intent-filter><!-- 接收push消息 --><action android:name="com.baidu.android.pushservice.action.MESSAGE" /><!-- 接收bind,unbind,fetch,delete等反饋消息 --><action android:name="com.baidu.android.pushservice.action.RECEIVE" /><action android:name="com.baidu.android.pushservice.action.notification.CLICK" /></intent-filter> </receiver><!-- push必須的receviver和service聲明 --> <receiver android:name="com.baidu.android.pushservice.PushServiceReceiver"android:process=":bdservice_v1"><intent-filter><action android:name="android.intent.action.BOOT_COMPLETED" /><action android:name="android.net.conn.CONNECTIVITY_CHANGE" /><action android:name="com.baidu.android.pushservice.action.notification.SHOW" /><action android:name="com.baidu.android.pushservice.action.media.CLICK" /><!-- 以下四項為可選的action聲明,可大大提高service存活率和消息到達速度 --><action android:name="android.intent.action.MEDIA_MOUNTED" /><action android:name="android.intent.action.USER_PRESENT" /><action android:name="android.intent.action.ACTION_POWER_CONNECTED" /><action android:name="android.intent.action.ACTION_POWER_DISCONNECTED" /></intent-filter> </receiver> <receiver android:name="com.baidu.android.pushservice.RegistrationReceiver"android:process=":bdservice_v1"><intent-filter><action android:name="com.baidu.android.pushservice.action.METHOD" /><action android:name="com.baidu.android.pushservice.action.BIND_SYNC" /></intent-filter><intent-filter><action android:name="android.intent.action.PACKAGE_REMOVED" /><data android:scheme="package" /></intent-filter> </receiver><service android:name="com.baidu.android.pushservice.PushService" android:exported="true"android:process=":bdservice_v1"><intent-filter><action android:name="com.baidu.android.pushservice.action.PUSH_SERVICE" /></intent-filter> </service> <service android:name="com.baidu.android.pushservice.CommandService"android:exported="true" /><!-- 適配Android N系統必需的ContentProvider聲明,寫權限包含應用包名--> <provider android:name="com.baidu.android.pushservice.PushInfoProvider"android:authorities="com.tianxin.mapclient.liteapp.bdpush" android:exported="true"android:protectionLevel="signature"android:writePermission="baidu.push.permission.WRITE_PUSHINFOPROVIDER.com.tianxin.mapclient.liteapp" /><!-- 在百度開發者中心查詢應用的API Key --> <meta-data android:name="api_key" android:value="tDrb727hnpdtLobd1BIpL5GQ" /><!-- UfoSDK --> <activity android:name="com.baidu.ufosdk.ui.FeedbackListActivity" /> <activity android:name="com.baidu.ufosdk.ui.FeedbackFacePageActivity" /> <activity android:name="com.baidu.ufosdk.ui.FeedbackImageViewFlipperActivity" /> <activity android:name="com.baidu.ufosdk.ui.FeedbackInputActivity" /> <activity android:name="com.baidu.ufosdk.ui.FeedbackHotActivity" />5、創建
MyPushMessageReceiver?
public class MyPushMessageReceiver extends PushMessageReceiver {/*** TAG to Log*/public static final String TAG = MyPushMessageReceiver.class.getSimpleName();/*** 調用PushManager.startWork后,sdk將對push* server發起綁定請求,這個過程是異步的。綁定請求的結果通過onBind返回。 如果您需要用單播推送,需要把這里獲取的channel* id和user id上傳到應用server中,再調用server接口用channel id和user id給單個手機或者用戶推送。** @param context BroadcastReceiver的執行Context* @param errorCode 綁定接口返回值,0 - 成功* @param appid 應用id。errorCode非0時為null* @param userId 應用user id。errorCode非0時為null* @param channelId 應用channel id。errorCode非0時為null* @param requestId 向服務端發起的請求id。在追查問題時有用;* @return none*/@Overridepublic void onBind(Context context, int errorCode, String appid,String userId, String channelId, String requestId) {String responseString = "onBind errorCode=" + errorCode + " appid="+ appid + " userId=" + userId + " channelId=" + channelId+ " requestId=" + requestId;Log.i("lgqbbbbb====dddd===", responseString);if (errorCode == 0) {// 綁定成功Log.d(TAG, "綁定成功");}// Demo更新界面展示代碼,應用請在這里加入自己的處理邏輯updateContent(context, responseString);}/*** 接收透傳消息的函數。** @param context 上下文* @param message 推送的消息* @param customContentString 自定義內容,為空或者json字符串*/@Overridepublic void onMessage(Context context, String message,String customContentString) {String messageString = "透傳消息 onMessage=\"" + message+ "\" customContentString=" + customContentString;Log.d(TAG, messageString);// 自定義內容獲取方式,mykey和myvalue對應透傳消息推送時自定義內容中設置的鍵和值if (!TextUtils.isEmpty(customContentString)) {JSONObject customJson = null;try {customJson = new JSONObject(customContentString);String myvalue = null;if (!customJson.isNull("mykey")) {myvalue = customJson.getString("mykey");}} catch (JSONException e) {e.printStackTrace();}}// Demo更新界面展示代碼,應用請在這里加入自己的處理邏輯updateContent(context, messageString);}/*** 接收通知到達的函數。** @param context 上下文* @param title 推送的通知的標題* @param description 推送的通知的描述* @param customContentString 自定義內容,為空或者json字符串*/@Overridepublic void onNotificationArrived(Context context, String title,String description, String customContentString) {String notifyString = "通知到達 onNotificationArrived title=\"" + title+ "\" description=\"" + description + "\" customContent="+ customContentString;Log.d(TAG, notifyString);// 自定義內容獲取方式,mykey和myvalue對應通知推送時自定義內容中設置的鍵和值if (!TextUtils.isEmpty(customContentString)) {JSONObject customJson = null;try {customJson = new JSONObject(customContentString);String myvalue = null;if (!customJson.isNull("mykey")) {myvalue = customJson.getString("mykey");}} catch (JSONException e) {// TODO Auto-generated catch blocke.printStackTrace();}}// Demo更新界面展示代碼,應用請在這里加入自己的處理邏輯// 你可以參考 onNotificationClicked中的提示從自定義內容獲取具體值updateContent(context, notifyString);}/*** 接收通知點擊的函數。** @param context 上下文* @param title 推送的通知的標題* @param description 推送的通知的描述* @param customContentString 自定義內容,為空或者json字符串*/@Overridepublic void onNotificationClicked(Context context, String title,String description, String customContentString) {String notifyString = "通知點擊 onNotificationClicked title=\"" + title + "\" description=\""+ description + "\" customContent=" + customContentString;Log.d(TAG, notifyString);// 自定義內容獲取方式,mykey和myvalue對應通知推送時自定義內容中設置的鍵和值if (!TextUtils.isEmpty(customContentString)) {JSONObject customJson = null;try {customJson = new JSONObject(customContentString);String myvalue = null;if (!customJson.isNull("mykey")) {myvalue = customJson.getString("mykey");}} catch (JSONException e) {// TODO Auto-generated catch blocke.printStackTrace();}}// Demo更新界面展示代碼,應用請在這里加入自己的處理邏輯updateContent(context, notifyString);}/*** setTags() 的回調函數。** @param context 上下文* @param errorCode 錯誤碼。0表示某些tag已經設置成功;非0表示所有tag的設置均失敗。* @param successTags 設置成功的tag* @param failTags 設置失敗的tag* @param requestId 分配給對云推送的請求的id*/@Overridepublic void onSetTags(Context context, int errorCode,List<String> successTags, List<String> failTags, String requestId) {String responseString = "onSetTags errorCode=" + errorCode+ " successTags=" + successTags + " failTags=" + failTags+ " requestId=" + requestId;Log.d(TAG, responseString);// Demo更新界面展示代碼,應用請在這里加入自己的處理邏輯updateContent(context, responseString);}/*** delTags() 的回調函數。** @param context 上下文* @param errorCode 錯誤碼。0表示某些tag已經刪除成功;非0表示所有tag均刪除失敗。* @param successTags 成功刪除的tag* @param failTags 刪除失敗的tag* @param requestId 分配給對云推送的請求的id*/@Overridepublic void onDelTags(Context context, int errorCode,List<String> successTags, List<String> failTags, String requestId) {String responseString = "onDelTags errorCode=" + errorCode+ " successTags=" + successTags + " failTags=" + failTags+ " requestId=" + requestId;Log.d(TAG, responseString);// Demo更新界面展示代碼,應用請在這里加入自己的處理邏輯updateContent(context, responseString);}/*** listTags() 的回調函數。** @param context 上下文* @param errorCode 錯誤碼。0表示列舉tag成功;非0表示失敗。* @param tags 當前應用設置的所有tag。* @param requestId 分配給對云推送的請求的id*/@Overridepublic void onListTags(Context context, int errorCode, List<String> tags,String requestId) {String responseString = "onListTags errorCode=" + errorCode + " tags="+ tags;Log.d(TAG, responseString);// Demo更新界面展示代碼,應用請在這里加入自己的處理邏輯updateContent(context, responseString);}/*** PushManager.stopWork() 的回調函數。** @param context 上下文* @param errorCode 錯誤碼。0表示從云推送解綁定成功;非0表示失敗。* @param requestId 分配給對云推送的請求的id*/@Overridepublic void onUnbind(Context context, int errorCode, String requestId) {String responseString = "onUnbind errorCode=" + errorCode+ " requestId = " + requestId;Log.d(TAG, responseString);if (errorCode == 0) {// 解綁定成功Log.d(TAG, "解綁成功");}// Demo更新界面展示代碼,應用請在這里加入自己的處理邏輯updateContent(context, responseString);}private void updateContent(Context context, String content) {Log.i("xiaodidaoda=====", "updateContent"+content);}}6、復制整個jniLibs文件到當前項目main下.。還有整個assets文件復制到main下
7、application下運行
?
UfoSDK.init(this); UfoSDK.openRobotAnswer();// 設置用戶的頭像 UfoSDK.setCurrentUserIcon(getMeIconBitmap()); // 在聊天界面中獲取聊天信息的時間間隔 UfoSDK.setChatThreadTime(10); // 設置當前用戶名 UfoSDK.setBaiduCuid(DeviceId.getCUID(this)); Log.i("lgq","iddddd=== "+DeviceId.getCUID(this)); // 我的反饋按鈕顏色 UfoSDK.setRootBackgroundColor(getResources().getColor(R.color.gray));8、MainActivity下運行
?
// 啟動百度push PushManager.startWork(getApplicationContext(), PushConstants.LOGIN_TYPE_API_KEY,Utils.getMetaValue(PushDemoActivity.this, "api_key"));9、創建推送,創建通知,發送通知,即可收到通知
在線交流bug:qq1085220040
?
總結
以上是生活随笔為你收集整理的Android百度云推送接入,附完整代码的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 浅谈协方差矩阵
- 下一篇: 编写TreeSet类的实现程序,其中相关