Like in my other post about how to get push notifications working from your CoronaSDK app with Parse we covered the topic on how to get remote push notifications. This article is about how to trigger how to trigger proximity based notifications.

So what if you were developing an app for a client and they would like to drive nearby customers to their shop/restaurant etc. Well, this is what we could use it for.

Grab the entire code here.

This is pretty simple, first we need to write the function for calculating the distance between two locations.

-- Earth radius in meters.
local earthRad = 6378137
 
-- Works for short distances.
local function distanceApproximation(lat1, long1, lat2, long2)
 
	if not lat1 or not long1 or not lat2 or not long2 then
	   print(" ===>>> Missing latitudes and longitudes")
	   return
	end 
 
	local l1, lng1 = math.rad(lat1), math.rad(long1)
	local l2, lng2 = math.rad(lat2), math.rad(long2)
	local x = (lng2-lng1) * math.cos((l1+l2)*0.5)
	local y = (l2-l1)
	local d = math.sqrt(x * x + y * y) * earthRad
 
	print(" ===>>> distanceApproximation: "..d.." meters")
 
	return d
end

Now let’s set up the location handler function but before that we need to make some forward declarations and setup som variables.

-- lat/long of the location you wish to trigger the notification for.
local myLat, myLong = 0,0 
-- Our timer
local proximityTimer 
-- Delay for the notification
local alertDelay = 5 
-- Meters, max range.
local myProximity = 200 
 
local message = {
	alert = "You are within range of location!",
	badge = 1,
	sound = "alarm.caf"
}
 
local function updateLocation()
	local function locationHandler(event)
	    if event.errorCode then
	      print(" ===>>> LOCATION HANDLER ERROR : " .. tostring(event.errorMessage) )
	    else
	      print(" ===>>> CURRENT LOCATION COORDS, LAT : "..event.latitude..", LONG : "..event.longitude)
 
	      local distance = distanceApproximation(event.latitude, event.longitude, myLat, myLong)
 
	      if distance < myProximity then 	      	
                  print(" ===>>> PING!!! WE ARE WITHIN RANGE OF OUR LOCATION ("..distance.."m), SHOW LOCAL NOTIFICATION. : "..message.alert)
		 system.scheduleNotification( os.date( "!*t", os.time() + alertDelay) , message) 
 
		-- Cancel the proximityTimer if we are within range.
		timer.cancel(proximityTimer)
	      else
	        print(" ===>>> PING!!! NOT YET WITHIN RANGE OF OUR LOCATION ("..distance.."m)")
	      end
 
	    end
	  Runtime:removeEventListener("location", locationHandler)
	end
	Runtime:addEventListener("location", locationHandler)
end
 
-- Update location once upon startup.
updateLocation()
 
-- Fire the timer every 5 sec.
proximityTimer = timer.performWithDelay(5000, updateLocation, 0 )

 

local function notificationListener(event)
 
     if (event.type == "local") then
 
	 local badgeNum = native.getProperty("applicationIconBadgeNumber")
               badgeNum = badgeNum - 1
 
         native.setProperty("applicationIconBadgeNumber", badgeNum)
	 native.showAlert( "Hello", message.alert, { "OK" } )
         print(" ===>>> LOCAL NOTIFICATION, CLEAR BADGE!!")
     end
end
 
Runtime:addEventListener("notification", notificationListener)

Now that we have almost everything done, we need to setup some launchArgs.
Add this line at the very top of your main.lua

local launchArgs = ...

Now add this after the notificationListener.

if (launchArgs and launchArgs.notification) then
    print(" ===>>> EVENT launchArgs...")
    notificationListener(launchArgs.notification)
end