【发布时间】:2017-12-01 23:49:02
【问题描述】:
实际上我在我的应用程序中尝试做的是,我正在为通知设置警报,然后我强制终止应用程序。现在当通知到来并且我正在点击它时,它只是打开应用程序而不是我要打开的特定控制器(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强制将视图控制器加载到内存中,然后再尝试显示视图控制器。 -
张,你能帮我在哪里加载 HoroscopeNotificationViewController。它在 Appdelegate 中吗?如果是,那么在哪里加载以及如何加载,如果你能帮助我详细说明代码,那么我会非常有义务。
-
您不是创建该应用的人吗?你应该知道它在哪里。按 Cmd-Shift-O 调出搜索字段,然后输入控制器的名称。
-
不,先生,我的查询是在 Appdelegate 中加载控制器的位置
-
@SirsenduDas 如果此解决方案适合您,请接受答案!
标签: ios iphone swift push-notification swift2.3