您好我是离子新手,我使用(katzer / cordova-plugin-local-notifications),我遇到了问题,我不知道发生了什么。
当我点击链接时,我会生成新通知。但我不知道在通知中第二次点击时," schedule"和"触发"执行两次,当我在通知中第三次点击时," schedule"和"触发"被执行三次,所以..
这是我的代码,非常简单:
$scope.addNotification = function (){
var idaudio = Math.round(Math.random() * 10000);
var date = Date.now();
cordova.plugins.notification.local.schedule({
id: idaudio,
title: 'Remember',
text: 'New Remember',
at: date
});
cordova.plugins.notification.local.on("schedule", function(notification){
alert("scheduled: " + notification.id);
});
cordova.plugins.notification.local.on('trigger', function (notification){
alert("trigger" + notification.id)
});
}
我需要在单击通知时只有一个警报打印相关的通知ID。
有人可以帮我吗?
提前致谢。
答案 0 :(得分:4)
欢迎使用stackoverflow =)
使用您的代码每次 点击添加通知,您添加事件处理程序以及"计划&#34 ;和"触发"事件。
例如,第一次点击 addNotification 时,cordova.plugins.notification.local.on(" schedule")将注册和事件处理程序到" functionA "。 第二次点击它,另一个事件将注册到" functionB ",依此类推。当"计划"时,functionA,functionB,...将全部称为事件被解雇了。
解决方案,将您的事件处理代码移到函数之外。
$scope.addNotification = function (){
var idaudio = Math.round(Math.random() * 10000);
var date = Date.now();
cordova.plugins.notification.local.schedule({
id: idaudio,
title: 'Remember',
text: 'New Remember',
at: date
});
}
//this should only be registered once
$scope.$on('$cordovaLocalNotification:schedule',function(notification) {
alert("scheduled: " + notification.id);
});
//this should only be registered once
$scope.$on('$cordovaLocalNotification:trigger',function(notification) {
alert("triggered: " + notification.id);
});
////// notification.id必须全局设置,否则它将显示为undefined。