1

I am trying to implement location-triggered-notification in an iOS app, which I recently started using SwiftUI.

As a first experimenting step, I have a button. And when tapped it should create a notification intented to fire once I get close to a certain location. As a starting point I read this page. But it is not all crystal clear when putting it into practice.

The location part is working but not the notification one. Though I have used CoreLocation and UserNotifications before, I have no previous experience with location triggered notifications.

Hereafter is the relevant (not working) code that I have at this point. Any tip or advice, in the right direction to make it work will be welcome.

import SwiftUI
import MapKit

struct ContentView: View {
    ......
    func handleShareLocation() {
        print(#function)
        //TRIAL-CODE
        guard CLLocationManager.locationServicesEnabled() else {
            return
        }
        UNUserNotificationCenter.current().requestAuthorization(
            options: [.alert, .sound, .badge],
            completionHandler: { _,_ in //[weak self] granted, _ in
                /*guard granted else {
                    self?.delegate?.notificationPermissionDenied()
                    return
                }*/

                let notificationInfo = LocationNotificationInfo (notificationId: "nyc_promenade_notification_id",
                locationId: "nyc_promenade_location_id",
                radius: 500.0,
                latitude: 40.696503,
                longitude: -73.997809,
                title: "Welcome to the Brooklyn Promenade!",
                body: "Tap to see more information",
                data: ["location": "NYC Brooklyn Promenade"])

                self?.requestNotification(notificationInfo:     notificationInfo)
        })
        //TRIAL-CODE
    }


    var body: some View {
        VStack {
            Spacer()
            Button(action: {
                self.handleShareLocation()
            }) {
                Text("Create location notification")
                .padding()
            }
            Spacer()
        }
    }
}

Beside, I am also using the class:

class LocationNotificationScheduler: NSObject {
     ........
}

coming from the document I mentioned. In there two unclear points are:

  1. Where is LocationNotificationSchedulerDelegate coming from?
  2. What is notificationScheduled?

Those two points are causing error messages like:

Use of undeclared type 'LocationNotificationSchedulerDelegate'

Let me know if more information is needed.

Michel
  • 10,303
  • 17
  • 82
  • 179
  • 1
    Reading the article there is a link to a gist that contains a definition of LocationNotificationInfo https://gist.github.com/jgsamudio/797ecbb1cffc8bb0bf7e81c03493c622 – Andrew Jul 06 '20 at 08:02
  • True. I had missed this one. Thanks. I may still have a long way to go before it all works though. – Michel Jul 06 '20 at 08:22
  • I edited my post to reflect the progress I made. It is still not working. – Michel Jul 06 '20 at 09:48
  • 1
    You really need to read the article. The writer even supplied a sample app (on github) with all the code that he used. The link is in the article. LocationNotificationSchedulerDelegate is a custom delegate that the writer created. You should probably directing the questions you are having to the writer of the article either on medium or on their github. – Andrew Jul 06 '20 at 11:12
  • Well, I'll take another look then. I only saw a few chunks of code on Gist (missing the one you mentioned earlier). – Michel Jul 06 '20 at 11:36

0 Answers0