Is there any way to trigger local notifications when iOS 10 app is in the background?

827 Views Asked by At

I'm trying trigger local notification when I receive remote push notification (VoIP PushKit) which start my app in the background.

I see my local notification in array of the pending notifications at the UNUserNotificationCenter.

[[UNUserNotificationCenter currentNotificationCenter] getPendingNotificationRequestsWithCompletionHandler:
^(NSArray<UNNotificationRequest *> * _Nonnull requests) 
{
    NSLog(@"Local notifications: %@",requests);
}];


"<UNNotificationRequest: 0x17162e500; identifier: 2A364020-3BA2-481B-9255-16EBBF2F4484, content: <UNNotificationContent: 0x1708e8080; title:test, subtitle: (null), body: test2, categoryIdentifier: missed, launchImageName: , peopleIdentifiers: (\n), threadIdentifier: , attachments: (\n), badge: 1, sound: <UNNotificationSound: 0x1704bfe60>, hasDefaultAction: YES, shouldAddToNotificationsList: YES, shouldAlwaysAlertWhileAppIsForeground: YES, shouldLockDevice: YES, shouldPauseMedia: NO, isSnoozeable: NO, fromSnooze: NO, darwinNotificationName: (null), darwinSnoozedNotificationName: (null), trigger: <UNTimeIntervalNotificationTrigger: 0x171624260; repeats: NO, timeInterval: 5.000000>>"

But it doesn't appear after 5 sec. I use UNTimeIntervalNotificationTrigger.

I have successfully call the local notification when the application in the foreground. I use the same function for trigger by calling it by pressing the button.

Is there any way to trigger local notification when iOS app is in the background?

1

There are 1 best solutions below

0
Piepants On

Yes its possible, and if its not working for you then you must be doing something incorrect with your code, but you didn't show it so can't comment on that.

Here's some working debugging code I have to display a notification on voip push receiving:

func pushRegistry(_ registry: PKPushRegistry, didReceiveIncomingPushWith payload: PKPushPayload, forType type: PKPushType)
    {
        registerBackgroundTask()
        //present a local notifcation to visually see when we are recieving a VoIP Notification
        if UIApplication.shared.applicationState == UIApplicationState.background {
            let notification  = UNMutableNotificationContent()
            notification.title      = "Voip Push"
            notification.body       = "App in background"
            let trigger = UNTimeIntervalNotificationTrigger(timeInterval: 1, repeats: false)
            let request = UNNotificationRequest(identifier: "id", content: notification, trigger: trigger)
            DispatchQueue.main.async {
                UNUserNotificationCenter.current().add(request)
                { (error) in
                    if error != nil
                    {
                        let e = error as? NSError
                        NSLog("Error posting notification \(e?.code) \(e?.localizedDescription)")
                    }
                }
            }
        }

        else
        {
            let notification  = UNMutableNotificationContent()
            notification.title      = "Voip Push"
            notification.body       = "App in foreground"
            let trigger = UNTimeIntervalNotificationTrigger(timeInterval: 1, repeats: false)
            let request = UNNotificationRequest(identifier: "id", content: notification, trigger: trigger)
            DispatchQueue.main.async {
                UNUserNotificationCenter.current().add(request)
                { (error) in
                    if error != nil
                    {
                        let e = error as? NSError
                        NSLog("Error posting notification \(e?.code) \(e?.localizedDescription)")
                    }
                }
            }
        }

        NSLog("incoming voip notfication: \(payload.dictionaryPayload)")
    }