PhoneGap API介绍:Notification

移动开发
本文将介绍PhoneGap API——Notification:设备的视觉、声音和触觉提醒。PhoneGap API Notification设备的视觉、听觉和触觉通知。

方法

 notification.alert

  • 显示一个定制的警告或对话框。
navigator.notification.alert(message, alertCallback, [title], [buttonName]); 
  • 1.
  • message:对话框信息。(字符串类型)
  • alertCallback:当警告对话框被忽略时调用的回调函数。(函数类型)
  • title:对话框标题。(字符串类型)(可选项,默认值为“Alert”)
  • buttonName:按钮名称(字符串类型)(可选项,默认值为“OK”)

说明

  • 大多数PhoneGap使用本地对话框实现该功能。然而,一些平台只是简单的使用浏览器的alert函数,而这种方法通常是不能定制的。

支持的平台

  • Android
  • BlackBerry (OS 4.6)
  • BlackBerry WebWorks (OS 5.0或更高版本)
  • iPhone

简单的范例

// Android / BlackBerry WebWorks (OS 5.0 and higher) // iPhone 
function alertDismissed() { 
    // 进行处理 

 
navigator.notification.alert( 
    'You are the winner!',  // 显示信息 
       alertDismissed,         // 警告被忽视的回调函数 
       'Game Over',            // 标题 
       'Done'                  // 按钮名称 
); 
 
// BlackBerry (OS 4.6) // webOS 
navigator.notification.alert('You are the winner!'); 
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.

完整的范例

<!DOCTYPE html> 
<html> 
<head>     
<title>Notification Example</title> 
 
<script type="text/javascript" charset="utf-8" src="phonegap.js"></script> 
<script type="text/javascript" charset="utf-8"> 
 
    // 等待加载PhoneGap 
    document.addEventListener("deviceready", onDeviceReady, false); 
     
    // PhoneGap加载完毕 
    function onDeviceReady() { 
        // 空 
    } 
     
    // 警告对话框被忽视 
    function alertDismissed() { 
        // 进行处理 
    } 
     
    // 显示一个定制的警告框 
    function showAlert() { 
        navigator.notification.alert( 
            'You are the winner!',  // 显示信息 
            alertDismissed,         // 警告被忽视的回调函数 
            'Game Over',            // 标题 
            'Done'                  // 按钮名称 
        ); 
    } 
 
</script> 
</head> 
<body> 
    <p><a href="#" onclick="showConfirm(); return false;">Show Confirm</a></p> 
</body> 
</html> 
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.
  • 33.
  • 34.
  • 35.
  • 36.
  • 37.

notification.confirm

  • 显示一个可定制的确认对话框。
navigator.notification.confirm(message, confirmCallback, [title], [buttonLabels]); 
  • 1.
  • message:对话框信息。(字符串类型)
  • confirmCallback:按下按钮后触发的回调函数,返回按下按钮的索引(1、2或3)。(函数类型)
  • title:对话框标题。(字符串类型)(可选项,默认值为“Confirm”)
  • buttonLabels:逗号分隔的按钮标签字符串。(字符串类型)(可选项,默认值为“OK、Cancel”)

说明

  • notification.confirm函数显示一个定制性比浏览器的confirm函数更好的本地对话框。

支持的平台

  • Android
  • BlackBerry WebWorks (OS 5.0或更高版本)
  • iPhone

简单的范例

// 处理确认对话框返回的结果 
function onConfirm(button) { 
    alert('You selected button ' + button); 

 
// 显示一个定制的确认对话框 
function showConfirm() { 
       navigator.notification.confirm( 
           'You are the winner!',  // 显示信息 
           onConfirm,              // 按下按钮后触发的回调函数,返回按下按钮的索引 
           'Game Over',            // 标题 
           'Restart,Exit'          // 按钮标签 
       ); 
 } 
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.

完整的范例

<!DOCTYPE html> 
<html> 
<head>     
<title>Notification Example</title> 
 
<script type="text/javascript" charset="utf-8" src="phonegap.js"></script> 
<script type="text/javascript" charset="utf-8"> 
     
    // 等待加载PhoneGap 
    document.addEventListener("deviceready", onDeviceReady, false); 
     
    // PhoneGap加载完毕 
    function onDeviceReady() { 
        // 空 
    } 
     
    // 处理确认对话框返回的结果 
    function onConfirm(button) { 
        alert('You selected button ' + button); 
    } 
     
    // 显示一个定制的确认对话框 
    function showConfirm() { 
        navigator.notification.confirm( 
            'You are the winner!',  // 显示信息 
            onConfirm,              // 按下按钮后触发的回调函数,返回按下按钮的索引     
            'Game Over',            // 标题 
            'Restart,Exit'          // 按钮标签 
        ); 
    } 
 
</script> 
</head> 
<body> 
    <p><a href="#" onclick="showConfirm(); return false;">Show Confirm</a></p> 
</body> 
</html> 
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.
  • 33.
  • 34.
  • 35.
  • 36.
  • 37.


notification.beep

  • 设备将发出蜂鸣声。
navigator.notification.beep(times); 
  • 1.
  • times:蜂鸣声的重复次数。(数字类型)

支持的平台

  • Android
  • BlackBerry (OS 4.6)
  • BlackBerry WebWorks (OS 5.0或更高版本)
  • iPhone

简单的范例

// 蜂鸣2次! 
navigator.notification.beep(2); 
  • 1.
  • 2.

完整的范例

<!DOCTYPE html> 
<html> 
<head>     
<title>Notification Example</title> 
 
<script type="text/javascript" charset="utf-8" src="phonegap.js"></script> 
<script type="text/javascript" charset="utf-8"> 
 
// 等待加载PhoneGap 
document.addEventListener("deviceready", onDeviceReady, false); 
 
// PhoneGap加载完毕 
function onDeviceReady() { 
       // 空 

 
// 显示一个定制的警告框 
function showAlert() { 
       navigator.notification.alert( 
           'You are the winner!',  // 显示信息 
           'Game Over',            // 标题 
           'Done'                  // 按钮名称 
       ); 

 
// 蜂鸣三次 
function playBeep() { 
       navigator.notification.beep(3); 

 
// 震动两秒 
function vibrate() { 
       navigator.notification.vibrate(2000); 

 
</script> 
</head> 
<body> 
    <p><a href="#" onclick="showAlert(); return false;">Show Alert</a></p> 
    <p><a href="#" onclick="playBeep(); return false;">Play Beep</a></p> 
    <p><a href="#" onclick="vibrate(); return false;">Vibrate</a></p> 
</body> 
</html> 
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.
  • 33.
  • 34.
  • 35.
  • 36.
  • 37.
  • 38.
  • 39.
  • 40.
  • 41.
  • 42.
  • 43.

Android的特异情况

  • Androids会播放在“设置/音效及显示”面板中指定的默认“通知铃声”。

iPhone的特异情况

  • 忽略蜂鸣次数参数。
  • iPhone没有本地的蜂鸣API。
  • PhoneGap通过多媒体API播放音频文件来实现蜂鸣。
  • 用户必须提供一个包含所需的蜂鸣声的文件。
  • 此文件播放时长必须短于30秒,位于www/root,并且必须命名为beep.wav。

notification.vibrate

  • 使设备震动指定的时长。
navigator.notification.vibrate(milliseconds); 
  • 1.
  • time:以毫秒为单位的设备震动时长,1000毫秒为1秒。(数字类型)

支持的平台

  • Android
  • BlackBerry (OS 4.6)
  • BlackBerry WebWorks (OS 5.0或更高版本)
  • iPhone

简单的范例

// 震动2.5秒 
navigator.notification.vibrate(2500); 
  • 1.
  • 2.

完整的范例

<!DOCTYPE html> 
<html> 
<head>     
<title>Notification Example</title> 
 
<script type="text/javascript" charset="utf-8" src="phonegap.js"></script> 
<script type="text/javascript" charset="utf-8"
 
// 等待加载PhoneGap 
document.addEventListener("deviceready", onDeviceReady, false); 
 
// PhoneGap加载完毕 
function onDeviceReady() { 
       //空 

 
// 显示定制警告框 
function showAlert() { 
       navigator.notification.alert( 
           'You are the winner!',  // 显示信息 
           'Game Over',            // 标题 
           'Done'                  // 按钮名称 
       ); 

 
// 响三次 
function playBeep() { 
       navigator.notification.beep(3); 

 
// 震动两秒 
function vibrate() { 
       navigator.notification.vibrate(2000); 

 
</script> 
</head> 
<body> 
    <p><a href="#" onclick="showAlert(); return false;">Show Alert</a></p> 
    <p><a href="#" onclick="playBeep(); return false;">Play Beep</a></p> 
    <p><a href="#" onclick="vibrate(); return false;">Vibrate</a></p> 
</body> 
</html> 
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.
  • 33.
  • 34.
  • 35.
  • 36.
  • 37.
  • 38.
  • 39.
  • 40.
  • 41.
  • 42.
  • 43.

iPhone的特异情况

  • time:忽略时长参数,震动时长为预先设定值。

简单的范例

navigator.notification.vibrate(); 
navigator.notification.vibrate(2500);   // 2500被忽略掉 
  • 1.
  • 2.

 

责任编辑:佚名 来源: PhoneGap中国
相关推荐

2011-09-13 13:47:56

PhoneGap AP

2011-12-20 11:20:46

PhoneGap APCompass

2011-12-19 16:09:32

PhoneGap APCamera

2011-12-19 15:30:25

AccelerometPhoneGap AP

2011-12-20 13:32:52

PhoneGap APContacts

2011-12-20 15:34:55

PhoneGap APConnection

2011-12-20 16:09:57

PhoneGap APDevice

2011-12-20 17:15:52

PhoneGap APEvents

2011-12-22 09:27:36

PhoneGap APGeolocation

2011-12-22 09:54:40

PhoneGap APMedia

2011-12-22 10:45:32

PhoneGap APStorage

2011-12-21 21:56:45

PhoneGap APFile

2011-12-19 16:26:39

PhoneGap APCapture

2011-12-30 13:47:57

PhoneGap AP视频Contacts

2011-12-30 13:59:38

PhoneGap APDevice视频

2011-12-30 14:09:32

PhoneGap APCompass视频

2011-12-30 14:05:17

PhoneGap APConnection视频

2011-12-30 14:16:02

AccelerometPhoneGap AP视频

2011-12-30 14:13:05

PhoneGap APCamera视频

2011-07-05 17:29:53

PhoneGapevents
点赞
收藏

51CTO技术栈公众号