码迷,mamicode.com
首页 > 移动开发 > 详细

项目ITP(三) 玩玩 服务端 到 app端

时间:2014-05-07 13:02:33      阅读:587      评论:0      收藏:0      [点我收藏+]

标签:android   style   blog   class   code   java   

前言

  系列文章:[传送门]

  泡泡脚,写写博客,规律生活,睡个好觉,待会看会书。

 

正文 

  上面讲了二维码生成,及 手机端扫一扫,大家有兴趣去看看。

  今天我们讲一下,百度云推送平台。

  每天想着问题,问题只会慢慢的清晰。想着想着,慢慢模式就出来了。

模式

          bubuko.com,布布扣

                    推送交互模式

  ①② 所指的是学生群体

  ③  所指的是教师

 

  ③ :教师可以基于http 给服务器指示,提示服务器进行操作(push...等);或是直接在web端进行操作

  ① :学生群体接受 push,或是直接查看某些通知,或是直接查看富文本,或是然后点击进行(③步骤)

  ② : 学生基于http 从服务器拉去资料

  ##ps:大家有好的点子 留言

 

百度云推送平台

   百度云推送服务的相关信息,主要包括两部分:

    1. 快速开发使用 Push 服务。

    2. 更多 Push 服务的开发及使用功能。

Android端

  1. 注册百度账号,并成为百度开发者;
  2. 创建应用,获取 API Key 及 Secret Key,请参考查看应用密钥;
  3. 下载应用示例;
  4. 把示例(Android 项目)导入 Eclipse 工程;
  5. 运行示例应用;
  6. 登录管理控制台发送通知;
  7. 手机端接收通知。

 

详细资料:http://developer.baidu.com/wiki/index.php?title=docs/cplat/push/guide

  下面是效果图:

      bubuko.com,布布扣

      bubuko.com,布布扣

  #API Key :     应用标识,服务端绑定和推送都要用到
  #Secret Key :  应用私钥,服务端推送时用到
  #APP ID:     这个id ,就是个id ,虽然也有唯一行,暂时没什么用
  #channel ID:  推送通道id,通常对应一台终端。同样的一个app ,装在手机A 和手机B上,channel id是不同的。
  #user id :      应用的用户id,同一个用户,可以在不同的终端上拥有同一个app 。user id 和 channel id 配合使用

服务端

  直接上代码吧

bubuko.com,布布扣
package sedion.jeffli.wmuitp.util.baidu;

import com.baidu.yun.channel.auth.ChannelKeyPair;
import com.baidu.yun.channel.client.BaiduChannelClient;
import com.baidu.yun.channel.exception.ChannelClientException;
import com.baidu.yun.channel.exception.ChannelServerException;
import com.baidu.yun.channel.model.PushBroadcastMessageRequest;
import com.baidu.yun.channel.model.PushBroadcastMessageResponse;
import com.baidu.yun.channel.model.PushUnicastMessageRequest;
import com.baidu.yun.channel.model.PushUnicastMessageResponse;
import com.baidu.yun.core.log.YunLogEvent;
import com.baidu.yun.core.log.YunLogHandler;

public class AndroidPushByBaiDuHelper
{
    private static String apiKey     = "xxx";
    private static String secretKey  = "xxx";
    
    /**
     * 初始化
     * @return
     */
    private static BaiduChannelClient initPushClient()
    {
        // 1. 设置developer平台的ApiKey/SecretKey
        ChannelKeyPair pair = new ChannelKeyPair(apiKey, secretKey);

        // 2. 创建BaiduChannelClient对象实例
        BaiduChannelClient channelClient = new BaiduChannelClient(pair);

        // 3. 若要了解交互细节,请注册YunLogHandler类
        channelClient.setChannelLogHandler(new YunLogHandler()
        {
            
            @Override
            public void onHandle(YunLogEvent event) 
            {
                System.out.println(event.getMessage());
            }
        });
        return channelClient;
    }
    
    
    /**
     * 推送广播消息(消息类型为透传,由开发方应用自己来解析消息内容) message_type = 0 (默认为0)
     * @param Content 推送内容
     */
    public static void pushBroadcastMessage(String Content)
    {
        
        BaiduChannelClient channelClient = initPushClient();

        try 
        {

            // 4. 创建请求类对象
            PushBroadcastMessageRequest request = new PushBroadcastMessageRequest();
            request.setDeviceType(3); 
            // device_type => 1: web 2: pc 3:android
            // 4:ios 5:wp

            request.setMessage(Content);

            // 5. 调用pushMessage接口
            PushBroadcastMessageResponse response = channelClient
                    .pushBroadcastMessage(request);

            // 6. 认证推送成功
            System.out.println("push amount : " + response.getSuccessAmount());

        } 
        catch (ChannelClientException e) 
        {
            // 处理客户端错误异常
            e.printStackTrace();
        } 
        catch (ChannelServerException e) 
        {
            // 处理服务端错误异常
            System.out.println(String.format(
                    "request_id: %d, error_code: %d, error_message: %s",
                    e.getRequestId(), e.getErrorCode(), e.getErrorMsg()));
        }

    }

    
    /**
     * 推送单播消息(消息类型为透传,由开发方应用自己来解析消息内容) message_type = 0 (默认为0)
     * @param ChannelId    手机端
     * @param content    推送内容
     * @param UserId    手机端
     */
    public static void pushMessageSample(String content, long ChannelId,String UserId)
    {

        BaiduChannelClient channelClient = initPushClient();

        try 
        {

            //创建请求类对象
            // 手机端的ChannelId, 手机端的UserId, 先用1111111111111代替,用户需替换为自己的
            PushUnicastMessageRequest request = new PushUnicastMessageRequest();
            request.setDeviceType(3); // device_type => 1: web 2: pc 3:android
                                      // 4:ios 5:wp
            request.setChannelId(ChannelId);
            request.setUserId(UserId);

            request.setMessage(content);

            // 5. 调用pushMessage接口
            PushUnicastMessageResponse response = channelClient
                    .pushUnicastMessage(request);

            // 6. 认证推送成功
            System.out.println("push amount : " + response.getSuccessAmount());

        } 
        catch (ChannelClientException e) 
        {
            // 处理客户端错误异常
            e.printStackTrace();
        }
        catch (ChannelServerException e)
        {
            // 处理服务端错误异常
            System.out.println(String.format(
                    "request_id: %d, error_code: %d, error_message: %s",
                    e.getRequestId(), e.getErrorCode(), e.getErrorMsg()));
        }
    }
    
}
bubuko.com,布布扣

#初始化

#推送广播消息(消息类型为透传,由开发方应用自己来解析消息内容) message_type = 0 (默认为0)

* @param Content 推送内容

# 推送单播消息(消息类型为透传,由开发方应用自己来解析消息内容) message_type = 0 (默认为0)

* @param ChannelId 手机端
* @param content 推送内容
* @param UserId 手机端

总结

  算个工具类吧 没别的。看书咯,加油大家

感谢及资源共享

    bubuko.com,布布扣

    bubuko.com,布布扣

    路上走来一步一个脚印,希望大家和我一起。

    感谢读者!很喜欢你们给我的支持。如果支持,点个赞。

    知识来源: 百度云平台

项目ITP(三) 玩玩 服务端 到 app端,布布扣,bubuko.com

项目ITP(三) 玩玩 服务端 到 app端

标签:android   style   blog   class   code   java   

原文地址:http://www.cnblogs.com/Alandre/p/3712830.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!