Can you apply time restrictions for ibeacon notifications when exiting a region?

28 Views Asked by At

I have implemented iBeacon into an app so that it will wake the app briefly from a suspended or killed state. A push notification is sent which would prompt the user to open the app if they wish to.

The problem is, when a user exits then enters a region again another notification is sent. In a shopping mall for example a user could walk past many beacons (enter and exit regions). What they probably will not want is lots of notifications annoying them.

Is there a way that you can control or restrict the number of notifications a user gets? For example time restrictions? Once a notification has been received then a user would not get another one for 15 mins or 30 mins etc?

There must be a solution as i am sure Apple would not want users to get lots of notifications that users dont want.

1

There are 1 best solutions below

1
On

There are no built-in tools to iOS SDKs to prevent multiple notifications from being sent in a specific time period. But you are correct that this is a very common problem. The solution is to just add a little bit of programming logic.

Here is a typical approach:

  1. Each time you send a notification, record a timestamp of when you sent it. Store this in the phone's persistent storage, so even if a user restarts the app or reboots the phone you will have a record of when the last notification was sent.

    UserDefaults.standard.set(Date().timeIntervalSince1970, 
                              forKey: "lastNotificationSentTime")
    
  2. Before you send a notification, check to see when the last time you sent one was. If it was too recent, suppress sending a new notification.

    let lastNotificationTime = UserDefaults.standard.value(forKey: "last") as? Double ?? 0.0
    if Date().timeIntervalSince1970 - lastNotificationTime < 60.0 {
      print("Not sending notification because I just sent one in the last 60 seconds.")
    }
    else {
      // TODO: Send notification here
    }