Search code examples
iosswiftxcodenotificationsguided-access

Guided access prevents user notifications from showing - how to fix? (Swift 3)


My app shows user notifications to the user (that pop up on the top of the screen) at random points throughout the app (they get fired remotely from the server, ie when another user sends a message to them), using the UserNotifications framework.

The notifications work perfectly when NOT in guided access mode, and they fire properly while user is in the app.

This is from app-delegate to enable notifications showing in-app:

extension AppDelegate : UNUserNotificationCenterDelegate {
    func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {
        completionHandler(.alert)
    }

This is how the notifications get fired (when an event gets triggered from Firebase):

 func sendNotification(notTitle: String, notSubtitle: String, notBody: String) {
        let notif = UNMutableNotificationContent()
        notif.title = notTitle
        notif.subtitle = notSubtitle
        notif.body = notBody

        let notifTrigger = UNTimeIntervalNotificationTrigger(timeInterval: 0.1, repeats: false)
        let request = UNNotificationRequest(identifier: "myNotification", content: notif, trigger: notifTrigger)
        UNUserNotificationCenter.current().add(request, withCompletionHandler: nil)
    }

So all the above works fine.

But my problem is, once the app goes into guided-access mode (triple tap home button - passcode locks the device to my specific app), then the notifications will not show. Guided access blocks them in some way.

It is essential for my app to be in guided access as it is provided from physical staff in a facility to clients.

Is there a way to prevent the guided access mode from blocking user notifications? Or some function I have to include in app-delegate?

I could not find anything on SO or the web regarding this issue. Any help would be greatly appreciated


Solution

  • User Notifications are generally intended to notify the user of something when they are not already in the app, and when the user taps it the app would open. For this reason, it is intended that Guided Access would block user notifications since they are usually tied with other apps. I don't know of a way around this.

    The best thing to do would probably be to create some sort of visual indication in the app itself for things that would merit notifications. You can still use user notifications, but when the user is inside the app already you can do some custom visual indication that a new message is received, etc.