ios - 使用 UrbanAirship iOS SDK 推送通知的自定义方法

标签 ios iphone swift push-notification urbanairship.com

我正在开发一个 iOS 应用程序,该应用程序从用 Java 编写的自定义 API 接收推送通知,该通知应提示用户接受或拒绝加入表的请愿书。我正在研究不久前编写的现有代码库并更新软件。

问题是,UrbanAirship 似乎已经从根本上改变了他们的行为,我无法再显示我们的自定义通知框。

我们的 API 发送的通知的结构如下:

[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;
}

我们在 AppDelegate 中实现了以下方法:UAPushNotificationDelegate(为了简洁起见,我仅包含与 UAirship 相关的代码:

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) {}

如果有人能伸出援手,至少为我指明更新代码以正确使用新版本 Urban Airship SDK 和 iOS 11 的正确方向,我将不胜感激。

谢谢!

最佳答案

您需要覆盖 receivedNotificationResponse而不是前台通知。然后,您将可以访问通知、用户响应和按钮操作 ID。

关于ios - 使用 UrbanAirship iOS SDK 推送通知的自定义方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47213544/

相关文章:

iphone - 使用新证书更新应用程序

ios - 在 Ipad 上使用 UITableview Longpress 进行操作表操作的最佳方式是什么

ios - 如果不在 ViewController 中,则估计不会调用 didChangeAuthorizationStatus

ios - CAGradientLayer 超出 UILabel 的框架

ios - 泄漏仪(iOS)发现的内存泄漏

iphone - Xcode ProcessPCH iPhone模拟器编译报错

ios - NSKeyedUnarchiver.unarchiveObject 在 Today 小部件和主机应用程序之间从 UserDefaults 中选取数据时失败并出现错误

ios - 应用因使用Google Maps SDK被Apple拒绝

ios - 如果用户接听电话,请停止播放音频

iphone - 在我的代码的其余部分中访问输出的 'NSInteger'