Can't get notification to work when app is killed (Swift)

Hey guys, I could really use some help with this. I’m having trouble getting my app to send notification when the app is killed. It will do it when in the background or when the phone is turned off but not when it is killed. I followed this tutorial to the dot: http://developer.estimote.com/proximity/ios-tutorial/#add-an-estimote-monitoring-manager

I also read this post here: https://community.estimote.com/hc/en-us/articles/203253193-Pushing-notifications-from-iBeacon-when-the-app-is-in-the-background-or-killed that showed exactly how to do it but that is in objective C correct? (Sorry I’m a bit of a beginner)

There was this one post I saw that said that the proximity app template does this but I couldn’t get it to work. This was the post: Get my app to detect beacons while in the background

Is there something I’m missing? What do I need to add to my app to make this work? Help would be GREATLY appreciated as I’ve been stuck on this for a while now.

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
    
    
    ESTConfig.setupAppID("weattendance-2lq", andAppToken: "a65fb0eb6418d444bc866aa8b5d44e15")

    self.monitoringManager = ESTMonitoringV2Manager(
        desiredMeanTriggerDistance: 2.0, delegate: self)
    
    self.monitoringManager.startMonitoring(forIdentifiers: [
        "2fe9f9eab6f63a83403d83d5fdd5f338",
        "600b8a212b5931dd7dc79b47566a032f"])
    
    let center = UNUserNotificationCenter.current()
    center.requestAuthorization(options: [.alert, .sound]) { (granted, error) in
        print("notifications allowed? = \(granted)")
    }


    return true
} 

This is what I have in my app delegate right now and according to the first community post, code for monitoring in background should be there but I don’t know what it is in swift.

If anyone could help me figure out how to make this work it would mean the world to me.

This is a fantastic question, and a great opportunity for us to explain how re-launching from the terminated state works for Estimote Monitoring.

Estimote Monitoring on iOS uses two system frameworks under the hood to do its job: Core Bluetooth and Core Location. And actually, we primarily use Core Bluetooth, but the one problem with CB we run into is specifically about re-launching apps—it works much less reliably than with Core Location. Hence, we added Core Location to the mix, to make apps using Estimote Monitoring be reliably relaunched when entering range of beacons.

The thing is, for Core Location to relaunch the app, you need to completely leave the range of all your beacons for at least 30 seconds. Then, when you re-enter the range, the app should get relaunched. (Note that Estimote Monitoring broadcasting by default uses transmit power of -4 dBm, which can easily translate to 50-70-100 meters of range. This is independent of what desiredMeanTriggerDistance you’ve set.)

This is usually not a problem at all in real-world situations: people leave the venue where the beacons are deployed, go home, play games on their smartphone, the app gets terminated due to memory pressure, then they come back to the venue the next day, and the app gets relaunched with no problems.

When developing your app however, it’s easy to remain in range of the beacon (even if the Estimote Monitoring triggered an exit, because the exit is only for your desiredMeanTriggerDistance), and thus relaunching will seemingly not work properly.

Things you could try:

  • kill the app
  • move at least 100 m away from all your beacons
  • wait at least 30 seconds
  • move back to the beacon
  • expect the event to fire

Alternatively, you can enable Flip to Sleep on your beacons, and instead of moving away from all your beacons, just flip them all to sleep for at least 30 seconds, then flip them back. This might be slightly less reliable though, because we optimized Estimote Monitoring against real-world scenario of people gradually moving toward the beacon and the signal strength increasing; and Flip to Sleep instead abruptly turns the signal off and on.

I tried the flip to sleep to test them, maybe that could be it. Can you confirm for me that the developer.estimote.com tutorial for adding an estimote monitoring manager and the proximity app template should work (as in send a notification when the app is fully killed and not just in background) just so I’m not getting myself confused.
Also thanks for the fast reply!

Also, I use the lowest transmit power, so moving away 5-10 feet should work right? Do you mind just confirming that if I follow the “Add proximity events to an iOS app” that the phone will get the notification even if the app is fully killed (double clicked home button, swiped up to kill the app)… Sorry, I just want to make sure it’s not something wrong with the code that I’m stuck on.

Hi heypiotr,

Gone through your post.Still app don’t find beacons FREQUENTLY as it comes under Beacon Region(When app is in terminated mode)

Can you guide something?
OR
Is there any special settings need to configure to detect beacon as it comes under beacon region in terminated mode?
OR
Is there any sample code where I can verify by upto now what I have implemented is in correct way for termination mode.

ESTMonitoringV2Manager is not work for Proximity ( event did enter and exit)

should try
class AppDelegate : ESTBeaconManagerDelegate

ESTConfig.setupAppID(“weattendance-2lq”, andAppToken: “a65fb0eb6418d444bc866aa8b5d44e15”)
let beaconManager = ESTBeaconManager()
self.beaconManager.delegate = self
self.beaconManager.requestAlwaysAuthorization()
let beacons = [[[[ beacon array ]]]]] //<- Please change

for beacon in beacons! {
self.beaconManager.startMonitoring(for: CLBeaconRegion(
proximityUUID: UUID(uuidString: beacon.uuid!)!,
major: UInt16(beacon.major!), minor: UInt16(beacon.minor!), identifier: beacon.Identifier!))
}


3 listening methods that you have to declare
func beaconManager(_ manager: Any, didEnter region: CLBeaconRegion) {
print("beaconManager didEnter ")
}
func beaconManager(_ manager: Any, didExitRegion region: CLBeaconRegion) {
print(“beaconManager didExitRegion (region.major!) (region.minor!)”)
}

func beaconManager(_ manager: Any, didStartMonitoringFor region: CLBeaconRegion) {
print(“beaconManager didStartMonitoringFor (region.major!) (region.minor!)”)
}

only this way work at background.

Also, I use the lowest transmit power, so moving away 5-10 feet should work right?

Are you sure? If you enabled Estimote Monitoring broadcasting on your beacon, then we hard-set the transmit power to -4 dBm, and there’s no way to change that with the Estimote app right now. (Basically, with the desired trigger distance set via API, we don’t see much reason to use lower transmit powers, battery impact is negligible.)

The only way you could currently set it to a lower value is by lowering the transmit power of the Estimote Location packet in Estimote Cloud. We’ll be changing that soon though, to follow the same behavior as in the Estimote app.

Do you mind just confirming that if I follow the “Add proximity events to an iOS app” that the phone will get the notification even if the app is fully killed (double clicked home button, swiped up to kill the app)… Sorry, I just want to make sure it’s not something wrong with the code that I’m stuck on.

and

Is there any special settings need to configure to detect beacon as it comes under beacon region in terminated mode?
OR
Is there any sample code where I can verify by upto now what I have implemented is in correct way for termination mode.

Yes, I can confirm there’s no extra steps or configuration necessary, just following the proximity tutorial should do. Only the caveat about leaving the range of all your beacons [1] for at least 30 seconds, before the app can get relaunched by iOS upon returning in range.

[1] this is important, it’s not just the beacons you’re monitoring for, it really needs to be all the beacons with Estimote Monitoring enabled

@Septiem if Estimote Monitoring doesn’t work for you, make sure you’ve enabled the Estimote Monitoring broadcasting on your beacons via the Estimote iOS/Android app.

@heypiotr You should publish this info about behaviour of relaunching after killing the app in the main documentation of ProximitySDK or GitHub Readme. It can save developers many hours of unsuccessful testing.

Hi,
I am developing an iOS app to detect beacons and send notification when the user enters and exits the beacons’ zones. When the user enters the zone the notification is sent to the device but the problem is that when the app is opened by user and killed by him, the user does not receive the exit notification. As I understand, it is because of relaunching the app.
I really need users receive the exit notification. So, is there any solution that makes the app relaunch when the user is in the range of beacons?

Hi @heypiotr I am also not getting notified on enter/exit events from Estimote proximity beacon when app is killed, using iOS 14.2 while location permission is set to always and bluetooth is also turned on. can you please help me out that what could be the possible reasons why its working when app is active and not when app is killed.
I also kept the iPhone away from beacon for at least two minutes before entering the region. looking forward to hear from you soon.