请选择 进入手机版 | 继续访问电脑版
帖子
帖子
用户
博客
课程
显示全部楼层
216
帖子
5
勋章
5750
Y币

aliPush模块(移动推送)demo示例

[复制链接]
发表于 2017-8-18 21:47:10
本帖最后由 52yaoer 于 2021-8-31 18:44 编辑

aliPush模块封装了阿里移动推送sdk,阿里移动推送(Alibaba Cloud Mobile Push)是基于大数据的移动智能推送服务,帮助App快速集成移动推送的功能,在实现高效、精确、实时的移动推送的同时,极大地降低了开发成本。让开发者最有效地与用户保持连接,从而提高用户活跃度、提高应用的留存率。
开发者在使用aliPush模块时,需要开发者自行到阿里云注册账号,并开通移动推送功能。本模块与其它阿里相关或者集成了小米推送相关模块会存在冲突的可能,请大家使用时注意。


关于安卓8.0以上的推送显示问题请看:https://community.apicloud.com/bbs/thread-114028-1-1.html



AliPush.html

  1. <!DOCTYPE html>
  2. <html>
  3.         <head>
  4.                 <title>Module Develop</title>
  5.                 <meta charset="utf-8">
  6.                 <meta name="viewport" content="maximum-scale=1.0,minimum-scale=1.0,user-scalable=0,width=device-width,initial-scale=1.0"/>
  7.                 <style type="text/css">
  8.                         html, body {
  9.                                 height: 100%
  10.                         }
  11.                         body {
  12.                                 background-color: #fff;
  13.                                 margin: 0;
  14.                         }
  15.                         #wrap {
  16.                                 height: 100%;
  17.                                 position: relative;
  18.                         }
  19.                         #header {
  20.                                 padding-top: 20px;
  21.                                 background-color: #5082c2;
  22.                                 height: 44px;
  23.                                 position: relative;
  24.                         }
  25.                         #header h1 {
  26.                                 font-size: 20px;
  27.                                 height: 44px;
  28.                                 line-height: 44px;
  29.                                 margin: 0em;
  30.                                 color: #fff;
  31.                                 margin-left: 100px;
  32.                                 margin-right: 100px;
  33.                                 text-align: center;
  34.                         }
  35.                         #main {
  36.                                 display: -webkit-box;
  37.                                 -webkit-box-orient: vertical;
  38.                                 -webkit-box-pack: center;
  39.                         }
  40.                         a.button {
  41.                                 display: -webkit-box;
  42.                                 -webkit-box-orient: vertical;
  43.                                 -webkit-box-pack: center;
  44.                                 -webkit-box-align: center;
  45.                                 height: 32px;
  46.                                 margin: 8px;
  47.                                 background-color: rgba(240,240,240,1.0);
  48.                                 border-color: rgba(220,220,220,1.0);
  49.                                 border-width: 2px;
  50.                                 border-style: solid;
  51.                         }
  52.                         a.active {
  53.                                 background-color: rgba(240,240,240,0.7);
  54.                         }
  55.                 </style>
  56.         </head>
  57.         <body>
  58.                 <div id="wrap">
  59.                         <div id="main">
  60.                                 <a class="button" tapmode="active" onclick="registerPush()" >阿里推送注册</a>
  61.                                 <a class="button" tapmode="active" onclick="setAutoRegister()" >设置是否在app启动是自动注册推送</a>
  62.                 <a class="button" tapmode="active" onclick="getAutoRegister()" >获取是否在app启动是自动注册推送</a>
  63.                                 <a class="button" tapmode="active" onclick="isRegister()" >是否注册成功</a>
  64.                                 <a class="button" tapmode="active" onclick="getDeviceId()" >获取设备ID</a>
  65.                                 <a class="button" tapmode="active" onclick="onAppStart()" >启动app统计(仅安卓有效)</a>
  66.                                 <a class="button" tapmode="active" onclick="addTag()" >添加标签</a>
  67.                                 <a class="button" tapmode="active" onclick="removeTag()" >删除标签</a>
  68.                                 <a class="button" tapmode="active" onclick="listTags()" >查询标签</a>
  69.                                 <a class="button" tapmode="active" onclick="addAlias()" >添加别名</a>
  70.                                 <a class="button" tapmode="active" onclick="removeAlias()" >删除别名</a>
  71.                                 <a class="button" tapmode="active" onclick="listAliases()" >查询别名</a>
  72.                                 <a class="button" tapmode="active" onclick="bindAccount()" >绑定账号</a>
  73.                                 <a class="button" tapmode="active" onclick="unbindAccount()" >解除绑定</a>
  74.                                 <a class="button" tapmode="active" onclick="setDoNotDisturb()" >设置免打扰时间段(仅安卓有效)</a>
  75.                                 <a class="button" tapmode="active" onclick="closeDoNotDisturbMode()" >取消免打扰(仅安卓有效)</a>
  76.                                 <a class="button" tapmode="active" onclick="clearNotifications()" >删除所有通知(仅安卓有效)</a>
  77.                                 <a class="button" tapmode="active" onclick="bindPhoneNumber()" >绑定电话号码(仅安卓有效)</a>
  78.                                 <a class="button" tapmode="active" onclick="unbindPhoneNumber()" >解绑电话号码(仅安卓有效)</a>
  79.                                 <a class="button" tapmode="active" onclick="setNotificationSoundFilePath()" >设置推送通知声音(仅安卓有效)</a>
  80.                         </div>
  81.                 </div>
  82.         </body>
  83.         <script type="text/javascript">
  84.                 var aliPush = null;
  85.                 apiready = function() {
  86.                         aliPush = api.require('aliPush');
  87.                         if(!aliPush){
  88.                                 alert("请添加模块后编译");
  89.                                 return;
  90.                         }
  91.                         //isRegister();
  92.                         aliPush.addEventListener({
  93.                                 name : 'onMessage'
  94.                         }, function(ret, err) {
  95.                                 alert("onMessage>>>addEventListener>>>>" + JSON.stringify(ret));
  96.                         });
  97.                         aliPush.addEventListener({
  98.                                 name : 'onNotification'
  99.                         }, function(ret, err) {
  100.                                 alert("onNotification>>>addEventListener>>>>" + JSON.stringify(ret));
  101.                         });
  102.                         aliPush.addEventListener({
  103.                                 name : 'onNotificationOpened'
  104.                         }, function(ret, err) {
  105.                                 alert("onNotificationOpened>>>addEventListener>>>>" + JSON.stringify(ret));
  106.                         });
  107.                         aliPush.addEventListener({
  108.                                 name : 'onNotificationRemoved'
  109.                         }, function(ret, err) {
  110.                                 alert("onNotificationRemoved>>>addEventListener>>>>" + JSON.stringify(ret));
  111.                         });

  112.                         api.addEventListener({
  113.                             name:'noticeclicked'
  114.                         },function(ret,err){
  115.                             alert("noticeclicked>>>"+JSON.stringify(ret.value));
  116.                         });

  117.                         // var fs = api.require('fs');
  118.                         // var ret = fs.copyToSync({
  119.                         //     oldPath: 'widget://res/alicloud_notification_sound.mp3',
  120.                         //     newPath: 'fs://'
  121.                         // });
  122.                         // if (ret.status) {
  123.                         //     alert('拷贝成功!');
  124.                         //                 setNotificationSoundFilePath();
  125.                         // } else {
  126.                         //     alert('拷贝失败!');
  127.                         // }

  128.                         //setAdvCusNotif();
  129.                 }

  130.                 function registerPush() {
  131.                         aliPush.registerPush(function(ret, err) {
  132.                                 alert(JSON.stringify(ret)+"  "+JSON.stringify(err));
  133.                                 //getDeviceId();
  134.                         });
  135.                 }

  136.                 function setAutoRegister(){
  137.             var ret = aliPush.setAutoRegister({isAutoRegister : true});
  138.             alert(JSON.stringify(ret));
  139.         }

  140.         function getAutoRegister(){
  141.             var ret = aliPush.getAutoRegister();
  142.             alert(JSON.stringify(ret));
  143.         }

  144.                 function getDeviceId() {
  145.                         aliPush.getDeviceId(function(ret, err) {
  146.                                 alert(JSON.stringify(ret)+"  "+JSON.stringify(err));
  147.                                 console.log(JSON.stringify(ret));
  148.                         });
  149.                 }

  150.                 function isRegister() {
  151.                         aliPush.isRegister(function(ret, err) {
  152.                                 alert(JSON.stringify(ret)+"  "+JSON.stringify(err));
  153.                                 //getDeviceId();
  154.                         });
  155.                 }

  156.                 function onAppStart() {
  157.                         aliPush.onAppStart(function(ret, err) {
  158.                                 alert(JSON.stringify(ret)+"  "+JSON.stringify(err));
  159.                         });
  160.                 }

  161.                 function addTag() {
  162.                         aliPush.addTag({
  163.                                 target : 3,
  164.                                 tag : "apicloud1 apicloud2",
  165.                                 alias : 'aliasname'
  166.                         }, function(ret, err) {
  167.                                 alert(JSON.stringify(ret)+"  "+JSON.stringify(err));
  168.                         });
  169.                 }

  170.                 function removeTag() {
  171.                         aliPush.removeTag({
  172.                                 target : 3,
  173.                                 tag : "apicloud1 apicloud2",
  174.                                 alias : 'aliasname'
  175.                         }, function(ret, err) {
  176.                                 alert(JSON.stringify(ret)+"  "+JSON.stringify(err));
  177.                         });
  178.                 }

  179.                 function listTags() {
  180.                         aliPush.listTags(function(ret, err) {
  181.                                 alert(JSON.stringify(ret)+"  "+JSON.stringify(err));
  182.                         });
  183.                 }


  184.                 function addAlias() {
  185.                         aliPush.addAlias({
  186.                                 alias : 'test01'
  187.                         }, function(ret, err) {
  188.                                 alert(JSON.stringify(ret)+"  "+JSON.stringify(err));
  189.                         });
  190.                 }

  191.                 function removeAlias() {
  192.                         aliPush.removeAlias({
  193.                                 alias : 'test01'
  194.                         }, function(ret, err) {
  195.                                 alert(JSON.stringify(ret)+"  "+JSON.stringify(err));
  196.                         });
  197.                 }

  198.                 function listAliases() {
  199.                         aliPush.listAliases(function(ret, err) {
  200.                                 alert(JSON.stringify(ret)+"  "+JSON.stringify(err));
  201.                         });
  202.                 }

  203.                 function bindAccount() {
  204.                         aliPush.bindAccount({
  205.                                 account : "apicloudaccount"
  206.                         }, function(ret, err) {
  207.                                 alert(JSON.stringify(ret)+"  "+JSON.stringify(err));
  208.                         });
  209.                 }

  210.                 function unbindAccount() {
  211.                         aliPush.unbindAccount(function(ret, err) {
  212.                                 alert(JSON.stringify(ret)+"  "+JSON.stringify(err));
  213.                         });
  214.                 }

  215.                 function setDoNotDisturb() {
  216.                         aliPush.setDoNotDisturb({
  217.                                 startHour : 23,
  218.                                 startMinute : 0,
  219.                                 endHour : 8,
  220.                                 endMinute : 0
  221.                         }, function(ret, err) {
  222.                                 alert(JSON.stringify(ret)+"  "+JSON.stringify(err));
  223.                         });
  224.                 }

  225.                 function closeDoNotDisturbMode() {
  226.                         aliPush.closeDoNotDisturbMode(function(ret, err) {
  227.                                 alert(JSON.stringify(ret)+"  "+JSON.stringify(err));
  228.                         });
  229.                 }

  230.                 function clearNotifications() {
  231.                         aliPush.clearNotifications(function(ret, err) {
  232.                                 alert(JSON.stringify(ret)+"  "+JSON.stringify(err));
  233.                         });
  234.                 }

  235.                 function bindPhoneNumber() {
  236.                         aliPush.bindPhoneNumber({
  237.                                 phoneNumber : "1300000000"
  238.                         }, function(ret, err) {
  239.                                 alert(JSON.stringify(ret)+"  "+JSON.stringify(err));
  240.                         });
  241.                 }

  242.                 function unbindPhoneNumber() {
  243.                         aliPush.unbindPhoneNumber(function(ret, err) {
  244.                                 alert(JSON.stringify(ret)+"  "+JSON.stringify(err));
  245.                         });
  246.                 }

  247.                 function setNotificationSoundFilePath() {
  248.                         aliPush.setNotificationSoundFilePath({
  249.                                 filePath : "fs://alicloud_notification_sound.mp3"
  250.                         }, function(ret, err) {
  251.                                 alert(JSON.stringify(ret)+"  "+JSON.stringify(err));
  252.                         });
  253.                 }




  254.                 function turnOnPushChannel() {
  255.                         aliPush.turnOnPushChannel(function(ret, err) {
  256.                                 alert(JSON.stringify(ret)+"  "+JSON.stringify(err));
  257.                         });
  258.                 }

  259.                 function turnOffPushChannel() {
  260.                         aliPush.turnOffPushChannel(function(ret, err) {
  261.                                 alert(JSON.stringify(ret)+"  "+JSON.stringify(err));
  262.                         });
  263.                 }

  264.                 function checkPushChannelStatus() {
  265.                         aliPush.checkPushChannelStatus(function(ret, err) {
  266.                                 alert(JSON.stringify(ret)+"  "+JSON.stringify(err));
  267.                         });
  268.                 }


  269.                 function setAdvCusNotif() {
  270.                         aliPush.setAdvCusNotif({
  271.                                 remindType:2, //0:静默 1:震动 2:声音 3:声音+震动
  272.                                 notificationId : 2,//默认2 范围:1-100
  273.                                 serverOptionFirst : false,
  274.                                 appInForeground : true //设置当推送到达时如果应用处于前台不创建通知: true : 创建 | false: 不创建
  275.                         },function(ret, err) {
  276.                                 setNotificationSoundFilePath();
  277.                                 alert(JSON.stringify(ret));
  278.                         });
  279.                 }
  280.         </script>
  281. </html>
复制代码

53
帖子
0
勋章
306
Y币
ios 角标如何去掉啊  
3
帖子
0
勋章
98
Y币
版主你好
https://community.apicloud.com/bbs/thread-112217-1-1.html
AppKey_Android和AppSecret_Android是阿里的appKey和AppSecret吗?
如果小米的我没有申请,XmAppID与XmAppKey是不是就不用填写了,如果我不使用ios,是不是AppKey_iOS与AppSecret_iOS也不需要填写?
华为的开发者比较好申请,我已经申请了华为推送,那华为的appKey与appSecret是不是不需要单独填写?
216
帖子
5
勋章
5750
Y币
Victor.X 发表于 2018-9-10 10:57
版主你好
https://community.apicloud.com/bbs/thread-112217-1-1.html
AppKey_Android和AppSecret_Android ...

华为的需要在阿里后台填写,,不需要在config.xml里面配置
216
帖子
5
勋章
5750
Y币
不用ios就不填下ios的参数即可。。
216
帖子
5
勋章
5750
Y币
小米的没申请也可以不填写。。
15
帖子
0
勋章
142
Y币
isRegister方法返回错误的参数是怎么回事?
aliPush>>>isRegister>>>>{"status":false},{"errorMessage":"错误的参数","errorCode":-1}
config.xml是:
<feature name="aliPushNoUtdId">
    <param name="AppKey_iOS" value="2560......"/>
    <param name="AppSecret_iOS" value="d9382697d61945824cf....."/>
    <param name="AppKey_Android" value="255..."/>
    <param name="AppSecret_Android" value="c234ca5655e79...."/>
</feature>
216
帖子
5
勋章
5750
Y币
<feature name="aliPush">   应该是这个
53
帖子
1
勋章
65
Y币
阿里的推送服务已经集成了vivo和魅族,什么时候代码能够更新到也支持这两种手机
17
帖子
0
勋章
177
Y币
channelName,channelId,channelDes 怎么获取阿
12345678910... 17下一页
您需要登录后才可以回帖 登录

本版积分规则