cordova插件之Local Notification(本地通知)

原文鏈接:http://www.ncloud.hk/%E6%8A%80%E6%9C%AF%E5%88%86%E4%BA%AB/cordova-pluginslocal-notification/

本地通知的基本目的是使應用程序能夠通知用戶,它爲他們提供了一些信息例如,當應用程序沒有在前臺運行時,通知用戶一個消息或即將到來的約會。本地通知大多是基於時間的,如果觸發就會在通知中心顯示並呈現給用戶。

local notification插件可以通過schedule()一次安排一個或多個本地通知,這些通知可以立即觸發或者在某個時間點觸發。在安排多個通知時,注意要使用schedule([])數組來包含所有通知。

每個本地通知都需要一個數字id,沒有設置默認爲0,但是調用本地通知時會取代相同id中較早的那個。

下面是一些屬性:

 

首先執行下面命令安裝該插件:

cordova plugin add https://github.com/katzer/cordova-plugin-local-notifications.git

一個通知的例子:

$scope.scheduleSingleNotification = function () {
cordova.plugins.notification.local.schedule({
  id: 1,
  title: ‘應用提醒’,
  text: ‘應用有新消息,快來查看吧’,
  at: new Date(new Date().getTime() + 5 * 60 * 1000)
  });
};

多個通知的例子:

$scope.scheduleMutipleNotification = function () {
cordova.plugins.notification.local.schedule({
  id: 1,
  title: ‘應用提醒1’,
  text: ‘應用有新消息,快來查看吧’,
  at: new Date(new Date().getTime() + 5 * 60 * 1000)
  },{
id: 2,
  title: ‘應用提醒2’,
  text: ‘應用又有新消息,快來查看吧’,
  at: new Date(new Date().getTime() + 10 * 60 * 1000)
});
};

推遲提醒:

$scope.scheduleDelayedNotification = function () {
var now             =newDate().getTime(),
    _5_sec_from_now =newDate(now +5*1000);
 
cordova.plugins.notification.local.schedule({
    text:"Delayed Notification",
    at: _5_sec_from_now,
    sound:null
});
};

重複提醒:

$scope.scheduleRepeatedlyNotification = function () {
cordova.plugins.notification.local.schedule({
    text:"Repeatedly Notification",
    firstAt: monday,
    every:"day",
    icon:"file://img/logo.png"
}, callback);
}

有兩種常用的事件類型:

schedule事件將會在你調用schedule()時觸發每一個本地通知,trigger事件只有到達它的觸發事件纔會觸發該通知。

schedule Event:

cordova.plugins.notification.local.on("schedule", function(notification) {
    alert("scheduled: "+ notification.id);
});

trigger Event:

cordova.plugins.notification.local.on("trigger", function(notification) {
    alert("triggered: "+ notification.id);
});


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章