我在應用程序上處理iOS的本地和遠程通知,並且即時通訊面臨一個問題。當應用程序被終止並且通知到達遠程或本地時,我點擊通知,然後調用didFinishLaunchingWithOptions被調用並打開應用程序。但是,如果我選擇自定義操作而不是點擊通知,我的應用程序會崩潰。處理應用程序死亡時的通知操作iOS 10
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)
}
}
請顯示'setActions'。 – shallowThought
我已經通過添加setActions更新了問題 –
我在這裏回答了同樣的問題: http://stackoverflow.com/a/43815251/3934111 –