測試了好幾天,搜尋了好多文章與範例,都無法讓Phonegap plugin LocalNotification正常運作,
最後總算成功發送通知,但是原因卻不是程式寫錯,也不是Phonegap 3.5無法支援(曾經懷疑)。

我的作法如下:
1、安裝LocalNotification的plugin
c:\phonegap_project\> phonegap local plugin add de.appplant.cordova.plugin.local-notification

2、查詢安裝的plugin
c:\phonegap_project\> phonegap plugin list

[phonegap] the following plugins are installed
de.appplant.cordova.plugin.local-notification 0.7.6 "LocalNotification"
org.apache.cordova.device 0.2.13-dev "Device"

3、在專案www底下的config.xml加入plugin(我就是卡在這裡錯誤)

或指定版本(最好指定第2步驟查詢出來的版次)


因為我第一次下載的plugin是「de.appplant.cordova.plugin.local-notification 0.8.0dev」,
以上述的語法加在config.xml是沒有效果的,必須改為下列的語法才能正確使用

所以最好每次加掛plugin時查詢一下版本才不會像我一直找不到錯誤原因

4、撰寫程式測試

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<meta name="format-detection" content="telephone=no" />
<!-- WARNING: for iOS 7, remove the width=device-width and height=device-height attributes. See https://issues.apache.org/jira/browse/CB-4323 -->
<meta name="viewport" content="user-scalable=no, initial-scale=1, maximum-scale=1, minimum-scale=1, width=device-width, height=device-height, target-densitydpi=device-dpi" />
<meta name="msapplication-tap-highlight" content="no" />
<title>Hello World</title>
</head>
<body>
<script type="text/javascript" src="cordova.js"></script>

<script type="text/javascript">

function test() {
window.plugin.notification.local.add({ message: '中文測試' });
alert('發送成功');
}
</script>
<div class="app">
<h1>Apache Cordova</h1>
<section>
<p><a href="#" onclick="test(); return false;">Notification Test</a></p>
</section>
</div>
</body>
</html>



5、執行的結果
img_020

arrow
arrow
    全站熱搜

    keven 發表在 痞客邦 留言(0) 人氣()