【问题标题】:Custom methods for push notifications using UrbanAirship iOS SDK使用 UrbanAirship iOS SDK 的自定义推送通知方法
【发布时间】:2017-11-09 23:47:32
【问题描述】:

我正在开发一个 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 方面为我指明正确的方向,我将不胜感激。

谢谢!

【问题讨论】:

  • 通知负载中的“类型”在哪里?似乎这将返回 nil: notification["type"]
  • 我可以修改通知以适应 SDK 的需要,但我觉得这个问题有点深。如果我消除所有自定义代码并从 tue 网站向所有 ios 设备发送通知,我会在设备上收到通知。到目前为止一切都很好,但是,除了网站提供的几个选项之外,我还无法弄清楚我如何与通知中显示的按钮进行交互。因此,如果有人可以提供一个简单的示例,可以与接受/拒绝按钮交互并为这些按钮运行自定义方法;这也行。

标签: ios iphone swift push-notification urbanairship.com


【解决方案1】:

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

【讨论】:

  • 谢谢;这是否意味着我应该完全删除 receivedForegroundNotification 功能,还是我可以把它留在那里?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-01-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-09-18
相关资源
最近更新 更多