本地通知“日程"和“触发"方法执行多次 [英] Local notification "schedule" and "trigger" methods are executing multiple times

查看:147
本文介绍了本地通知“日程"和“触发"方法执行多次的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是离子的新手,正在使用(katzer/cordova-plugin-local-notifications),我遇到了问题,不知道发生了什么.

Hi I'm new on ionic and I'm using (katzer/cordova-plugin-local-notifications), I have a problem and I don't know what is happening.

当我单击链接时,我正在生成一个新通知.但是我不知道在通知中第二次单击时,两次执行计划"和触发"中的警报,而当我第三次单击通知中时,内部警报"schedule"和"trigger"的执行被执行了三次,等等.

When I click in a link, I'm generating a new notification. But I don't know for what when I click for the second time in the notification, the alert inside of "schedule" and "trigger" is executed two times, and when I click for the third time in the notification, the alert inside of "schedule" and "trigger" is executed three times, and so..

这是我的代码,非常简单:

This is my code, it's very simple:

$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.

I need that when I click in a notification only one alert print the related notification id.

有人可以帮我吗?

谢谢.

推荐答案

欢迎使用stackoverflow =)

Welcome to stackoverflow =)

使用您的代码,每次 单击添加通知,即添加事件处理程序以进行计划"和触发"事件.

With your code, every time you click to add a notification, you are adding event handlers for "schedule" and "trigger" events.

例如,您第一次点击 addNotification 上,cordova.plugins.notification.local.on(时间表")将注册和事件处理程序添加到" functionA ".单击第二次,另一个事件将被注册到" functionB ",依此类推.触发计划"事件时,将全部调用functionA,functionB,... .

For example, the first time you click on addNotification, cordova.plugins.notification.local.on("schedule") will register and event handler to "functionA". The second time you click it, another event will be registered to "functionB", and so on. functionA, functionB,...will all be called when the "schedule" event is fired.

解决方案,将事件处理代码移到函数之外.

Solution, move your event handling code outside of the function.

$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.

//////notification.id must be set globally, otherwise it is gonna show as undefined.

这篇关于本地通知“日程"和“触发"方法执行多次的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆