Updating code to handle custom code for notifications

I'm working on an iOS app which receives a push notification from a custom API written in Java which should prompt the user to either acccept or reject a petition to join a table. I'm working on an existing code-base written a while back and updating the software.

The problem is, that it seems that UrbanAirship has radically modified their behaviors and I'm no longer able to present our custom notification boxes.

The notifications being sent by our API are structed as follows:

[Line 227] Received notification: {
"_" = "4f5a2ac1-f337-4181-9c88-70b65d4f501f";
aps = {
alert = "Tu mesa ya est\U00e1 abierta";
};
command = 1;
mode = createCommand;
posType = 2;
restaurant = "Zadia Restaurant";
restaurantId = 1;
}

And we have the following methods implemented in our AppDelegate : UAPushNotificationDelegate (I'm only including the code relevant to UAirship for the sake of brevety:

func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
if !application.isRegisteredForRemoteNotifications() { //En caso de que no tengamos permisos para push notifications
showWarningDialog()
} else {
UAirship.takeOff()
UAirship.push().userNotificationTypes = ([UIUserNotificationType.Alert, UIUserNotificationType.Badge, UIUserNotificationType.Sound])
UAirship.push().userPushNotificationsEnabled = true
UAirship.push().pushNotificationDelegate = UIApplication.sharedApplication().delegate as! AppDelegate
}
}

func receivedForegroundNotification(notification: [NSObject : AnyObject], fetchCompletionHandler completionHandler: ((UIBackgroundFetchResult) -> Void)) {

if OurApp.sharedInstance.isCommandTopViewController && notification["type"] != nil && (notification["type"] as! String) == "updateCommand" {
OurApp.sharedInstance.commandController?.refresh()
}

if notification["type"] != nil {
if (notification["type"] as! String).contains("joinRequest") {
showJoinRequestDialog(notification["aps"]!["alert"] as! String, userId: notification["userId"] as! Int)
} else if (notification["type"] as! String).contains("joinResponse") {
let state = notification["state"] as! Int
if state == 1 {
self.preferenceManager.saveCurrentTab(notification["command"] as? String)
self.preferenceManager.saveCurrentRestaurant(notification["restaurant"] as? String)
}
showJoinResponseDialog(notification["aps"]!["alert"] as! String, state: state)
} else if (notification["type"] as! String).contains("promotion") {
showNewPromotionDialog(notification["aps"]!["alert"] as! String, promotion: notification["promotionId"] as! Int)
}
}

// Call the completion handler
completionHandler(UIBackgroundFetchResult.NoData)
}

func launchedFromNotification(notification: [NSObject : AnyObject], fetchCompletionHandler completionHandler: ((UIBackgroundFetchResult) -> Void)) {
if notification["type"] != nil {
if (notification["type"] as! String).contains("joinRequest") {
showJoinRequestDialog(notification["aps"]!["alert"] as! String, userId: notification["userId"] as! Int)
} else if (notification["type"] as! String).contains("joinResponse") {
let state = notification["state"] as! Int
if state == 1 {
self.preferenceManager.saveCurrentTab(notification["command"] as? String)
self.preferenceManager.saveCurrentRestaurant(notification["restaurant"] as? String)
}
showJoinResponseDialog(notification["aps"]!["alert"] as! String, state: state)
}
}

// Call the completion handler
completionHandler(UIBackgroundFetchResult.NoData)
}
func showJoinRequestDialog(message: String, userId: Int) {
let dialog: UIAlertController = UIAlertController(title: NSLocalizedString("Atención", comment:""), message: message + NSLocalizedString("¿Quieres aceptarlo?", comment:""), preferredStyle: .Alert)
dialog.addAction(UIAlertAction(title: NSLocalizedString("Aceptar", comment:""), style: .Default, handler: { action in
let state = 1
self.sendAcceptRequest(userId, state: state)
dialog.dismissViewControllerAnimated(true, completion: nil)
}))
dialog.addAction(UIAlertAction(title: NSLocalizedString("Rechazar", comment:""), style: .Cancel, handler: { action in
let state = 0
self.sendAcceptRequest(userId, state: state)
dialog.dismissViewControllerAnimated(true, completion: nil)
}))
self.window!.rootViewController?.presentViewController(dialog, animated: true, completion: nil)
}

func showJoinResponseDialog(message: String, state: Int) {
let dialog: UIAlertController = UIAlertController(title: NSLocalizedString("Atención", comment:""), message: message, preferredStyle: .Alert)
dialog.addAction(UIAlertAction(title: NSLocalizedString("Aceptar", comment:""), style: .Default, handler: { action in
if(state == 1) {
let mainViewController = OurApp.sharedInstance.mainController
mainViewController.navigateOnSideMenu("command")
}
dialog.dismissViewControllerAnimated(true, completion: nil)
}))
self.window!.rootViewController?.presentViewController(dialog, animated: true, completion: nil)
}

func showNewPromotionDialog(message: String, promotion: Int) {
let dialog: UIAlertController = UIAlertController(title: NSLocalizedString("Atención", comment:""), message: message, preferredStyle: .Alert)
dialog.addAction(UIAlertAction(title: NSLocalizedString("Aceptar", comment:""), style: .Default, handler: { action in
let mainViewController = OurApp.sharedInstance.mainController
mainViewController.navigateOnSideMenu("promotions")
dialog.dismissViewControllerAnimated(true, completion: nil)
}))
self.window!.rootViewController?.presentViewController(dialog, animated: true, completion: nil)
}

func sendAcceptRequest(userId: Int, state: Int) {}


If anyone could lend a helping hand to, at least, point me in the right direction in regards to updating the code to work correctly with the new versions of the Urban Airship SDK and iOS 11, I'd greatly appreciate it.

Thank you!

Didn't find what you were looking for?

New post

Comments

3 comments

  • Hi Akseli,

    Generally if you're trying to handle the App Delegate methods yourself, you'll want to take a look at our Automatic Integration documentation.

    The Urban Airship SDK automatically intercepts those methods, so if you're not seeing your App Delegate methods running, you'll likely need to disable automatic integration, and forward those calls to Urban Airship, so that you can continue running your code to handle and show the notification to the user.

    0
  • Thank you.

    I just finished implementing the code and have successfuly managed to handle foreground notifications.

    0
  • Glad to hear it!

    0

Please sign in to leave a comment.