0

您好我是新的離子,我正在使用(katzer/cordova-plugin-local-notifications),我有一個問題,我不知道發生了什麼事。本地通知「日程安排」和「觸發器」方法執行多次

當我點擊一個鏈接時,我正在生成一個新的通知。但是我不知道當我在通知中第二次點擊時,「schedule」和「trigger」內部的警報執行了兩次,而當我在通知中第三次點擊時,內部警報「時間表」和「觸發」執行三次,等..

這是我的代碼,這是非常簡單的:

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

任何人都可以幫助我嗎?

在此先感謝。 每次點擊添加通知

回答

4

歡迎計算器=)

與您的代碼,你添加事件處理的「時間表」和「觸發」事件。

例如,你第一次點擊addNotification,cordova.plugins.notification.local.on( 「安排」)將寄存器和事件處理程序 「泛函」。 第二次您點擊它,另一個事件將被註冊爲「函數B」,依此類推。 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必須全局設置,否則它會顯示爲undefined。

+0

是的,你是對的。 :) 謝謝你的幫助!! – JPinios

+0

真的很棒的答案。它幫助我解決了這個問題並節省了大量的時間。 onSchedule和onTrigger函數似乎需要進行一些修改。已經根據我的工作建議。非常感謝您的寶貴答案。 :) –

+0

你可以請解決同樣的問題離子2?我面臨的問題與上述問題中所述的相同, – KCP