본문 바로가기

나는개발자다

phonegap으로 안드로이드 push 개발하기 ( client 부분 )

이 문서는 cordova  4.1.2 기준으로 설명한다. (cordova --version 하면 나온다)


참고 : phonegap 설치  ,  phonegap 빌드하기


이 문서는 http://web.geusgod.pe.kr:8081/20476 를 내용을 정리했을 뿐이다.


1. push plugin 설치하기

d:\hellodemo>cordova plugin add https://github.com/phonegap-build/PushPlugin.git


설치하다가 window 일경우  plugman temp create fail?? 비슷한 에러 메세지가 나오는 경우가 있다. 

단순히 temp 폴더에 plugman?  폴더를 만들어주면 된다. (구글링 검색하면 나온다.)



2. D:\helloDemo\platforms\android\assets\www\plugins\com.phonegap.plugins.PushPlugin\www\PushNotification.js 파일을 www 폴더에 복사한다.


3. jquery.js를 www 폴더에 복사한다.


4. device plugin 추가

d:\hellodemo>cordova plugin add https://git-wip-us.apache.org/repos/asf/cordova-plugin-device.git

혹은 

d:\helloworld>cordova plugin add org.apache.cordova.device


5. andorid에 permission 추가.

AndroidManifest.xml 파일에 <uses-permission android:name="android.permission.READ_PHONE_STATE" /> 


6. GCM 등록하기 ( Google Cloud Message )

http://blog.saltfactory.net/216 이 문서 참고


www.폴더에 있는 html 파일의 소스이다. (index.html or default.html or etc....)


<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8" />
    <title>Notification Example</title>
 
    <script type="text/javascript" charset="utf-8" src="jquery.js"></script>
    <script type="text/javascript" charset="utf-8" src="cordova.js"></script>
    <script type="text/javascript" charset="utf-8" src="PushNotification.js"></script>
    <script type="text/javascript" charset="utf-8">
        document.addEventListener("deviceready", onDeviceReady, false);
      
        function onDeviceReady() {
            var pushNotification;
            pushNotification = window.plugins.pushNotification;
     
            if (device.platform == 'android' || device.platform == 'Android') {
                pushNotification.register(successHandler, errorHandler,{"senderID":"본인의Project Number입력","ecb":"onNotificationGCM"});
            } else {
                pushNotification.register(tokenHandler, errorHandler,{"badge":"true","sound":"true","alert":"true","ecb":"onNotificationAPN"});
            }      
         
            $("#app-status-ul").append('<li>deviceready event received</li>');
         
            document.addEventListener("backbutton", function(e)
            {
                $("#app-status-ul").append('<li>backbutton event received</li>');
         
                if( $("#home").length > 0)
                {
                    e.preventDefault();
                    pushNotification.unregister(successHandler, errorHandler);
                    navigator.app.exitApp();
                }
                else
                {
                    navigator.app.backHistory();
                }
            }, false);
         
            // aditional onDeviceReady work…
        }
         
        // result contains any message sent from the plugin call
        function successHandler (result) {
            alert('result = '+result);
            //$("#app-status-ul").append('<li>result = '+result+'</li>');
        }
             
        // result contains any error description text returned from the plugin call
        function errorHandler (error) {
            alert('error = '+error);
            //$("#app-status-ul").append('<li>error = '+error+'</li>');
        }
                 
        function tokenHandler (result) {
            // Your iOS push server needs to know the token before it can push to this device
            // here is where you might want to send it the token for later use.
            alert('device token = '+result);
            //$("#app-status-ul").append('<li>device token = '+result+'</li>');
        }
  
        // iOS
        function onNotificationAPN(event) {
            if (event.alert) {
                navigator.notification.alert(event.alert);
            }
         
            if (event.sound) {
                var snd = new Media(event.sound);
                snd.play();
            }
         
            if (event.badge) {
                pushNotification.setApplicationIconBadgeNumber(successHandler, errorHandler, event.badge);
            }
        }
         
         
        // Android
        function onNotificationGCM(e) {
            $("#app-status-ul").append('<li>EVENT -> RECEIVED:' + e.event + '</li>');
 
            switch( e.event )
            {
                case 'registered':
                if ( e.regid.length > 0 )
                {
                    $("#app-status-ul").append('<li>REGISTERED -> REGID:' + e.regid + "</li>");
                    // Your GCM push server needs to know the regID before it can push to this device
                    // here is where you might want to send it the regID for later use.
                    console.log("regID = " + e.regID);
                }
                break;
 
                case 'message':
                    // if this flag is set, this notification happened while we were in the foreground.
                    // you might want to play a sound to get the user's attention, throw up a dialog, etc.
                    if (e.foreground)
                    {
                        $("#app-status-ul").append('<li>--INLINE NOTIFICATION--' + '</li>');
 
                        // if the notification contains a soundname, play it.
                        //var my_media = new Media("/android_asset/www/"+e.soundname);
                        //my_media.play();
                    }
                    else
                    {   // otherwise we were launched because the user touched a notification in the notification tray.
                        if (e.coldstart)
                            $("#app-status-ul").append('<li>--COLDSTART NOTIFICATION--' + '</li>');
                        else
                        $("#app-status-ul").append('<li>--BACKGROUND NOTIFICATION--' + '</li>');
                    }
 
                    $("#app-status-ul").append('<li>MESSAGE -> MSG: ' + e.payload.message + '</li>');
                    $("#app-status-ul").append('<li>MESSAGE -> MSGCNT: ' + e.payload.msgcnt + '</li>');
                break;
 
                case 'error':
                    $("#app-status-ul").append('<li>ERROR -> MSG:' + e.msg + '</li>');
                break;
 
                default:
                    $("#app-status-ul").append('<li>EVENT -> Unknown, an event was received and we do not know what it is</li>');
                break;
            }
        }      
    </script>
  </head>
  <body>
    <div id="home">
        <div id="app-status-div">
            <ul id="app-status-ul">
                <li>Cordova PushNotification Plugin Demo</li>
            </ul>
        </div>
    </div>
  </body>
</html>




7. Build

d:\helloDemo>cordova build

필드하게 되면 src 폴더에 class가 몇 개 등록이 된다.



결과물 ( 해드폰에 설치해서 실행했다.)


처음엔 위 2줄만 나오고 몇 초 후에 ( 5-10초)에 아래 2줄이 나온다...