博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
push-消息推送
阅读量:7298 次
发布时间:2019-06-30

本文共 8704 字,大约阅读时间需要 29 分钟。

  hot3.png

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

你可能感兴趣的文章
Universal Windows Platform(UWP)应用的窗口特性
查看>>
最新2018年全球DevOps薪资报告:行业和团队选择指南
查看>>
Visual Studio 15.4发布,新增多平台支持
查看>>
Swift和Objective-C的运行时编程
查看>>
OneAPM挂牌新三板,续写ITOM新篇章
查看>>
KubeEdge:开源的Kubernetes原生边缘计算框架
查看>>
Stefan Tilkov:跳过单体应用,从微服务开始
查看>>
华为2018:年收入首破千亿美元大关,研发投入过千亿
查看>>
云计算怎么支撑起《流浪地球》的硬科幻实力?
查看>>
微服务基础架构的5个关键问题
查看>>
Git漏洞导致攻击者可在用户电脑上运行任意代码
查看>>
360首席安全官谭晓生宣布离职
查看>>
《与编码人员一起工作》作者访谈
查看>>
编写Linux内核模块——第一部分:前言
查看>>
python学习笔记 - StringIO以及BytesIO
查看>>
消息称微软计划收购GitHub,估值超50亿美元
查看>>
Netflix Play API:我们为什么构建了一个演进式架构?
查看>>
知乎推荐页Ranking构建历程和经验分享
查看>>
Web性能API——帮你分析Web前端性能
查看>>
Swift 4正式发布,新功能概览
查看>>