3

I'm trying to create a notification which triggers when entering a region using UNNotificationLocationTrigger. I implemented it like this:

let destination = //destination is added by user through interface//

let notification = UNMutableNotificationContent()
notification.title = "You've reached your destination"
notification.body = "Some"
notification.sound = UNNotificationSound.default()

let destRegion = CLCircularRegion(center: destination, radius: 1000.0, identifier: "DistanceToDestination")
destRegion.notifyOnEntry = true
let trigger = UNLocationNotificationTrigger(region: destRegion, repeats: false)

let request = UNNotificationRequest(identifier: "destAlarm", content: notification, trigger: trigger)
UNUserNotificationCenter.current().add(request, withCompletionHandler: { error in
     if error == nil {
           print("Successful notification")
     } else {
           print(error ?? "Error")
     }
})

I tried it on a real device and walked around. It worked 2 times perfectly when I entered the region (I checked it with a map and region as circle overlay in map). But after that all other attempts failed.

EDIT: For those reading the comments. It's not working again. So it seems to be pretty inconsistent. If somebody know if it's a bug or know how to fix it I would be very grateful.

Codey
  • 1,131
  • 2
  • 15
  • 34
  • did you called CLLocationManager().startUpdateLocation()? I have exact problem here, notification doesn't trigger on simulator – zzheads Jan 17 '17 at 11:18
  • 1
    No I didn't. After a week not doing anything to my project I tried it again just today after your replied and it worked again. This time I made more tests and every single time it worked (on the simulator). That's very strange... – Codey Jan 17 '17 at 13:18
  • Agreed, after some unsuccessful attempts to trigger it I gave up and solved my problem with implementing didEnterRegion/didExitRegion CLLocationManager delegate methods where I make notification with trigger:timeInterval manually... – zzheads Jan 17 '17 at 13:25
  • I think you didn't understand me. The UNLocationNotificationTrigger now works again for me, which is very strange because a week ago it didn't and I haven't changed any code since then. Comment to your solution: I also thought about that but it is kind of a workaround solution because of the time interval. – Codey Jan 17 '17 at 13:27
  • I got it, I meant "agreed" with that is very strange. I have no time to wait when simulator will trigger it by location... – zzheads Jan 17 '17 at 13:28
  • Oh ok. Well I hope that my app now works with that and doesn't break again. Would you like to upload your code here. It wouldn't be the answer but a good and working solution. – Codey Jan 17 '17 at 13:30
  • 1
    Sure: https://github.com/zzheads/ProximityReminders – zzheads Jan 17 '17 at 13:31
  • I've got UNLocationNotificationTrigger to work fairly consistently by doing the following: http://stackoverflow.com/questions/40628095/unlocationnotificationtrigger-not-working-in-simulator/41835750#41835750 – Berthier Lemieux Jan 24 '17 at 17:59
  • Thanks but I already set it to `requestAlwaysLocation`. I'm having the problem that it works in the simulator but not on the real device. – Codey Jan 26 '17 at 08:51
  • @Codey I have the same problem, nothing happens, but is it really necessary with a CLLocationManager and startUpdateLocation(). Whats the point then with a UNLocationNotificationTrigger? I though the iOS-system would handle the location :S Can the app be triggered when not in use by the system? (like the time notification) – Jonas Feb 20 '17 at 12:59
  • @Jonas Calling startUpdateLocation() didn't help, so I don't know if it is necessary. The point of UNLocationNotificationTrigger is that you only have two write two lines of code (create a region, create location trigger) and then you have a geofencing trigger. It is much more simple than writing all that by yourself. I suppose UNLocationNotificationTrigger would trigger if the app is in the background, otherwise it wouldn't be very helpful. – Codey Feb 22 '17 at 17:06

1 Answers1

0

First of all, you should call locationmanager.startupdatinglocation Second of all, you should change the trigger so that repeats = true