0

In our app, the user will clock-in when he starts his work. If the user forgets to clock-out, we will have to automatically clock him out after 24 hours from the clock-in time. The app might not be in the active/background state for such a long time. It might be terminated. So our idea is to post a local notification through which will execute the code in the background to clock him out. This notification has to be a silent notification. But our understanding from the research is that local notifications cannot be silent. So is there any other way we could achieve this? Or can we actually schedule a silent local notification?

class func generateLocalNotificationWith(timeInterval : TimeInterval, title : String, message : String, mobileTimeClockId: Int)
{    
    UNUserNotificationCenter.current().removeAllPendingNotificationRequests()
    let content = UNMutableNotificationContent()

    //adding title, subtitle, body and badge
    content.title = title
    content.body = message

    let userInfoDictionary = ["badge":"0","content-available": "1"]

    let dict = ["aps": userInfoDictionary, "MobileTimeClockId": mobileTimeClockId] as [String : Any]

    content.userInfo = dict

    //getting the notification trigger
    let trigger = UNTimeIntervalNotificationTrigger(timeInterval: timeInterval, repeats: false)

    //getting the notification request
    let request = UNNotificationRequest(identifier: "SimplifiedIOSNotification", content: content, trigger: trigger)

    //adding the notification to notification center
    UNUserNotificationCenter.current().add(request, withCompletionHandler: nil)
}
Mano
  • 670
  • 1
  • 9
  • 28
  • you can use silent notification for this! Please review this link which describes difference between both the notifications https://stackoverflow.com/questions/42275060/what-is-difference-between-remote-notification-and-silent-notification-in-ios – Sachin Dobariya Jul 25 '18 at 09:19

1 Answers1

0

If you want a notification to be silent and do background work it has to be a push notification. A local notification can't wake your app without some user interaction.

The best way to do what you want is to use Core Location region monitoring to wake your app when the user physically leaves their workplace. Region monitoring can wake your app silently in the background and let you do some work. The problem is that it's not 100% assured that it will be triggered and the user will need to accept background location permissions.

nevan king
  • 112,709
  • 45
  • 203
  • 241
  • Thanks! Is it possible to achieve this similar to an alarm clock concept? Can we post an alert similar to alarm and force the user to clock out? I see the phone's alarm app works in two ways. 1) If the phone is locked, it shows a full page notification for which the user has to either click on snooze or stop. 2) If the phone is unlocked, it shows a notification at the top (similar to push notification) and urges the user to react. And in the second case, i was able to clear the notification by a swipe. Can we achieve this? – Mano Jul 25 '18 at 09:52
  • @Mano Nope, you can't make anything that the user *has* to respond to. They can ignore or disable all your notifications and you can't do anything about it. System apps can do much more than ordinary developers can. – nevan king Jul 25 '18 at 10:00
  • @nevanking how lengthy task we can perform in action of local notification which is triggered from region monitoring in iOS ? if you have any idea then please let me know.Thanks I am trying to achieve one task when app got awake from geofence region monitoring: https://stackoverflow.com/questions/60184807/action-from-local-notification-not-working-in-ios-13# – shaqir saiyed Feb 12 '20 at 10:06