百度消息推送SDK探究(并附上最简推送Demo)

上一篇《百度消息推送REST API探究》中了解了如何使用REST API推送消息,这一篇我们来看一下百度消息推送为我们提供的SDK.

帮助文档:http://developer.baidu.com/wiki/index.php?title=docs/cplat/push/api


我们先来看一下服务端SDK


下载解压后的目录结构


还是前面提到的那句话,先看sample

01package test;
02 
03import com.baidu.yun.channel.auth.ChannelKeyPair;
04import com.baidu.yun.channel.client.BaiduChannelClient;
05import com.baidu.yun.channel.exception.ChannelClientException;
06import com.baidu.yun.channel.exception.ChannelServerException;
07import com.baidu.yun.channel.model.PushBroadcastMessageRequest;
08import com.baidu.yun.channel.model.PushBroadcastMessageResponse;
09import com.baidu.yun.channel.model.PushUnicastMessageRequest;
10import com.baidu.yun.channel.model.PushUnicastMessageResponse;
11import com.baidu.yun.core.log.YunLogEvent;
12import com.baidu.yun.core.log.YunLogHandler;
13 
14public 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

01public 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}
PushReciver.java

01package com.example.baidulotterypush;
02 
03import android.content.BroadcastReceiver;
04import android.content.Context;
05import android.content.Intent;
06import android.util.Log;
07import android.widget.Toast;
08 
09import com.baidu.android.pushservice.PushConstants;
10 
11public 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


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值