0

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.

  • There is not enough information in this post for you to get a good answer. We need to see what you have tried, what has worked, what has not worked, and any errors you get. Please see this [Link](https://stackoverflow.com/help/how-to-ask) on how to ask a good question so you get better answers. – Edney Holder Mar 09 '20 at 14:56

1 Answers1

0

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
    }
    
davidgyoung
  • 63,876
  • 14
  • 121
  • 204
  • Why thank you for the advice, I will try this approach. I would have thought this maybe something many people come up against. I'll let you know how I get on :) – CraigBlackpool Mar 10 '20 at 09:18