Skip to content

Instantly share code, notes, and snippets.

@sungkwangsong
Created June 20, 2014 06:57
Show Gist options
  • Save sungkwangsong/2d5191e47235ab5e9c86 to your computer and use it in GitHub Desktop.
Save sungkwangsong/2d5191e47235ab5e9c86 to your computer and use it in GitHub Desktop.
PhoneGap(Cordova) 하이브리드 앱에서 PushPlugin으로 푸시 서비스 구현하기 예제
<!DOCTYPE html>
<!--
Copyright (c) 2012-2014 Adobe Systems Incorporated. All rights reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-->
<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" />
<link rel="stylesheet" type="text/css" href="css/index.css" />
<title>Hello World</title>
</head>
<body>
<div class="app">
<h1>PhoneGap</h1>
<div id="deviceready" class="blink">
<p class="event listening">Connecting to Device</p>
<p class="event received">Device is Ready</p>
</div>
</div>
<script type="text/javascript" src="phonegap.js"></script>
<!--<script type="text/javascript" src="js/index.js"></script>-->
<script type="text/javascript">
// app.initialize();
/**
* tokenHandler
*
* @param result
*
* 디바이스 토큰핸들러 콜백함수.
* 푸시 서비스를 활성화 하였을 때, window.plugins.pushNotification.register 메소드가 실행되면서 디바이스 토큰을 가져와서 출력한다.
* 만약에 푸시 서버로 디바이스 토큰을 보내야할 경우 이 함수 안에서 서버로 디바이스 토큰을 전송하면 된다.
*/
function tokenHandler(result){
console.log('deviceToken:' + result);
}
/**
* errorHandler
*
* @param err
*
* 에러 핸들러 콜백 함수.
*/
function errorHandler(err){
console.log('error:' + err);
}
/**
* successHandler
*
* @param result
*
* 디바이스로 푸시 메세지를 받았을 때 뱃지처리 이후 호출하는 콜백함수
*/
function successHandler(result){
console.log('result:'+result);
}
/**
* onNotificationAPN
*
* @param event
*
* 디바이스로 푸시 메세지를 받을 때 호출되는 콜백함수 window.plugins.pushNotification.register 옵션 설정에서 ecb의 이름에 매칭된다.
*/
function onNotificationAPN (event){
// 푸시 메세지에 alert 값이 있을 경우
if (event.alert){
navigator.notification.alert(event.alert);
}
// 푸시 메세지에 sound 값이 있을 경우
if (event.sound){
var snd = new Media(event.sound);
snd.play();
}
// 푸시 메세지에 bage 값이 있을 경우
if (event.badge){
window.plugins.pushNotification.setApplicationIconBadgeNumber(successHandler, errorHandler, event.badge);
}
}
// 디바이스가 ready가 될때 실행될 수 있도록 이벤트 리스너에 등록한다.
document.addEventListener("deviceready", function(){
// PushPlugin을 설치했다면 window.plugins.pushNotification.register를 이용해서 iOS 푸시 서비스를 등록한다.
window.plugins.pushNotification.register(tokenHandler, errorHandler, {
"badge":"true", // 뱃지 기능을 사용한다.
"sound":"true", // 사운드를 사용한다.
"alert":"true", // alert를 사용한다.
"ecb": "onNotificationAPN" // 디바이스로 푸시가 오면 onNotificationAPN 함수를 실행할 수 있도록 ecb(event callback)에 등록한다.
});
});
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment