Share via


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

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

이 항목에서는 앱의 포그라운드에서 Geofence 알림을 처리하는 단계를 안내합니다.

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

소개

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

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

실행 중인 앱에서 직접 이벤트를 수신 대기할 수도 있고, 이벤트 발생 시 백그라운드 알림을 수신하도록 백그라운드 작업을 등록할 수도 있습니다. 백그라운드 작업 및 지오펜스에 대한 자세한 내용은 백그라운드에서 지오펜스 이벤트 수신 대기, 백그라운드 작업에서 지오펜스 알림 처리지오펜스에 대한 지침을 참조하세요.

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

앱이 지오펜스 상태 변경의 포그라운드 알림을 수신하도록 하려면 이벤트 처리기를 등록해야 합니다. 이는 대개 지오펜스를 만들 때 설정됩니다.

    function initialize() {

    // other initialization logic

    Windows.Devices.Geolocation.Geofencing.GeofenceMonitor.current.addEventListener("geofencestatechanged", onGeofenceStateChanged);
}

지오펜스 이벤트 처리기 구현

다음 단계에서는 이벤트 처리기를 구현합니다. 여기에서 취하는 조치는 앱에서 지오펜스를 사용하는 목적에 따라 달라집니다.

public async void OnGeofenceStateChanged(GeofenceMonitor sender, object e)
{
    var reports = sender.ReadReports();

    await Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () =>
    {
        foreach (GeofenceStateChangeReport report in reports)
        {
            GeofenceState state = report.NewState;

            Geofence geofence = report.Geofence;

            if (state == GeofenceState.Removed)
            {
                // remove the geofence from the geofences collection
                GeofenceMonitor.Current.Geofences.Remove(geofence);
            }
            else if (state == GeofenceState.Entered)
            {
                // Your app takes action based on the entered event

                // NOTE: You might want to write your app to take particular
                // action based on whether the app has internet connectivity.

            }
            else if (state == GeofenceState.Exited)
            {
                // Your app takes action based on the exited event

                // NOTE: You might want to write your app to take particular
                // action based on whether the app has internet connectivity.

            }
        }
    });
}


관련 항목

로드맵

JavaScript로 작성한 앱용 로드맵

앱용 UX 디자인

작업

지오펜스 설정

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

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

참조

Geoshape

Geofence

Geolocator

다른 리소스

Windows 10 지리적 위치 샘플

Windows 8.1 지리적 위치 샘플

지오펜싱에 대한 지침