实际上我想在我的应用程序中做的是,我设置了一个通知闹钟,然后强制终止了应用程序。当通知到来并且我点击通知时,它只是打开应用程序而不是我想要打开的特定控制器(HoroscopeNotificationViewController)。而在设置闹钟后,如果应用程序在后台运行,则在点击通知时,特定的控制器会正常打开和工作。
非常感谢您的帮助。提前致谢。
非常感谢您的帮助。提前致谢。
func application(application: UIApplication,didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
if let _:UILocalNotification = launchOptions?[UIApplicationLaunchOptionsLocalNotificationKey] as? UILocalNotification {
NSTimer.scheduledTimerWithTimeInterval(3.0, target: self, selector: #selector(AppDelegate.showNotification), userInfo: nil, repeats: false)
let firstCategory:UIMutableUserNotificationCategory = UIMutableUserNotificationCategory()
firstCategory.identifier = "FIRST_CATEGORY"
}
func application(application: UIApplication, didReceiveRemoteNotification userInfo: [NSObject : AnyObject]) {
print("MessagesID : \(userInfo["gcm_message_id"]!)")
print(userInfo)
}
func application(application: UIApplication,
handleActionWithIdentifier identifier:String?,
forLocalNotification notification:UILocalNotification,
completionHandler: (() -> Void))
{
if (identifier == "FIRST_ACTION")
{
NSNotificationCenter.defaultCenter().postNotificationName("actionOnePressed", object: nil)
}
else if (identifier == "SECOND_ACTION")
{
NSNotificationCenter.defaultCenter().postNotificationName("actionTwoPressed", object: nil)
}
completionHandler()
}
func application(application: UIApplication, didReceiveRemoteNotification userInfo: [NSObject : AnyObject], fetchCompletionHandler completionHandler: (UIBackgroundFetchResult) -> Void) {
print("MessageId: \(userInfo["gcm_message_id"])")
print(userInfo)
}
func application(application: UIApplication, didReceiveLocalNotification notification: UILocalNotification) {
if #available(iOS 8.2, *) {
print("Function goes here : \(notification.alertTitle)")
} else {
// Fallback on earlier versions
}
if notification.category == "FIRST_CATEGORY" {
sleep(8)
let horoscopeNotificationObj = window?.rootViewController?.storyboard!.instantiateViewControllerWithIdentifier("HoroscopeNotificationViewController") as! HoroscopeNotificationViewController
window?.rootViewController?.presentViewController(horoscopeNotificationObj, animated: true, completion: nil)
}
}
func showNotification() {
NSNotificationCenter.defaultCenter().postNotificationName("modifyListNotification", object: nil)
}
myController.view
以强制将视图控制器加载到内存中。 - Zhang