如何设置首次设置通知后每秒触发的本地通知?

问题描述 投票:0回答:1

我想要的是,我将当地通知设置为早上7点,并立即点火,通知会持续发出,直到用户不对通知执行任何操作或打开应用程序。

以下是第一次发送通知的代码

 let alarmNotification: UNMutableNotificationContent = UNMutableNotificationContent()
 alarmNotification.title = "Demo"
 alarmNotification.body = "Test"
 alarmNotification.categoryIdentifier = "myDemoCategory"

 let now = Date()
 let triggerWeekly = Calendar.current.dateComponents([.weekday,.hour,.minute], from: now)
 let trigger = UNCalendarNotificationTrigger(dateMatching: triggerWeekly, repeats: true)
 let request = UNNotificationRequest(identifier: "TestNotification\(now)", content: alarmNotification, trigger: trigger)

 UNUserNotificationCenter.current().add(request) {(error) in
     if let error = error {         
           print("Uh oh! We had an error: \(error)")
     }
 }

请建议连续发送本地通知。

谢谢

ios swift swift3 localnotification unusernotificationcenter
1个回答
0
投票

在您的日期组件中添加秒数

let triggerWeekly = Calendar.current.dateComponents([.weekday,.hour,.minute], from: now)

通过递增秒来设置循环中每秒所需的通知数量。在iOS中,最大值为64。

点击通知时会关闭所有通知并重置以供下次使用。

© www.soinside.com 2019 - 2024. All rights reserved.