pushNotificationDelegate doesn't react to incoming push notifications - swift

Hi,

I'm trying to intercept incoming push notifications so I can extract the payload inside with an iOS client, using swift.

The client receives the notification just fine - notification shows if app is not active and I see the event in the logs (attached at bottom)

The iOS guide doesn't really mention interacting with incoming push notification themselves, but I found this guide on your site (iOS Integration Best Practices for Swift and Objective-C), and followed all the steps. However none of the functions in the notification handler are triggered when a push is received (receivedForegroundNotification, receivedBackgroundNotification, launchedFromNotification).

Don't know if it helps, but I have been able to get actions contained in the push to be triggered (according to the iOS guide).

Your help would be greatly appreciated.

Dave

 

relevant code in AppDelegate.swift:

var customPushHandler = PushHandler()

func
application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey : Any]?) -> Bool {
...
let config = UAConfig.default()
UAirship.takeOff(config)
UAirship.push().userPushNotificationsEnabled = true
PFUser.current()?["UAirshipChannelId"] = UAirship.push().channelID
PFUser.current()?.saveInBackground()
UAirship.push().pushNotificationDelegate = customPushHandler
...
}

 

PushHandler.swift:

import AirshipKit

class PushHandler: NSObject, UAPushNotificationDelegate {

func receivedForegroundNotification(notification: [NSObject : AnyObject], fetchCompletionHandler completionHandler: ((UIBackgroundFetchResult) -> Void)) {
        print("Received a notification while the app was already in the foreground: \(notification)")
        completionHandler(UIBackgroundFetchResult.noData)
    }

    func launchedFromNotification(notification: [NSObject : AnyObject], fetchCompletionHandler completionHandler: ((UIBackgroundFetchResult) -> Void)) {
        print("The application was launched or resumed from a notification: \(notification)");  
        for (key, value) in notification {          
            if key as! String == "aps" {
                print("body Found: \(value)")
            }
        }
        completionHandler(UIBackgroundFetchResult.noData)
    }
        
    func receivedBackgroundNotification(notification: [NSObject : AnyObject], actionIdentifier identifier: String, completionHandler: () -> Void) {
        print("The application was started in the background from a user notification button");
        completionHandler()
    }

    func receivedBackgroundNotification(notification: [NSObject : AnyObject], fetchCompletionHandler completionHandler: ((UIBackgroundFetchResult) -> Void)) {
        print("A CONTENT_AVAILABLE notification was received with the app in the background: \(notification)");
        completionHandler(UIBackgroundFetchResult.noData)
    }
}



 

UA trace logs when a push is received:

 

2016-11-22 08:51:53.817493 iseeya[4437:8695132] [D] +[UAAppIntegration userNotificationCenter:willPresentNotification:withCompletionHandler:] [Line 136] Notification center will present notification: <UNNotification: 0x17403ed00; date: 2016-11-22 06:51:51 +0000, request: <UNNotificationRequest: 0x1740326c0; identifier: EEA00682-6DB8-4357-B2A4-1669DED83CE7, content: <UNNotificationContent: 0x1742e3e80; title: (null), subtitle: (null), body: 123 godammit !!!!, categoryIdentifier: , launchImageName: , peopleIdentifiers: (
), threadIdentifier: , attachments: (
), badge: (null), sound: (null), hasDefaultAction: YES, shouldAddToNotificationsList: YES, shouldAlwaysAlertWhileAppIsForeground: NO, shouldLockDevice: NO, shouldPauseMedia: NO, isSnoozeable: NO, fromSnooze: NO, darwinNotificationName: (null), darwinSnoozedNotificationName: (null), trigger: <UNPushNotificationTrigger: 0x174017ca0; contentAvailable: NO, mutableContent: NO>>>
2016-11-22 08:51:53.818201 iseeya[4437:8695132] [I] +[UAAppIntegration handleIncomingNotification:foregroundPresentation:completionHandler:] [Line 228] Received notification: {
    "_" = "bd1cac0f-7540-47a2-b66b-fa66196d32ce";
    aps =     {
        alert = "123 godammit !!!!";
    };
}
2016-11-22 08:51:53.818441 iseeya[4437:8695132] [D] +[UAActionRunner runActionWithName:value:situation:metadata:completionHandler:] [Line 84] No action found with name aps, skipping action.
2016-11-22 08:51:53.819083 iseeya[4437:8695132] [D] +[UAActionRunner runActionWithName:value:situation:metadata:completionHandler:] [Line 84] No action found with name _, skipping action.
2016-11-22 08:51:57.461822 iseeya[4437:8695132] [D] __41-[UAAnalytics uploadOperationWithEvents:]_block_invoke [Line 536] Analytics data sent successfully. Status: 200

 

Didn't find what you were looking for?

New post

Comments

4 comments

  • Hello Dave,

    There is actually a newer, better way to do this now.

    We have a feature called "Automatic Integration" that automatically intercepts those push notification related delegate methods for you, so you don't have to handle them yourself.

    However, there are cases where you'll want to override that behavior to meet your use cases. In this case, you can actually disable Automatic Integration in the app and declare those push notification delegate methods to do what you need to do.

    To do so, you can follow our Automatic Integration portion of our main documentation.

    1
  • Hello Michael,

    thank you for the response.

    I have a follow up question:

    when applying the code from the automatic integration guide, Xcode informed me that :

    'UNUserNotificationCenter' is only available on iOS 10.0 or newer

    however my project requires compatibility with iOS versions 8, 9 & 10.

    so I tried using only these functions (which don't seem to raise any compatibility warnings):

    func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data)
    func application(_ application: UIApplication, didRegister notificationSettings: UIUserNotificationSettings) 
    func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable : Any], fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void)
    func application(_ application: UIApplication, handleActionWithIdentifier identifier: String?, forRemoteNotification userInfo: [AnyHashable : Any], completionHandler: @escaping () -> Void) 
    func application(_ application: UIApplication, handleActionWithIdentifier identifier: String?, forRemoteNotification userInfo: [AnyHashable : Any], withResponseInfo responseInfo: [AnyHashable : Any], completionHandler: @escaping () -> Void) }

    and not including "UNUserNotificationCenter.current().delegate = self" in didFinishLaunchingWithOptions

    UNUserNotificationCenterDelegate methods.

    and everything seems to work as it should -  Yay!!

    but I would like to know if omitting the use of UNUserNotificationCenter could result in any problems/issues I should be aware of ?

    Thanks again,

    Dave

     

     

    0
  • Dave,

    Good to hear!

    Excluding the UNUserNotificationCenterDelegate methods just means that you will not be able to utilize iOS 10 features, such as Rich Notifications, when sending a push.

    1
  • 0

Please sign in to leave a comment.