实际上,我在我的应用程序中尝试做的是,我为通知设置一个警报,然后我强制终止应用程序。现在,当通知到来时,我点击它,它只会打开应用程序,而不是我想要打开的特定控制器(HoroscopeNotificationViewController),而在设置警报后,如果应用程序在后台运行,然后在轻敲通知时,特定控制器打开并工作良好。
你的帮助将非常感激1.提前感谢
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)
}
4条答案
按热度按时间k4aesqcs1#
要打开一个特定的视图控制器在点击通知时处于终止状态或已删除状态,你需要检查应用委托方法didFinishLaunchingWithOptions参数中是否存在启动选项对象。请参考:
ttisahbt2#
感谢大家为解决我的查询所做的努力。不要使用“NSTimer.scheduledTimerWithTimeInterval(3. 0,目标:自我,选择器:#选择器(应用程序委派.showNotification),使用者信息:无,重复:如果您有任何问题,请使用以下方法解决:nil)}”我只是简单地使用instantiateViewControllerWithIdentifier将控制器从didFinishLaunchingWithOptions推送到指定的控制器,它对我很有效。再次感谢@Sachin和@Zhang
lc8prwob3#
**
SWIFT 4
**
92dk7w1h4#