百度消息推送SDK探究(并附上最简推送Demo)
2015-10-23 10:44
507 查看
上一篇《百度消息推送REST API探究》中了解了如何使用REST API推送消息,这一篇我们来看一下百度消息推送为我们提供的SDK.
帮助文档:http://developer.baidu.com/wiki/index.php?title=docs/cplat/push/api
我们先来看一下服务端SDK
下载解压后的目录结构
还是前面提到的那句话,先看sample
例子很简单,从名字上就能看出作用,这里就不啰嗦了。
下面我们来看看客户端的SDK
下载Android端SDK后解压如下
从用户手册中我们可以看到,Android Push服务以后台service方式运行,如果某个手机中集成了多个百度推送服务,为了减少内存和和功耗,只有一个后台service来共享Push通道。
接下来来看一下用户手册:
根据用户手册上的描述,我做了一个最简Demo,这个demo完全可以满足一般需求。
MainActivity.java
PushReciver.java
运行结果:
Demo下载地址:http://download.csdn.net/detail/lxq_xsyu/6954373
帮助文档:http://developer.baidu.com/wiki/index.php?title=docs/cplat/push/api
我们先来看一下服务端SDK
下载解压后的目录结构
还是前面提到的那句话,先看sample
01 | package test; |
02 |
03 | import com.baidu.yun.channel.auth.ChannelKeyPair; |
04 | import com.baidu.yun.channel.client.BaiduChannelClient; |
05 | import com.baidu.yun.channel.exception.ChannelClientException; |
06 | import com.baidu.yun.channel.exception.ChannelServerException; |
07 | import com.baidu.yun.channel.model.PushBroadcastMessageRequest; |
08 | import com.baidu.yun.channel.model.PushBroadcastMessageResponse; |
09 | import com.baidu.yun.channel.model.PushUnicastMessageRequest; |
10 | import com.baidu.yun.channel.model.PushUnicastMessageResponse; |
11 | import com.baidu.yun.core.log.YunLogEvent; |
12 | import com.baidu.yun.core.log.YunLogHandler; |
13 |
14 | public class AndroidPushNotificationSample { |
15 |
16 | public static void main(String[] args) { |
17 |
18 | /* |
19 | * @brief 推送单播通知(Android Push SDK拦截并解析) message_type = 1 (默认为0) |
20 | */ |
21 |
22 | // 1. 设置developer平台的ApiKey/SecretKey |
23 | String apiKey = "自己的apiKey"; |
24 | String secretKey = "自己的secretKey"; |
25 | ChannelKeyPair pair = new ChannelKeyPair(apiKey, secretKey); |
26 |
27 | // 2. 创建BaiduChannelClient对象实例 |
28 | BaiduChannelClient channelClient = new BaiduChannelClient(pair); |
29 |
30 | // 3. 若要了解交互细节,请注册YunLogHandler类 |
31 | channelClient.setChannelLogHandler(new YunLogHandler() { |
32 | @Override |
33 | public void onHandle(YunLogEvent event) { |
34 | System.out.println(event.getMessage()); |
35 | } |
36 | }); |
37 |
38 | try { |
39 |
40 | // 4. 创建请求类对象 |
41 | // 手机端的ChannelId, 手机端的UserId, 先用1111111111111代替,用户需替换为自己的 |
42 | PushBroadcastMessageRequest request = new PushBroadcastMessageRequest(); |
43 |
44 | //PushUnicastMessageRequest request = new PushUnicastMessageRequest(); |
45 |
46 |
47 | request.setDeviceType(3); // device_type => 1: web 2: pc 3:android |
48 | // 4:ios 5:wp |
49 | //request.setChannelId(3721876992860457831L); |
50 | //request.setUserId("1105477905904433716"); |
51 |
52 | request.setMessageType(1); |
53 | request.setMessage("{\"title\":\"大碗干拌\",\"description\":\"欢迎访问大碗干拌的CSDN博客\"}"); |
54 |
55 | // 5. 调用pushMessage接口 |
56 | /* PushUnicastMessageResponse response = channelClient |
57 | .pushUnicastMessage(request); |
58 |
59 | */ |
60 | PushBroadcastMessageResponse response = channelClient.pushBroadcastMessage(request); |
61 |
62 | // 6. 认证推送成功 |
63 | System.out.println( "push amount : " + response.getSuccessAmount()); |
64 |
65 | } catch (ChannelClientException e) { |
66 | // 处理客户端错误异常 |
67 | e.printStackTrace(); |
68 | } catch (ChannelServerException e) { |
69 | // 处理服务端错误异常 |
70 | System.out.println(String.format( |
71 | "request_id: %d, error_code: %d, error_message: %s" , |
72 | e.getRequestId(), e.getErrorCode(), e.getErrorMsg())); |
73 | } |
74 |
75 | } |
76 |
77 | } |
下面我们来看看客户端的SDK
下载Android端SDK后解压如下
从用户手册中我们可以看到,Android Push服务以后台service方式运行,如果某个手机中集成了多个百度推送服务,为了减少内存和和功耗,只有一个后台service来共享Push通道。
接下来来看一下用户手册:
根据用户手册上的描述,我做了一个最简Demo,这个demo完全可以满足一般需求。
MainActivity.java
01 | public class MainActivity extends Activity { |
02 |
03 | @Override |
04 | protected void onCreate(Bundle savedInstanceState) { |
05 | super .onCreate(savedInstanceState); |
06 | setContentView(R.layout.activity_main); |
07 | // 以apikey的方式登录,一般放在主Activity的onCreate中 |
08 | PushManager.startWork(getApplicationContext(), |
09 | PushConstants.LOGIN_TYPE_API_KEY, "自己的apikey" ); |
10 |
11 | } |
12 | } |
01 | package com.example.baidulotterypush; |
02 |
03 | import android.content.BroadcastReceiver; |
04 | import android.content.Context; |
05 | import android.content.Intent; |
06 | import android.util.Log; |
07 | import android.widget.Toast; |
08 |
09 | import com.baidu.android.pushservice.PushConstants; |
10 |
11 | public class MyPushMessageReceiver extends BroadcastReceiver{ |
12 |
13 | private static final String TAG = "大碗干拌" ; |
14 |
15 | @Override |
16 | public void onReceive(Context context, Intent intent) { |
17 | if (intent.getAction().equals(PushConstants.ACTION_MESSAGE)) { |
18 |
19 | } else if (intent.getAction().equals(PushConstants.ACTION_RECEIVE)) { |
20 |
21 | } else if (intent.getAction().equals( |
22 | PushConstants.ACTION_RECEIVER_NOTIFICATION_CLICK)) { |
23 | Log.i(TAG, "title = " + intent.getStringExtra(PushConstants.EXTRA_NOTIFICATION_TITLE)); |
24 | Log.i(TAG, "content = " + intent.getStringExtra(PushConstants.EXTRA_NOTIFICATION_CONTENT)); |
25 | } |
26 | } |
27 | } |
Demo下载地址:http://download.csdn.net/detail/lxq_xsyu/6954373
相关文章推荐
- Android ListView 使用
- 2016校招offer
- 十进制转二进制
- 淮安之rac行
- 数学之美
- 结构体
- 重建二叉树
- 能“分体”的小米到底能够走多远?
- Java ConcurrentModificationException 异常分析与解决方案(顶)
- Android开发中如何使用ProGuard
- Tomcat启动报Error listenerStart错误
- Makefile相关知识
- 网卡驱动程序
- Install nginx on centos
- Git基础
- jQuery实现的网页右下角tab样式在线客服效果代码
- 进程地址空间与虚拟存储空间的理解
- 8.2.1.17 DISTINCT Optimization
- 两条的链表交点
- android studio 可以自定义不提交哪些内容