Share via


백그라운드에서 지오펜스 이벤트 수신 대기(HTML)

[ 이 문서는 Windows 런타임 앱을 작성하는 Windows에서 8.x 및 Windows Phone 8.x 개발자를 대상으로 합니다. Windows 10용으로 개발하는 경우에는 최신 설명서를 참조하세요.]

이 항목에서는 앱에서 Geofence 알림을 수신 대기하도록 백그라운드 작업을 설정하는 단계를 안내합니다.

로드맵: 이 항목은 다음 항목과 연관되어 있습니다. 참고 항목:

소개

지오펜스를 만들었으면, 지오펜스 이벤트가 발생할 때 일어날 일을 처리하기 위한 논리를 추가해야 합니다. 설정한 MonitoredStates에 따라, 다음의 경우 이벤트를 수신할 수 있습니다.

  • 사용자가 관심 영역에 들어왔을 때.
  • 사용자가 관심 영역을 떠났을 때.
  • 지오펜스가 만료되거나 제거되었을 때. 제거 이벤트에 대해서는 백그라운드 앱이 활성화되지 않습니다.

이 항목에서는 지오펜스 이벤트가 발생하면 이를 앱에 알리도록 백그라운드 작업을 설정하는 방법에 대해 설명합니다. 그러나 실행 중인 앱에서 직접 이벤트를 처리할 수도 있습니다. 자세한 내용은 포그라운드에서 지오펜스 알림 처리지오펜스에 대한 지침을 참조하세요.

백그라운드에서 지오펜스 이벤트를 수신 대기하려면 몇 가지 단계가 필요합니다.

  • 앱의 매니페스트에서 백그라운드 작업 선언
  • 앱에서 백그라운드 작업을 등록합니다. 이벤트가 트리거될 때 앱이 인터넷에 액세스해야 하는 경우(예: 클라우드 서비스에 액세스) 이에 대한 플래그를 설정할 수 있습니다. 이벤트가 트리거될 때 사용자에게 표시되어 사용자가 알 수 있도록 플래그를 설정할 수도 있습니다.
  • 앱이 포그라운드에서 실행 중일 때 앱 위치 사용 권한을 허용하도록 사용자에게 메시지를 표시합니다.

지오펜스 상태 변경 이벤트 등록

앱 매니페스트의 선언 탭에서 위치 백그라운드 작업에 선언을 추가합니다. 이렇게 하려면 다음을 수행합니다.

  • 백그라운드 작업 유형의 선언을 추가합니다.
  • 위치의 속성 작업 유형을 설정합니다.
  • 이벤트가 트리거될 때 호출하도록 앱에 진입점을 설정합니다.

백그라운드 작업 등록

다음 코드는 지오펜싱 백그라운드 작업을 등록합니다. 지오펜스가 생성되면 위치 사용 권한을 확인한다는 점을 기억하세요. 자세한 내용은 지오펜스 설정을 참조하세요.

function registerBackgroundTask() {
    try {
        // Request lockscreen access
        Background.BackgroundExecutionManager.requestAccessAsync().done(
            function (backgroundAccessStatus) {
                var builder =  new Windows.ApplicationModel.Background.BackgroundTaskBuilder();

                // Register the background task
                builder.name = sampleBackgroundTaskName;
                builder.taskEntryPoint = sampleBackgroundTaskEntryPoint;
                builder.setTrigger(new Windows.ApplicationModel.Background.LocationTrigger(Windows.ApplicationModel.Background.LocationTriggerType.geofence));

                // If it is important that there is user presence and/or
                // internet connection when OnCompleted is called
                // the following could be called before calling Register()
                // var condition = new SystemCondition(SystemConditionType.userPresent | SystemConditionType.internetAvailable);
                // builder.addCondition(condition);

                geofenceTask = builder.register();

                geofenceTask.addEventListener("completed", onCompleted);

                LocationTriggerBackgroundTask.updateButtonStates(/*registered:*/ true);

                switch (backgroundAccessStatus) {
                    case Background.BackgroundAccessStatus.unspecified:
                    case Background.BackgroundAccessStatus.denied:
                        WinJS.log && WinJS.log("This app is not allowed to run in the background.", "sample", "status");
                        break;

                    default:
                        // Finish by getting an initial position. This will present the location consent
                        // dialog if it's the first attempt for this application to access location.
                        getGeopositionAsync();
                        break;
                }
            },
            function (e) {
                // Did you forget to do the background task declaration in the package manifest?
                WinJS.log && WinJS.log(e.toString(), "sample", "error");
            }
        );
    } catch (ex) {
        // HRESULT_FROM_WIN32(ERROR_NOT_SUPPORTED) === -2147024846

        if (ex.number === -2147024846) {
            WinJS.log && WinJS.log("Location Simulator not supported.  Could not get permission to add application to the lock screen, this application must be added to the lock screen before the background task will run.", "sample", "status");
        } else {
            WinJS.log && WinJS.log(ex.toString(), "sample", "error");
        }
    }
}

관련 항목

로드맵

JavaScript로 작성한 앱용 로드맵

앱용 UX 디자인

작업

지오펜스 설정

포그라운드에서 지오펜스 알림 처리

백그라운드 작업에서 지오펜스 알림 처리

참조

Geoshape

Geofence

Geolocator

다른 리소스

Windows 10 지리적 위치 샘플

Windows 8.1 지리적 위치 샘플

지오펜싱에 대한 지침