我在应用程序上处理iOS的本地和远程通知,但我遇到了问题。当应用程序被终止并且通知到达远程或本地时我点击通知然后调用了学习的didFinishLaunchingWithOptions并且应用程序打开。但是,如果我选择自定义操作而不是单击通知,我的应用程序崩溃。
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
setActions()
if #available(iOS 10.0, *) {
UNUserNotificationCenter.current().requestAuthorization(options: [.alert,.badge,.sound], completionHandler: {(granted,error) in
if granted{
print("Notification access granted")
}
else
{
print(error?.localizedDescription as Any)
}
})
}
else {
// Fallback on earlier versions
}
if #available(iOS 10.0, *) {
UNUserNotificationCenter.current().delegate = self
} else {
// Fallback on earlier versions
}
registerForPushNotifications(application: application)
if let notification = launchOptions?[UIApplicationLaunchOptionsKey.remoteNotification] as? [String: AnyObject]
{
self.application(application, didReceiveRemoteNotification: notification)
}
return true
}
func application(_ application: UIApplication, didReceiveRemoteNotification data: [AnyHashable : Any]) {
let aps = data["aps"] as! [String:AnyObject]
let alert = aps["alert"] as! [String:AnyObject]
let title = alert["title"] as! String
let deadlineTime = DispatchTime.now() + .milliseconds(10)
DispatchQueue.main.asyncAfter(deadline: deadlineTime) {
let navigationController = self.window?.rootViewController as! UINavigationController
let activeViewCont = navigationController.viewControllers
let viewController = activeViewCont[0] as? ViewController
viewController?.button.setTitle(title, for: .normal)
}
print("Push notification received: \(data)")
}
func setActions(){
if #available(iOS 10.0, *) {
let like = UNNotificationAction(identifier: "like", title: "Like", options: .foreground)
let dismiss = UNNotificationAction(identifier: "dismiss", title: "Dismiss", options: .destructive)
//Action for CHAT REPLY
let reply = UNTextInputNotificationAction(identifier: "reply", title: "Reply", options: .foreground, textInputButtonTitle: "Send", textInputPlaceholder: "Enter Your Message")
let categoryOne = UNNotificationCategory(identifier: "customNotification", actions: [like,dismiss,reply], intentIdentifiers: [], options: [])
let categoryTwo = UNNotificationCategory(identifier: "localNotification", actions: [like,dismiss], intentIdentifiers: [], options: [])
UNUserNotificationCenter.current().setNotificationCategories([categoryOne,categoryTwo])
}
else
{
let like = UIMutableUserNotificationAction()
like.activationMode = .foreground
like.identifier = "like"
like.title = "Like"
let dismiss = UIMutableUserNotificationAction()
dismiss.activationMode = .background
dismiss.identifier = "dismiss"
dismiss.title = "Dismiss"
let reply = UIMutableUserNotificationAction()
reply.activationMode = .background
reply.identifier = "reply"
reply.title = "Reply"
if #available(iOS 9.0, *) {
reply.behavior = .textInput
} else {
// Fallback on earlier versions
}
let categoryOne = UIMutableUserNotificationCategory()
categoryOne.identifier = "customNotification"
categoryOne.setActions([dismiss,like,reply], for: .default)
let categoryTwo = UIMutableUserNotificationCategory()
categoryTwo.identifier = "localNotification"
categoryTwo.setActions([dismiss], for: .default)
let notificationSettings = UIUserNotificationSettings(types: [.badge, .sound, .alert], categories: [categoryOne,categoryTwo])
UIApplication.shared.registerUserNotificationSettings(notificationSettings)
}
}
答案 0 :(得分:-2)
在调度队列中使用此代码。
let mainStoryboard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
var homeViewController = mainStoryboard.instantiateViewControllerWithIdentifier("HomeViewController") as! HomeViewController
let nav = UINavigationController(rootViewController: homeViewController)
appdelegate.window!.rootViewController = nav