菜鸟教程小白 发表于 2022-12-13 03:04:11

ios - 如何在 UNNotificationRequest 中设置重复间隔和触发日期


                                            <p><p>以前我在我的应用程序中使用 <code>UILocalNotification</code> 进行提醒。</p>

<p>但如上 API 在 iOS 10.0 中已弃用,现在需要使用 UserNotifications Framework 的 <code>UNNotificationRequest</code>。</p>

<p>使用 <code>UILocalNotification</code> 我能够设置触发日期以及重复间隔,如下所示:</p>

<pre><code>UILocalNotification *localNotification = [ init];

    NSDate *tomorrow = ];
    localNotification.fireDate = tomorrow;


    if(repeatUnit!=0){
          localNotification.repeatInterval = NSWeekCalendarUnit;
    }

    [ scheduleLocalNotification:localNotification];
</code></pre>

<p>在上面的代码中,我可以设置明天的通知将触发的日期,并且我设置了 1 周的重复间隔,这将从触发日期开始。</p>

<p>如何使用 UserNotifications Framework 的 <code>UNNotificationRequest</code> 来实现。</p>

<p>因为在新的 api 中我们可以使用触发器 <code>UNCalendarNotificationTrigger</code> 和 <code>UNTimeintervalNotificationTrigger</code> 来启动 <code>UNNotificationRequest</code>。</p>

<p>有什么方法可以像 <code>UILocalNotification</code> 那样设置吗?</p></p>
                                    <br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
                                            <p><p>您仍然可以使用触发日期和重复间隔,但它不像以前那么明显了。初始化通知触发器时,将其重复参数设置为 <code>YES</code>。重复间隔由您通过 <code>dateMatching</code> 参数传递给触发器的日期组件定义:</p>

<p><strong>每天重复:</strong><br/>
只需传递日期组件 <strong>hour</strong>、<strong>minute</strong>、<strong>second</strong><br/>
⟶ 触发器每天都会在那个时候触发</p>

<p><strong>每周重复:</strong><br/>
同时传递所需的工作日组件:<strong>weekday</strong>、<strong>hour</strong>、<strong>minute</strong>、<strong>second</strong><br/>
⟶ 触发器将在每周的那个工作日触发</p>

<p>这是一个示例,它在明天的同一时间触发通知并每周重复一次:</p>

<p><strong> objective-C :</strong> </p>

<pre><code>UNMutableNotificationContent *notification = [ init];

// find out what weekday is tomorrow
NSDate *sameTimeTomorrow = ;
NSInteger weekdayTomorrow = [ component:NSCalendarUnitWeekday fromDate: sameTimeTomorrow];

// set the current time (without weekday)
unsigned unitFlags = NSCalendarUnitHour | NSCalendarUnitMinute |NSCalendarUnitSecond;
NSDateComponents *firstFireDateComponents = [ components:unitFlags fromDate:sameTimeTomorrow];

// add tomorrow&#39;s weekday
firstFireDateComponents.weekday = weekdayTomorrow;

// set a repeating trigger for the current time and tomorrow&#39;s weekday
// (trigger repeats every week on that weekday at the current time)
UNCalendarNotificationTrigger *notificationTrigger = ;

UNNotificationRequest *request = ;
[ addNotificationRequest:request withCompletionHandler: nil];
</code></pre>

<p><strong> swift :</strong> </p>

<pre><code>let notification = UNMutableNotificationContent()

// find out what weekday is tomorrow
let weekDayTomorrow = Calendar.current.component(.weekday, from: Date(timeIntervalSinceNow: 60*60*24))

// set the current time (without weekday)
var firstFireDateComponents = Calendar.current.dateComponents([.hour, .minute, .second], from: Date())

// add tomorrow&#39;s weekday
firstFireDateComponents.weekday = weekDayTomorrow

// set a repeating trigger for the current time and tomorrow&#39;s weekday
// (trigger repeats every week on that weekday at the current time)
let notificationTrigger = UNCalendarNotificationTrigger(dateMatching: firstFireDateComponents, repeats: true)

let request = UNNotificationRequest(identifier: &#34;notification&#34;, content: notification, trigger: notificationTrigger)
UNUserNotificationCenter.current().add(request, withCompletionHandler: nil)
</code></pre></p>
                                   
                                                <p style="font-size: 20px;">关于ios - 如何在 UNNotificationRequest 中设置重复间隔和触发日期,我们在Stack Overflow上找到一个类似的问题:
                                                        <a href="https://stackoverflow.com/questions/47135461/" rel="noreferrer noopener nofollow" style="color: red;">
                                                                https://stackoverflow.com/questions/47135461/
                                                        </a>
                                                </p>
                                       
页: [1]
查看完整版本: ios - 如何在 UNNotificationRequest 中设置重复间隔和触发日期