我正在开发一个 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/