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

简介:

正文 

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

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

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

模式

          

                    推送交互模式

  ①② 所指的是学生群体

  ③  所指的是教师

 

  ③ :教师可以基于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

  下面是效果图:

      

      

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

服务端

  直接上代码吧

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()));
        }
    }
    
}

#初始化

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

* @param Content 推送内容

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

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

总结

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

相关文章
|
1月前
|
存储 小程序 API
【微信小程序】-- uni-app 项目-- 购物车 -- 首页 - 轮播图效果(五十二)
【微信小程序】-- uni-app 项目-- 购物车 -- 首页 - 轮播图效果(五十二)
【微信小程序】-- uni-app 项目-- 购物车 -- 首页 - 轮播图效果(五十二)
|
1月前
|
小程序 开发工具 git
【微信小程序】-- uni-app 项目--- 购物车 -- 配置 tabBar 效果(五十一)
【微信小程序】-- uni-app 项目--- 购物车 -- 配置 tabBar 效果(五十一)
|
1月前
|
JavaScript Android开发
【问题篇】打包Vue-cli3创建的vue项目成App的apk文件
【问题篇】打包Vue-cli3创建的vue项目成App的apk文件
25 0
|
16天前
|
监控 数据可视化 安全
智慧工地SaaS可视化平台源码,PC端+APP端,支持二开,项目使用,微服务+Java++vue+mysql
环境实时数据、动态监测报警,实时监控施工环境状态,有针对性地预防施工过程中的环境污染问题,打造文明生态施工,创造绿色的生态环境。
14 0
智慧工地SaaS可视化平台源码,PC端+APP端,支持二开,项目使用,微服务+Java++vue+mysql
|
1月前
|
移动开发 自然语言处理 小程序
miniprogram-to-uniapp使用指南(各种小程序项目转换为uni-app项目)
miniprogram-to-uniapp使用指南(各种小程序项目转换为uni-app项目)
35 1
|
1月前
|
小程序 安全 JavaScript
【微信小程序】-- uni-app 项目创建 & 目录结构讲解(四十九)
【微信小程序】-- uni-app 项目创建 & 目录结构讲解(四十九)
|
3月前
|
存储 JavaScript 前端开发
如何在uni-app项目中进行数据持久化
如何在uni-app项目中进行数据持久化
62 0
|
3月前
|
API
如何在uni-app项目中使用插件
如何在uni-app项目中使用插件
69 0
|
3月前
|
缓存 JavaScript 算法
在uni-app项目中,如何进行性能优化
在uni-app项目中,如何进行性能优化
53 0
|
3月前
|
JavaScript
如何在uni-app项目中使用路由
如何在uni-app项目中使用路由
27 0