push-消息推送

push-消息推送
Push模块管理推送消息功能,可以实现在线、离线的消息推送,通过plus.push可获取推送消息管理对象。

方法:
addEventListener: 添加推送消息事件监听器
clear: 清空所有推送消息
createMessage: 创建本地消息
getAllMessage: 获取所有推送消息
getClientInfo: 获取客户端推送标识信息
setAutoNotification: 设置程序是否将消息显示在系统消息中心
remove: 删除推送消息
对象:
ClientInfo: JSON对象,获取的客户端标识信息
PushMessage: JSON对象,推送消息对象
MessageOptions: JSON对象,获客户端创建本地消息的参数
回调方法:
PushReceiveCallback: 客户端接收到推动消息的回调函数
PushClickCallback: 用户点击推送消息事件的回调函数
权限:
permissions

{
// ...
"permissions":{
    // ...
    "Push": {
        "description": "消息推送"
    }
}
}
属性:

cover: 设定显示推送消息的模式

可取值true或false,true表示推送消息覆盖模式显示,即仅显示最后接收到的推送消息;false表示在系统消息中心显示多条消息。 默认值为ture。

平台支持
Android - 2.2+ (支持)
iOS - 4.3+ (不支持): 不支持覆盖消息,每条信息都在系统消息中心,忽略cover属性值。
ClientInfo
JSON对象,获取的客户端标识信息

属性:
token: _(String 类型 )_设备令牌(iOS设备唯一标识),用于APNS服务推送中标识设备的身份

clientid: _(String 类型 )_推送服务令牌(设备唯一标识),用于标识推送信息接收者身份

第三方推送服务器管理的设备唯一标识,在iOS平台此值通常与token不同;在其它平台此值通常与token值一致。 此值与设备及应用都相关,即不同的apk/ipa安装到同一台设备上的值都不相同。

appid: _(String 类型 )_第三方推送服务的应用标识

第三方推送服务器管理的应用标识,通常需要在第三方推送服务器平台进行注册获取。

appkey: _(String 类型 )_第三方推送服务器的应用键值

第三方推送服务器管理的应用键值,通常需要在第三方推送服务器平台进行注册获取。

PushMessage
JSON对象,推送消息对象

属性:
title: _(String 类型 )_推送消息显示的标题

content: _(String 类型 )_推送消息显示的内容

payload: _(JSON 类型 )_推送消息承载的数据

如果推送消息中传输的数据不符合JSON格式,则作为String类型数据保存。

aps: _(JSON 类型 )_Apple APNS推送协议数据

MessageOptions
JSON对象,获客户端创建本地消息的参数

属性:
appid: _(String 类型 )_要启动流应用的appid

默认值为当前流应用的appid。

title: _(String 类型 )_推送消息的标题

在系统消息中心显示的通知消息标题,默认值为程序的名称。

sound: _(String 类型 )_推送消息的提示音

显示消息时的播放的提示音,可取值: “system”-表示使用系统通知提示音; “none”-表示不使用提示音; 默认值为“system”。

cover: _(Boolean 类型 )_是否覆盖上一次提示的消息

可取值true或false,true为覆盖,false不覆盖,默认为permission中设置的cover值。

when: _(Date 类型 )_消息上显示的提示时间

默认为当前时间,如果延迟显示则使用延时后显示消息的时间。

delay: _(Number 类型 )_提示消息延迟显示的时间

当设备接收到推送消息后,可不立即显示,而是延迟一段时间显示,延迟时间单位为s,默认为0s,立即显示。

PushReceiveCallback
客户端接收到推动消息的回调函数

void onReceive( msg ) {
     // Recieved push message code.
 }
参数:
msg: ( String ) 必选 接收到的推送信息msg
返回值:
void : 无

PushClickCallback
用户点击推送消息事件的回调函数

void onClick( msg ) {
    // Clicked push message code. 
}
参数:
msg: ( String ) 必选 用户点击的推送信息msg
返回值:
void : 无

<!DOCTYPE HTML>
<html>
    <head>
        <meta charset="utf-8" />
        <meta name="viewport" content="initial-scale=1.0, maximum-scale=1.0, user-scalable=no" />
        <meta name="HandheldFriendly" content="true" />
        <meta name="MobileOptimized" content="320" />
        <title>Hello H5+</title>
        <script type="text/javascript">
            var pushServer = "http://demo.dcloud.net.cn/push/?";
            var message = null;
            // 监听plusready事件  
            document.addEventListener("plusready", function () {
                message = document.getElementById("message");
                // 监听点击消息事件
                plus.push.addEventListener("click", function (msg) {
                    // 判断是从本地创建还是离线推送的消息
                    switch (msg.payload) {
                        case "LocalMSG":
                            outSet("点击本地创建消息启动:");
                            break;
                        default:
                            outSet("点击离线推送消息启动:");
                            break;
                    }
                    // 提示点击的内容
                    plus.nativeUI.alert(msg.content);
                    // 处理其它数据
                    logoutPushMsg(msg);
                }, false);
                // 监听在线消息事件
                plus.push.addEventListener("receive", function (msg) {
                    if (msg.aps) { // Apple APNS message
                        outSet("接收到在线APNS消息:");
                    } else {
                        outSet("接收到在线透传消息:");
                    }
                    logoutPushMsg(msg);
                }, false);
            }, false);

            /**
             * 日志输入推送消息内容
             */
            function logoutPushMsg(msg) {
                console.log("title: " + msg.title);
                console.log("content: " + msg.content);
                if (msg.payload) {
                    if (typeof (msg.payload) == "string") {
                        console.log("payload(String): " + msg.payload);
                    } else {
                        console.log("payload(JSON): " + JSON.stringify(msg.payload));
                    }
                } else {
                    console.log("payload: undefined");
                }
                if (msg.aps) {
                    console.log("aps: " + JSON.stringify(msg.aps));
                }
            }

            /**
             * 获取本地推送标识信息
             */
            function getPushInfo() {
                var info = plus.push.getClientInfo();
                outSet("获取客户端推送标识信息:");
                console.log("id: " + info.id);
                console.log("token: " + info.token);
                console.log("clientid: " + info.clientid);
                console.log("appid: " + info.appid);
                console.log("appkey: " + info.appkey);
            }
            /**
             * 本地创建一条推动消息
             */
            function createLocalPushMsg() {
                var options = {
                    cover: false
                };
                var str = dateToStr(new Date());
                str += ": 欢迎使用HTML5+创建本地消息!";
                plus.push.createMessage(str, "LocalMSG", options);
                outSet("创建本地消息成功!");
                console.log("请到系统消息中心查看!");
                if (plus.os.name == "iOS") {
                    console.log('*如果无法创建消息,请到"设置"->"通知"中配置应用在通知中心显示!');
                }
            }
            /**
             * 读取所有推送消息
             */
            function listAllPush() {
                var msgs = null;
                switch (plus.os.name) {
                    case "Android":
                        msgs = plus.push.getAllMessage();
                        break;
                    default:
                        break;
                }
                if (!msgs) {
                    outSet("此平台不支持枚举推送消息列表!");
                    return;
                }
                outSet("枚举消息列表(" + msgs.length + "):");
                for (var i in msgs) {
                    var msg = msgs[i];
                    console.log(i + ": " + msg.title + " - " + msg.content);
                }
            }
            /**
             * 清空所有推动消息
             */
            function clearAllPush() {
                plus.push.clear();
                outSet("清空所有推送消息成功!");
            }
            /**
             * 请求‘简单通知’推送消息
             */
            function requireNotiMsg() {
                if (navigator.userAgent.indexOf('StreamApp') > 0) {
                    plus.nativeUI.toast('当前环境暂不支持发送推送消息');
                    return;
                }
                var inf = plus.push.getClientInfo();
                var url = pushServer + 'type=noti&appid=' + encodeURIComponent(plus.runtime.appid);
                inf.id && (url += '&id=' + inf.id);
                url += ('&cid=' + encodeURIComponent(inf.clientid));
                if (plus.os.name == 'iOS') {
                    url += ('&token=' + encodeURIComponent(inf.token));
                }
                url += ('&title=' + encodeURIComponent('Hello H5+'));
                url += ('&content=' + encodeURIComponent('欢迎回来体验HTML5 plus应用!'));
                url += ('&version=' + encodeURIComponent(plus.runtime.version));
                plus.runtime.openURL(url);
            }
            /**
             * 请求‘打开网页’推送消息
             */
            function requireLinkMsg() {
                if (navigator.userAgent.indexOf('StreamApp') > 0) {
                    plus.nativeUI.toast('当前环境暂不支持发送推送消息');
                    return;
                }
                var inf = plus.push.getClientInfo();
                var url = pushServer + "type=link&appid=" + encodeURIComponent(plus.runtime.appid);
                inf.id && (url += '&id=' + inf.id);
                url += ('&cid=' + encodeURIComponent(inf.clientid));
                if (plus.os.name == 'iOS') {
                    url += ('&token=' + encodeURIComponent(inf.token));
                }
                url += ('&title=' + encodeURIComponent('HBuilder飞一样的编码'));
                url += ('&content=' + encodeURIComponent('看HBuilder如何追求代码编写速度的极致!立即去瞧一瞧?'));
                url += ('&url=' + encodeURIComponent('http://www.dcloud.io/'));
                url += ('&version=' + encodeURIComponent(plus.runtime.version));
                plus.runtime.openURL(url);
            }
            /**
             * 请求‘下载链接’推送消息
             */
            function requireDownMsg() {
                if (navigator.userAgent.indexOf('StreamApp') > 0) {
                    plus.nativeUI.toast('当前环境暂不支持发送推送消息');
                    return;
                }
                if (plus.os.name != "Android") {
                    plus.nativeUI.alert("此平台不支持!");
                    return;
                }
                var inf = plus.push.getClientInfo();
                var url = pushServer + 'type=down&appid=' + encodeURIComponent(plus.runtime.appid);
                inf.id && (url += '&id=' + inf.id);
                url += ('&cid=' + encodeURIComponent(inf.clientid));
                url += ('&title=' + encodeURIComponent('Hello H5+'));
                url += ('&content=' + encodeURIComponent('新版本发布了!立即下载体验?'));
                url += ('&ptitle=' + encodeURIComponent('Hello H5+'));
                url += ('&pcontent=' + encodeURIComponent('1. 优化用户体验;\n2. 修复在Android2.3.x某些设备可能异常退出的问题.'));
                url += ('&dtitle=' + encodeURIComponent('下载Hello H5+'));
                url += ('&durl=' + encodeURIComponent('http://www.dcloud.io/helloh5/HelloH5.apk'));
                url += ('&version=' + encodeURIComponent(plus.runtime.version));
                plus.runtime.openURL(url);
            }
            /**
             * 请求‘透传数据’推送消息
             */
            function requireTranMsg() {
                if (navigator.userAgent.indexOf('StreamApp') > 0) {
                    plus.nativeUI.toast('当前环境暂不支持发送推送消息');
                    return;
                }
                var inf = plus.push.getClientInfo();
                var url = pushServer + 'type=tran&appid=' + encodeURIComponent(plus.runtime.appid);
                inf.id && (url += '&id=' + inf.id);
                url += ('&cid=' + encodeURIComponent(inf.clientid));
                if (plus.os.name == 'iOS') {
                    url += ('&token=' + encodeURIComponent(inf.token));
                }
                url += ('&title=' + encodeURIComponent('Hello H5+'));
                url += ('&content=' + encodeURIComponent('带透传数据推送通知,可通过plus.push API获取数据并进行业务逻辑处理!'));
                url += ('&payload=' + encodeURIComponent(
                    '{"title":"Hello H5+ Test","content":"test content","payload":"1234567890"}'));
                url += ('&version=' + encodeURIComponent(plus.runtime.version));
                plus.runtime.openURL(url);
            }

        </script>

    </head>
    <body>
        <header id="header">
            <div class="nvbt iback" οnclick="back()"></div>
            <div class="nvtt">Push</div>
            <div class="nvbt idoc" οnclick="openDoc('Push Document','/doc/push.html')"></div>
        </header>
        <div id="dcontent" class="dcontent">
            <div class="button" οnclick="requireNotiMsg()">发送"普通通知"消息</div>
            <div class="button" οnclick="requireLinkMsg()">发送"打开网页"消息</div>
            <div class="button" οnclick="requireDownMsg()">发送"下载链接"消息</div>
            <div class="button" οnclick="requireTranMsg()">发送"透传数据"消息</div>
            <br />
            <ul id="dlist" class="dlist">
                <li class="ditem" οnclick="getPushInfo()">获取客户端推送标识</li>
                <li class="ditem" οnclick="createLocalPushMsg()">创建本地消息</li>
                <li class="ditem" οnclick="listAllPush()">枚举推送消息</li>
                <li class="ditem" οnclick="clearAllPush()">清空推送消息</li>
                <!--<li class="ditem" οnclick="plus.push.setAutoNotification(false)">关闭自动显示消息</li>
                <li class="ditem" οnclick="plus.push.setAutoNotification(true)">开启自动显示消息</li>-->
            </ul>
        </div>
        <div id="output">
            Push模块管理推送消息功能,可以实现在线、离线的消息推送,通过plus.push可获取推送消息管理对象。
        </div>
    </body>
</html>

转载于:https://my.oschina.net/lslDn/blog/2999209

(注意使用前先将IP改为自己的IP或127.0.0.1) 本软件是使用套接字、ReceiveCallBack(IAsyncResult AR)函数为例的客服实例,修正了关闭客户端会导致异常的Bug;并且还是一个RichTextBox颜色使用的范例,不同的事件使用不同的颜色:如用户登录用红色、用户名用绿色、聊天内容用黑色^_^! 代码附赠全套注释,帮助初学者学习使用。 下面是核心代码 private void ReceiveCallBack(IAsyncResult AR) { try { DateTime dt = DateTime.Now; //如果服务器突然关闭后,客户端还坚持与之连接就会弹出异常; //检查是否套接字还连接上就可以避免这一问题。 if (!ClientSocket.Connected) { return; } //挂起AR,独占的使用AR来接收传过来的内容 int REnd = ClientSocket.EndReceive(AR); string StrOfREnd=Encoding.Unicode.GetString(MsgBuffer, 0, REnd); //截断的传输过来的字符串,"\n"前的是用户名 "\n"后的是聊天的内容 string UsersName = StrOfREnd.Substring(0, StrOfREnd.LastIndexOf("\n")); string Content = StrOfREnd.Substring(StrOfREnd.LastIndexOf("\n")+1); string Login=StrOfREnd.Substring(0,2); //MessageBox.Show("缓存中的内容:" + StrOfREnd + "\n" + "截断的用户名:" + UsersName + "\n" + "截断的内容:" + Content); if (Login != "登录") { //第一个字符不为“登陆” int oldlenth = tb_RecieveMsg.TextLength; this.tb_RecieveMsg.Select(oldlenth, 0); this.tb_RecieveMsg.SelectionColor = Color.Green; string str = Encoding.Unicode.GetString(MsgBuffer, 0, REnd); str = str.Substring(1, str.Length - 1); //用户使用绿色字体 this.tb_RecieveMsg.AppendText(" " + string.Format("{0:T}", dt) + " " + "用户:" + UsersName + "说:" + "\r\n"); this.tb_RecieveMsg.SelectionColor = Color.Black; this.tb_RecieveMsg.AppendText(" " + Content + "\r\n"); this.tb_RecieveMsg.AppendText("\r\n"); } else {
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值