菜鸟教程小白 发表于 2022-12-11 18:40:27

swift - 为什么只有我最后一个本地通知功能被调用?


                                            <p><p>我对 swift 还很陌生,我试图在 UISwitch IBAction 中调用多个请求本地通知的函数。我想在某个日期发送通知 - 一年中的每个季度的第 4、7、10、1 个月。只有第四季度的函数被调用。我怎样才能让所有四个函数都被调用?这是我的代码:</p>

<p>//用于季度通知的 UISwitch</p>

<pre><code>@IBAction func quarterlyFrequencyTapped(_ sender: UISwitch) {

if quarterlyFrequency.isOn == true {

    firstQuarter(); secondQuarter(); thirdQuarter(); fourthQuarter()

    print(&#34;quarterly frequency is \(quarterlyFrequency.isOn)&#34;)

} else {

    removeQuarterlyNotification()

    print(&#34;quaterly frequency is \(monthlyFrequency.isOn)&#34;)

       }

}
</code></pre>

<p>//所有四个季度的函数</p>

<pre><code>func firstQuarter() {
    let firstQuarterContent = UNMutableNotificationContent()
    firstQuarterContent.title = &#34;First Quarter&#34;
    firstQuarterContent.subtitle = &#34;Some string&#34;
    firstQuarterContent.body = &#34;Some other string&#34;

    var firstQuarterDate = DateComponents()
    firstQuarterDate.month = 3
    firstQuarterDate.day = 11
    firstQuarterDate.hour = 19
    firstQuarterDate.minute = 20

    let firstQuarterTrigger = UNCalendarNotificationTrigger(dateMatching: firstQuarterDate, repeats: true)
    let firstQuarterRequestIdentifier = &#34;Quarterly&#34;
    let firstQuarterRequest = UNNotificationRequest(identifier: firstQuarterRequestIdentifier, content: firstQuarterContent, trigger: firstQuarterTrigger)
    UNUserNotificationCenter.current().add(firstQuarterRequest, withCompletionHandler: nil)
}

func secondQuarter() {
    let secondQuarterContent = UNMutableNotificationContent()
    secondQuarterContent.title = &#34;Second Quarter&#34;
    secondQuarterContent.subtitle = &#34;Some string&#34;
    secondQuarterContent.body = &#34;Some other string&#34;

    var secondQuarterDate = DateComponents()
    secondQuarterDate.month = 3
    secondQuarterDate.day = 11
    secondQuarterDate.hour = 19
    secondQuarterDate.minute = 21

    let secondQuarterTrigger = UNCalendarNotificationTrigger(dateMatching: secondQuarterDate, repeats: true)
    let secondQuarterRequestIdentifier = &#34;Quarterly&#34;
    let secondQuarterRequest = UNNotificationRequest(identifier: secondQuarterRequestIdentifier, content: secondQuarterContent, trigger: secondQuarterTrigger)
    UNUserNotificationCenter.current().add(secondQuarterRequest, withCompletionHandler: nil)
}

func thirdQuarter() {
    let thirdQuarterContent = UNMutableNotificationContent()
    thirdQuarterContent.title = &#34;Third Quarter&#34;
    thirdQuarterContent.subtitle = &#34;Some string&#34;
    thirdQuarterContent.body = &#34;Some other string&#34;

    var thirdQuarterDate = DateComponents()
    thirdQuarterDate.month = 3
    thirdQuarterDate.day = 11
    thirdQuarterDate.hour = 19
    thirdQuarterDate.minute = 22

    let thirdQuarterTrigger = UNCalendarNotificationTrigger(dateMatching: thirdQuarterDate, repeats: true)
    let thirdQuarterRequestIdentifier = &#34;Quarterly&#34;
    let thirdQuarterRequest = UNNotificationRequest(identifier: thirdQuarterRequestIdentifier, content: thirdQuarterContent, trigger: thirdQuarterTrigger)
    UNUserNotificationCenter.current().add(thirdQuarterRequest, withCompletionHandler: nil)
}

func fourthQuarter() {
    let fourthQuarterContent = UNMutableNotificationContent()
    fourthQuarterContent.title = &#34;Fourth Quarter&#34;
    fourthQuarterContent.subtitle = &#34;Some string&#34;
    fourthQuarterContent.body = &#34;Some other string&#34;

    var fourthQuarterDate = DateComponents()
    fourthQuarterDate.month = 3
    fourthQuarterDate.day = 11
    fourthQuarterDate.hour = 19
    fourthQuarterDate.minute = 23

    let fourthQuarterTrigger = UNCalendarNotificationTrigger(dateMatching: fourthQuarterDate, repeats: true)
    //let trigger = UNTimeIntervalNotificationTrigger(timeInterval: 60, repeats: true)
    let fourthQuarterRequestIdentifier = &#34;Quarterly&#34;
    let fourthQuarterRequest = UNNotificationRequest(identifier: fourthQuarterRequestIdentifier, content: fourthQuarterContent, trigger: fourthQuarterTrigger)
    UNUserNotificationCenter.current().add(fourthQuarterRequest, withCompletionHandler: nil)
}
</code></pre></p>
                                    <br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
                                            <p><p>我认为您使用的是 +iOS10 并且您正在使用 UserNotifications 框架,对吗?</p>

<p>很可能您的 <code>identifier</code> 具有 <strong>相同</strong>,并且每个都是 <strong>UPDATING</strong> 之前的通知。</p>

<p><code>identifier</code> 存在的原因是为了帮助您更新具有相同标识符的先前通知。例如,您发送通知将分数从 <code>0-0</code> 更新为 <code>0-1</code>。而不是在屏幕上显示 2 个通知,您现在只有一个。 </p>

<p>您的解决方法是为每个标识符使用 <em>不同</em> 标识符。</p>

<p>欲了解更多信息,请参阅 <a href="https://developer.apple.com/videos/play/wwdc2016/707/?time=1120" rel="noreferrer noopener nofollow">moment</a>用于 WWDC 视频</p>

<p><strong>修改后更新:</strong>
正如我所说...您的所有通知都以 <code>"Quarterly"</code> 作为其标识符。给他们一个单独的名字。</p></p>
                                   
                                                <p style="font-size: 20px;">关于swift - 为什么只有我最后一个本地通知功能被调用?,我们在Stack Overflow上找到一个类似的问题:
                                                        <a href="https://stackoverflow.com/questions/46204538/" rel="noreferrer noopener nofollow" style="color: red;">
                                                                https://stackoverflow.com/questions/46204538/
                                                        </a>
                                                </p>
                                       
页: [1]
查看完整版本: swift - 为什么只有我最后一个本地通知功能被调用?