菜鸟教程小白 发表于 2022-12-11 19:59:21

ios - 如何在应用程序终止或终止时点击推送通知后打开特定 Controller


                                            <p><p>实际上我在我的应用程序中尝试做的是,我正在为通知设置警报,然后我强制终止应用程序。现在当通知到来并且我正在点击它时,它只是打开应用程序而不是我要打开的特定 Controller (HoroscopeNotificationViewController),而在设置警报后如果应用程序在后台运行,然后点击通知特定 Controller 的打开并正常工作。</p>

<p>您的帮助将不胜感激..提前致谢</p>

<pre><code>func application(application: UIApplication,didFinishLaunchingWithOptions launchOptions: ?) -&gt; 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 = &#34;FIRST_CATEGORY&#34;
}

func application(application: UIApplication, didReceiveRemoteNotification userInfo: ) {
    print(&#34;MessagesID : \(userInfo[&#34;gcm_message_id&#34;]!)&#34;)
    print(userInfo)
}

func application(application: UIApplication,
               handleActionWithIdentifier identifier:String?,
                                          forLocalNotification notification:UILocalNotification,
                                                               completionHandler: (() -&gt; Void))
{

    if (identifier == &#34;FIRST_ACTION&#34;)
    {
      NSNotificationCenter.defaultCenter().postNotificationName(&#34;actionOnePressed&#34;, object: nil)
    }
    else if (identifier == &#34;SECOND_ACTION&#34;)
    {
      NSNotificationCenter.defaultCenter().postNotificationName(&#34;actionTwoPressed&#34;, object: nil)
    }

    completionHandler()
}

func application(application: UIApplication, didReceiveRemoteNotification userInfo: , fetchCompletionHandler completionHandler: (UIBackgroundFetchResult) -&gt; Void) {
    print(&#34;MessageId: \(userInfo[&#34;gcm_message_id&#34;])&#34;)
    print(userInfo)
}

func application(application: UIApplication, didReceiveLocalNotification notification: UILocalNotification) {
    if #available(iOS 8.2, *) {
      print(&#34;Function goes here : \(notification.alertTitle)&#34;)
    } else {
      // Fallback on earlier versions
    }
    if notification.category == &#34;FIRST_CATEGORY&#34; {
      sleep(8)
      let horoscopeNotificationObj = window?.rootViewController?.storyboard!.instantiateViewControllerWithIdentifier(&#34;HoroscopeNotificationViewController&#34;) as! HoroscopeNotificationViewController
      window?.rootViewController?.presentViewController(horoscopeNotificationObj, animated: true, completion: nil)
      }
    }

func showNotification() {

    NSNotificationCenter.defaultCenter().postNotificationName(&#34;modifyListNotification&#34;, 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: ?) -&gt; 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]
查看完整版本: ios - 如何在应用程序终止或终止时点击推送通知后打开特定 Controller