JPi*_*ios 1 notifications localnotification phonegap-plugins cordova cordova-plugins
嗨,我是 ionic 新手,我正在使用 (katzer/cordova-plugin-local-notifications),我遇到了问题,我不知道发生了什么。
当我点击一个链接时,我会生成一个新的通知。但是我不知道为什么当我在通知中第二次点击时,“计划”和“触发”里面的警报被执行了两次,当我在通知中第三次点击时,里面的警报"schedule" 和 "trigger" 执行 3 次,依此类推。
这是我的代码,非常简单:
$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)
});
}
Run Code Online (Sandbox Code Playgroud)
我需要当我单击通知时,只有一个警报会打印相关的通知 ID。
有人可以帮我吗?
提前致谢。
欢迎使用 stackoverflow =)
与您的代码,每次你点击添加一个通知,你添加事件处理的“时间表”和“触发”事件。
例如,你第一次点击的addNotification,cordova.plugins.notification.local.on(“安排”)将注册和事件处理程序“泛函”。在第二次你点击它,另一个事件将被登记为“ functionB ”,等等。functionA, functionB,...都将在“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
});
}
//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);
});
Run Code Online (Sandbox Code Playgroud)
//////notification.id 必须全局设置,否则会显示为未定义。