本地通知 "schedule" 和 "trigger" 方法正在执行多次

Local notification "schedule" and "trigger" methods are executing multiple times

嗨,我是 ionic 的新手,我正在使用 (katzer/cordova-plugin-local-notifications),我有一个问题,我不知道发生了什么。

当我点击 link 时,我正在生成一个新通知。但是不知道为什么第二次点击通知里面的"schedule"和"trigger"里面的alert执行了两次,第三次点击通知里面的alert ,"schedule"和"trigger"里面的alert被执行了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)
    });
}

我需要在单击通知时只有一个警报打印相关的通知 ID。

有人能帮帮我吗?

提前致谢。

欢迎使用 Whosebug =)

使用您的代码,每次单击添加通知,您就是在添加事件处理程序 用于 "schedule" 和 "trigger" 事件。

例如,您第一次在 addNotification单击 时,cordova.plugins.notification.local.on("schedule") 将 向“functionA”注册和事件处理程序。 第二次点击它,另一个事件将注册到“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);
});

//////notification.id 必须全局设置,否则会显示为未定义。