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

 

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/415656.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

新建用户、付权限命令

删除已有用户及与用户相关的 drop user usernam cascade; 以DBA登录 sqlplus sys/gislxghas sysdba 创建表空间 create tablespace workdatafile E:\ghoa\ldghTableSpace\work.dbfsize 400m autoextend on next 20m online; 创建新用户 create user work identified…

Spring自动装配----注解装配----Spring自带的@Autowired注解

Spring自动装配----注解装配----Spring自带的Autowired注解 父类 package cn.ychx;public interface Person {public void sayHello();} 学生子类package cn.ychx;public class Student implements Person {Overridepublic void sayHello() {System.out.println("Hello! M…

mac 查看指定端口情况 并杀死该进程

本来自己的8081端口很好用的&#xff0c;但是突然访问不了了。估计测试的时候有其他进程占用了端口。下面我需要查杀他。 1、查看端口情况 lsof -i tcp:8081COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME node 26519 caowei 31u IPv4 0x57e8…

工作117:eachat图

let option1 {tooltip : {trigger: axis},legend: {data:[邮件营销,联盟广告,视频广告,直接访问,搜索引擎]},calculable : true,xAxis : [{type : category,boundaryGap : false,axisTick: {show: false},data : [周一,周二,周三,周四,周五,周六,周日]}],yAxis : [{type : val…

CreateProcess error = 2,系统找不到指定的文件

针对有安装NDK用户Android/Sdk/ndk-bundle/toolchains/mips64el-linux-android-4.9/prebuilt/linux-x86_64/bin/mips64el-linux-android-strip 找不到, 导致编译报错 也就是说在Android/Sdk/ndk-bundle/toolchains/mips64el-linux-android-4.9/prebuilt/linux-x86_64/ 路径下找…

有关性能测试结果的几点分析原则

性能 测试结果的分析原则&#xff1a; 具体问题具体分析&#xff08;这是由于不同的应用系统&#xff0c;不同的测试目的&#xff0c;不同的性能关注点&#xff09; 查找瓶颈时按以下顺序&#xff0c;由易到难。 服务器硬件瓶颈-〉网络瓶颈&#xff08;对局域网&#xff0c;可以…

UVALive4256 Salesmen

题意:一个n个点的联通图(n<100)的无向联通图&#xff0c;还有一个长度为L序列(L<200)&#xff0c;问最少改变序列中几个数使得序列相邻两个数是相同或者在图中相邻 题解:dp[i][j]代表第i个数变为j的最小次数,O(n*L*n) #include <bits/stdc.h> #define maxn 210 #de…

小程序 mpvue input 文本控制

我的需求是输入金额&#xff0c;当金额过大时&#xff0c;可以将input中的金额调小&#xff0c;即不允许客户随便输入。 直接上代码了 <input input"moneyControl" placeholder"请填写提现金额" />moneyControl(e) {const value e.mp.detail.valuec…

Error inflating class xxx.view

方法1&#xff1a; 缺少实例化 如&#xff1a; Error inflating class com.facebook.drawee.view.SimpleDraweeView 原因是没有执行&#xff0c;下面这句话 Fresco.initialize(this); setContentView(R.layout.activity_main); 方法2&#xff1a; 只需AndroidManifest加入…

mpvue 小程序下拉刷新 三个点那种

在mpvue中&#xff0c;onPullDownRefresh直接能用 1、首先要保证配置 {path: pages/user,config: {navigationBarTitleText: 个人中心,navigationBarBackgroundColor: theme[primary-color],enablePullDownRefresh: true}}2、和methods同层级写 onPullDownRefresh() {wx.showNa…

word 2010中正文页码如何从第1页开始?

今天碰到了一个问题&#xff0c;word 文档在编写完后&#xff0c;需要调整格式&#xff0c;想调整为从正文开始&#xff0c;页码从第1页开始设置。 首先想到的第一个思路是分节&#xff0c;从正文开始设置新节&#xff0c;但是页码不对&#xff0c;怎么从第1页开始呢&#xff1…

CSS3的transition和transform

CSS3中的transition和transform是制作HTML5动画一定要使用到的两个属性。 注&#xff1a;这篇文章不考虑兼容性&#xff0c;只讨论webkit核心的浏览器。所以本文的所有例子请用chrome&#xff0c;safari或360极速浏览器看。 transition transition对标签的变化过程进行设置。比…

微信小程序 客服功能 客服消息

很多应用场景&#xff0c;需要小程序的客服功能&#xff0c;只需要按照官方配置设置好&#xff0c;就OK。 官网文档参考地址&#xff1a; https://developers.weixin.qq.com/miniprogram/dev/component/button.html https://developers.weixin.qq.com/miniprogram/dev/framewor…

textview点击展开全部或收起,内容过长显示省略号,设置行间距,字间距,跑马灯显示

跑马灯显示 android:ellipsize"marquee" android:singleLine"true" paomad.setSelected(true); 使用RelativeLayout可以使用图标点击旋转&#xff0c;展开textview或收缩textview <RelativeLayoutandroid:layout_width"match_parent"android…

在windows 2003系统安装oracle11G出现的问题

最近因为项目的需要&#xff0c;需要在虚拟机中搭建oracle11g&#xff0c;结果在安装中出现了一些平时没遇到过的问题&#xff0c;暂且记录下来。 问题报告&#xff1a; 正在检查网络配置要求... 检查完成。此次检查的总体结果为: 失败 <<<< 问题: 安装检测到系统…

前端学习(2615):数据映射map

第一步 引入 第二步 计算属性

Java的运算符-取整,取绝对值,取余数,四舍五入

double d (double) weiguidesc.length() / 18;//文本长度除以每行字符长度int okcprogress (int) (Math.floor(d))1;//除数取整&#xff0c;也就是行数 float ab 15f/4f; int ac (int)ab; Log.i("lgq","......ac"ac"........ab"ab); 结果…

hapi和typescript构建项目(正在更新中)

1、初始化项目 初始化 yarn init生成配置文件tsconfig.json tsc --init 注意&#xff1a;将outDir设置为"outDir": "dist" 全部编译配置文档地址&#xff1a;https://www.w3cschool.cn/typescript/typescript-compiler-options.html 安装工具concurren…