ios - 如何在应用程序终止或终止时点击推送通知后打开特定 Controller
<p><p>实际上我在我的应用程序中尝试做的是,我正在为通知设置警报,然后我强制终止应用程序。现在当通知到来并且我正在点击它时,它只是打开应用程序而不是我要打开的特定 Controller (HoroscopeNotificationViewController),而在设置警报后如果应用程序在后台运行,然后点击通知特定 Controller 的打开并正常工作。</p>
<p>您的帮助将不胜感激..提前致谢</p>
<pre><code>func application(application: UIApplication,didFinishLaunchingWithOptions launchOptions: ?) -> Bool {
if let _:UILocalNotification = launchOptions? 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: ) {
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: , 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)
}
</code></pre></p>
<br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
<p><p>要在终止状态或终止状态下点击通知打开特定的 ViewController ,您需要在应用程序委托(delegate)方法 didFinishLaunchingWithOptions 参数中检查启动选项对象的存在。请引用:</p>
<pre><code>func application(application: UIApplication,didFinishLaunchingWithOptions launchOptions: ?) -> Bool {
if let localNotification: UILocalNotification = launchOptions? as? UILocalNotification {
//launchOptions is not nil
//if this will not work then call this below method when application root view controller is ready to handle child view transition or just do a sec delay
self.application(application, didReceiveLocalNotification: localNotification)
}
</code></pre></p>
<p style="font-size: 20px;">关于ios - 如何在应用程序终止或终止时点击推送通知后打开特定 Controller ,我们在Stack Overflow上找到一个类似的问题:
<a href="https://stackoverflow.com/questions/44805438/" rel="noreferrer noopener nofollow" style="color: red;">
https://stackoverflow.com/questions/44805438/
</a>
</p>
页:
[1]