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

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

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

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

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

下面是一些屬性:插件

 

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

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

一個通知的例子:cordova

$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)
});
};

推遲提醒:ip

$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
});
};

重複提醒:get

$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);
});
相關文章
相關標籤/搜索