菜鸟教程小白 发表于 2022-12-11 19:17:44

ios - 如何关闭通知内容扩展?


                                            <p><p>我想实现与 IOS 原始时钟应用程序相同的功能。但我遇到了一个障碍:当贪睡剩余时间设置为零时,我无法以编程方式关闭通知内容扩展。
图片如下:
<a href="/image/5xoPl.png" rel="noreferrer noopener nofollow"><img src="/image/5xoPl.png" alt="enter image description here"/></a> </p>

<p>我的 NotificationViewController.swift:</p>

<pre><code>import UIKit
import UserNotifications
import UserNotificationsUI

class NotificationViewController: UIViewController, UNNotificationContentExtension {

    @IBOutlet var label: UILabel?

    var previousNotification: UNNotification?
    var secondsDown = Int()
    var countDownTimer: Timer?

    override func viewDidLoad() {
      super.viewDidLoad()

      let size = view.bounds.size
      preferredContentSize = CGSize(width: size.width, height: size.width * 0.7)
    }

    func didReceive(_ notification: UNNotification) {
      previousNotification = notification

      // Calculate the actual remaining seconds.
      let timeInterval = -(notification.date.timeIntervalSinceNow)
      secondsDown = (notification.request.content.userInfo[&#34;snoozeTime&#34;] as! Int) * 60 - Int(timeInterval) - 1

      // Achieve count down timer.
      if countDownTimer == nil {
            countDownTimer = Timer(timeInterval: 1, target: self, selector: #selector(self.countDownAction), userInfo: nil, repeats: true)
            RunLoop.main.add(countDownTimer!, forMode: .defaultRunLoopMode)
      }

      // print the remaining time to text label.
      let miniute = String(format:&#34;%ld&#34;, (secondsDown / 60))
      let second = String(format:&#34;%02ld&#34;, (secondsDown % 60))
      let formatTime = String(format: &#34;%@:%@&#34;, miniute, second)
      self.label?.text = String(format: &#34;%@&#34;, formatTime)
    }

    func countDownAction(timer: Timer) {
      secondsDown -= 1

      let miniute = String(format:&#34;%ld&#34;, (secondsDown / 60))
      let second = String(format:&#34;%02ld&#34;, (secondsDown % 60))
      let formatTime = String(format: &#34;%@:%@&#34;, miniute, second)
      self.label?.text = String(format: &#34;%@&#34;, formatTime)

      if secondsDown == 0 {
            countDownTimer?.invalidate()
      }
    }
}
</code></pre></p>
                                    <br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
                                            <p><p>似乎从 iOS 12 开始有解决这个问题的方法:</p>

<pre><code>   if #available(iOSApplicationExtension 12.0, *) {
                  //self.extensionContext?.performNotificationDefaultAction()
                  self.extensionContext?.dismissNotificationContentExtension()
    }
</code></pre></p>
                                   
                                                <p style="font-size: 20px;">关于ios - 如何关闭通知内容扩展?,我们在Stack Overflow上找到一个类似的问题:
                                                        <a href="https://stackoverflow.com/questions/42874245/" rel="noreferrer noopener nofollow" style="color: red;">
                                                                https://stackoverflow.com/questions/42874245/
                                                        </a>
                                                </p>
                                       
页: [1]
查看完整版本: ios - 如何关闭通知内容扩展?