A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

本篇记录cordova的本地推送,早上折腾了一个多小时,安装本地推送插件后,打包一直报错,都忘记以前是怎么安装的了,通过和以前项目对比,发现是插件安装的顺序不同导致的,很多插件都有依赖的插件,本地推送插件依赖device插件,在安装本地推送插件之前,先移除device插件和其关联的插件。

一、安装本地推送插件

命令行输入cordova plugin add cordova-plugin-local-notification

D:\java\android\test>cordova plugin add cordova-plugin-local-notification
1
查看安装的插件cordova plugins ls

D:\java\android\test>cordova plugins ls
cordova-hot-code-push-plugin 1.5.3 "Hot Code Push Plugin"
cordova-plugin-badge 0.8.8 "Badge"
cordova-plugin-camera 4.0.3 "Camera"
cordova-plugin-device 2.0.2 "Device"
cordova-plugin-geolocation 4.0.1 "Geolocation"
cordova-plugin-local-notification 0.9.0-beta.2 "LocalNotification"
cordova-plugin-whitelist 1.3.3 "Whitelist"
1
2
3
4
5
6
7
8
安装插件后重新打包apk。

二、推送本地消息

2.1 推送单条消息

在www文件夹下打开index.html,添加:

<button id="single">单条消息</button>
1
打开index.js,添加:

var single = document.getElementById("single");
                single.onclick = function(){
                    cordova.plugins.notification.local.schedule({
                                  title: '单条信息',
                                  text: '推送单条消息内容',
                                  foreground: true
                          });
                }
1
2
3
4
5
6
7
8
命令行输入cordova-hcp build

D:\java\android\test>cordova-hcp build
Running build
Config { update: 'start',
  content_url: 'http://yktzs.top/cordova/',
  release: '2019.01.01-10.30.47' }
Build 2019.01.01-10.30.47 created in D:\java\android\test\www
1
2
3
4
5
6
将更新的文件发布至服务器,打开app。

点击按钮,任务栏出现推送的小图标,里面有推送的消息内容。

2.2 推送多条消息

index.html:

<button id="more">多条消息</button>
1
index.js:

var more = document.getElementById("more");
                more.onclick = function(){
                    cordova.plugins.notification.local.schedule([{
                                  id : 1,
                                  title: '第一条信息',
                                  text: '推送第一条消息内容',
                                  foreground: true,
                                  at : new Date()
                          },{
                                  id : 2,
                                  title: '第二条信息',
                                  text: '推送第二条消息内容',
                                  foreground: true,
                                  at :  new Date(new Date().getTime() + 1000)
                          }]);
                }
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
数组里面存放多条消息,可以定义推送时间。

2.3 参数说明

获取插件的属性默认值:

cordova.plugins.notification.local.getDefaults().foreground;//默认false
1
设置属性的默认值

cordova.plugins.notification.local.setDefaults({
                                foreground: true
                        });
cordova.plugins.notification.local.getDefaults().foreground;//显示true
1
2
3
4
id : 消息的唯一标示,如果没有值,默认为0,每次推送相同的消息id会覆盖之前的。

id : 1
1
title : 消息的标题。

title: '单条信息'
1
text : 消息内容,可为字符串和数组。

text: '推送单条消息内容'
1
数组使用person区分对象。

text: [
                { message: 'I say:you' },
                { person: 'you', message: 'you say' },
                { person: 'she',message: 'she say' },
            { person: 'he',message: 'he say ' }
        ]
1
2
3
4
5
6
at : 消息的推送时间。

at : new Date()
1
firstAt : 开始计算循环的时间,与every同时使用,重复发送消息。
every : 参数可为year、quarter、month、week、hour、day、minute。

//事件触发后一分钟之后开始循环触发(不是立即触发)
firstAt : new Date(),
every : 'minute'
1
2
3
icon : 消息内容图标。

icon : 'http://yktzs.top/work/images/first.jpg',
1
actions : 消息数组,可与事件监听同时使用,用户点击时触发相应的事件。

actions: [
                        { id: 'yes', title: 'Yes' },
                        { id: 'no',  title: 'No' }
                  ]
1
2
3
4
2.4 事件监听

event:add, trigger, click, clear, cancel, update, clearall and cancelall,也可为id名称。

cordova.plugins.notification.local.on(event, callback);
1
cordova.plugins.notification.local.on('yes', function (notification, eopts) { alert('点击了yes'); });
---------------------
转载,仅作分享,侵删
作者:这是个什么
原文:https://blog.csdn.net/qq_37926711/article/details/85533017


1 个回复

正序浏览
奈斯,加油
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马