- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在创建一个应用程序,该应用程序会在用户通过应用程序界面指定的时间发送每日本地通知。
在测试期间,我安排了 20 条每日本地通知,覆盖 20 天。在最后一天,用户可以点击按钮来安排另外 20 个事件。
当我运行应用程序时,它仅发送第一个通知。其余的通知永远不会发送。如何修复我的代码以使时间增加一天?
// Function that schedules the 20 notifications in advance so each one is delivered every day
func scheduleNotif() {
// Notification #1
let content = UNMutableNotificationContent()
content.title = "Good Morning \(name)!"
content.body = sendMessage()
content.sound = UNNotificationSound.default()
// Time in which notification will fire
let dailyTime = Calendar.current.dateComponents([.hour, .minute,], from: timePick.date)
let trigger = UNCalendarNotificationTrigger(dateMatching: dailyTime, repeats: false)
// The identifier for the notification so that it is distinguishable from others in code
let identifier = "Positive Message"
// Requesting for notification to be delivered
let request = UNNotificationRequest(identifier: identifier, content: content, trigger: trigger)
let center = UNUserNotificationCenter.current()
center.add(request) { (error : Error?) in
if let theError = error {
print(theError.localizedDescription)
}
}
// Notification #2
let content1 = UNMutableNotificationContent()
content1.title = "Good Morning \(name)!"
content1.body = sendMessage()
content1.sound = UNNotificationSound.default()
var dailyTime1 = Calendar.current.dateComponents([.hour, .minute,], from: timePick.date)
dailyTime1.hour! += 24
let trigger1 = UNCalendarNotificationTrigger(dateMatching: dailyTime1, repeats: false)
let identifier1 = "Positive Message1"
let request1 = UNNotificationRequest(identifier: identifier1, content: content1, trigger: trigger1)
center.add(request1) { (error : Error?) in
if let theError = error {
print(theError.localizedDescription)
}
}
// Notification #3
let content2 = UNMutableNotificationContent()
content2.title = "Good Morning \(name)!"
content2.body = sendMessage()
content2.sound = UNNotificationSound.default()
var dailyTime2 = Calendar.current.dateComponents([.hour, .minute,], from: timePick.date)
dailyTime2.hour! += 48
let trigger2 = UNCalendarNotificationTrigger(dateMatching: dailyTime2, repeats: false)
let identifier2 = "Positive Message2"
let request2 = UNNotificationRequest(identifier: identifier2, content: content2, trigger: trigger2)
center.add(request2) { (error : Error?) in
if let theError = error {
print(theError.localizedDescription)
}
}
// Notification #4
let content3 = UNMutableNotificationContent()
content3.title = "Good Morning \(name)!"
content3.body = sendMessage()
content3.sound = UNNotificationSound.default()
var dailyTime3 = Calendar.current.dateComponents([.hour, .minute,], from: timePick.date)
dailyTime3.hour! += 72
let trigger3 = UNCalendarNotificationTrigger(dateMatching: dailyTime3, repeats: false)
let identifier3 = "Positive Message3"
let request3 = UNNotificationRequest(identifier: identifier3, content: content3, trigger: trigger3)
center.add(request3) { (error : Error?) in
if let theError = error {
print(theError.localizedDescription)
}
}
// Notification #5
let content4 = UNMutableNotificationContent()
content4.title = "Good Morning \(name)!"
content4.body = sendMessage()
content4.sound = UNNotificationSound.default()
var dailyTime4 = Calendar.current.dateComponents([.hour, .minute,], from: timePick.date)
dailyTime4.hour! += 96
let trigger4 = UNCalendarNotificationTrigger(dateMatching: dailyTime4, repeats: false)
let identifier4 = "Positive Message4"
let request4 = UNNotificationRequest(identifier: identifier4, content: content4, trigger: trigger4)
center.add(request4) { (error : Error?) in
if let theError = error {
print(theError.localizedDescription)
}
}
// Notification #6
let content5 = UNMutableNotificationContent()
content5.title = "Good Morning \(name)!"
content5.body = sendMessage()
content5.sound = UNNotificationSound.default()
var dailyTime5 = Calendar.current.dateComponents([.hour, .minute,], from: timePick.date)
dailyTime5.hour! += 120
let trigger5 = UNCalendarNotificationTrigger(dateMatching: dailyTime5, repeats: false)
let identifier5 = "Positive Message5"
let request5 = UNNotificationRequest(identifier: identifier5, content: content5, trigger: trigger5)
center.add(request5) { (error : Error?) in
if let theError = error {
print(theError.localizedDescription)
}
}
// Notification #7
let content6 = UNMutableNotificationContent()
content6.title = "Good Morning \(name)!"
content6.body = sendMessage()
content6.sound = UNNotificationSound.default()
var dailyTime6 = Calendar.current.dateComponents([.hour, .minute,], from: timePick.date)
dailyTime6.hour! += 144
let trigger6 = UNCalendarNotificationTrigger(dateMatching: dailyTime6, repeats: false)
let identifier6 = "Positive Message6"
let request6 = UNNotificationRequest(identifier: identifier6, content: content6, trigger: trigger6)
center.add(request6) { (error : Error?) in
if let theError = error {
print(theError.localizedDescription)
}
}
// Notification #8
let content7 = UNMutableNotificationContent()
content7.title = "Good Morning \(name)!"
content7.body = sendMessage()
content7.sound = UNNotificationSound.default()
var dailyTime7 = Calendar.current.dateComponents([.hour, .minute,], from: timePick.date)
dailyTime7.hour! += 168
let trigger7 = UNCalendarNotificationTrigger(dateMatching: dailyTime7, repeats: false)
let identifier7 = "Positive Message7"
let request7 = UNNotificationRequest(identifier: identifier7, content: content7, trigger: trigger7)
center.add(request7) { (error : Error?) in
if let theError = error {
print(theError.localizedDescription)
}
}
// Notification #9
let content8 = UNMutableNotificationContent()
content8.title = "Good Morning \(name)!"
content8.body = sendMessage()
content8.sound = UNNotificationSound.default()
var dailyTime8 = Calendar.current.dateComponents([.hour, .minute,], from: timePick.date)
dailyTime8.hour! += 192
let trigger8 = UNCalendarNotificationTrigger(dateMatching: dailyTime8, repeats: false)
let identifier8 = "Positive Message8"
let request8 = UNNotificationRequest(identifier: identifier8, content: content8, trigger: trigger8)
center.add(request8) { (error : Error?) in
if let theError = error {
print(theError.localizedDescription)
}
}
// Notification #10
let content9 = UNMutableNotificationContent()
content9.title = "Good Morning \(name)!"
content9.body = sendMessage()
content9.sound = UNNotificationSound.default()
var dailyTime9 = Calendar.current.dateComponents([.hour, .minute,], from: timePick.date)
dailyTime9.hour! += 216
let trigger9 = UNCalendarNotificationTrigger(dateMatching: dailyTime9, repeats: false)
let identifier9 = "Positive Message9"
let request9 = UNNotificationRequest(identifier: identifier9, content: content9, trigger: trigger9)
center.add(request9) { (error : Error?) in
if let theError = error {
print(theError.localizedDescription)
}
}
// Notification #11
let content10 = UNMutableNotificationContent()
content10.title = "Good Morning \(name)!"
content10.body = sendMessage()
content10.sound = UNNotificationSound.default()
var dailyTime10 = Calendar.current.dateComponents([.hour, .minute,], from: timePick.date)
dailyTime10.hour! += 240
let trigger10 = UNCalendarNotificationTrigger(dateMatching: dailyTime10, repeats: false)
let identifier10 = "Positive Message10"
let request10 = UNNotificationRequest(identifier: identifier10, content: content10, trigger: trigger10)
center.add(request10) { (error : Error?) in
if let theError = error {
print(theError.localizedDescription)
}
}
// Notification #12
let content11 = UNMutableNotificationContent()
content11.title = "Good Morning \(name)!"
content11.body = sendMessage()
content11.sound = UNNotificationSound.default()
var dailyTime11 = Calendar.current.dateComponents([.hour, .minute,], from: timePick.date)
dailyTime11.hour! += 264
let trigger11 = UNCalendarNotificationTrigger(dateMatching: dailyTime11, repeats: false)
let identifier11 = "Positive Message11"
let request11 = UNNotificationRequest(identifier: identifier11, content: content11, trigger: trigger11)
center.add(request11) { (error : Error?) in
if let theError = error {
print(theError.localizedDescription)
}
}
// Notification #13
let content12 = UNMutableNotificationContent()
content12.title = "Good Morning \(name)!"
content12.body = sendMessage()
content12.sound = UNNotificationSound.default()
var dailyTime12 = Calendar.current.dateComponents([.hour, .minute,], from: timePick.date)
dailyTime12.hour! += 288
let trigger12 = UNCalendarNotificationTrigger(dateMatching: dailyTime12, repeats: false)
let identifier12 = "Positive Message12"
let request12 = UNNotificationRequest(identifier: identifier12, content: content12, trigger: trigger12)
center.add(request12) { (error : Error?) in
if let theError = error {
print(theError.localizedDescription)
}
}
// Notification #14
let content13 = UNMutableNotificationContent()
content13.title = "Good Morning \(name)!"
content13.body = sendMessage()
content13.sound = UNNotificationSound.default()
var dailyTime13 = Calendar.current.dateComponents([.hour, .minute,], from: timePick.date)
dailyTime13.hour! += 312
let trigger13 = UNCalendarNotificationTrigger(dateMatching: dailyTime13, repeats: false)
let identifier13 = "Positive Message13"
let request13 = UNNotificationRequest(identifier: identifier13, content: content13, trigger: trigger13)
center.add(request13) { (error : Error?) in
if let theError = error {
print(theError.localizedDescription)
}
}
// Notification #15
let content14 = UNMutableNotificationContent()
content14.title = "Good Morning \(name)!"
content14.body = sendMessage()
content14.sound = UNNotificationSound.default()
var dailyTime14 = Calendar.current.dateComponents([.hour, .minute,], from: timePick.date)
dailyTime14.hour! += 336
let trigger14 = UNCalendarNotificationTrigger(dateMatching: dailyTime14, repeats: false)
let identifier14 = "Positive Message14"
let request14 = UNNotificationRequest(identifier: identifier14, content: content14, trigger: trigger14)
center.add(request14) { (error : Error?) in
if let theError = error {
print(theError.localizedDescription)
}
}
// Notification #16
let content15 = UNMutableNotificationContent()
content15.title = "Good Morning \(name)!"
content15.body = sendMessage()
content15.sound = UNNotificationSound.default()
var dailyTime15 = Calendar.current.dateComponents([.hour, .minute,], from: timePick.date)
dailyTime15.hour! += 360
let trigger15 = UNCalendarNotificationTrigger(dateMatching: dailyTime15, repeats: false)
let identifier15 = "Positive Message15"
let request15 = UNNotificationRequest(identifier: identifier15, content: content15, trigger: trigger15)
center.add(request15) { (error : Error?) in
if let theError = error {
print(theError.localizedDescription)
}
}
// Notification #17
let content16 = UNMutableNotificationContent()
content16.title = "Good Morning \(name)!"
content16.body = sendMessage()
content16.sound = UNNotificationSound.default()
var dailyTime16 = Calendar.current.dateComponents([.hour, .minute,], from: timePick.date)
dailyTime16.hour! += 384
let trigger16 = UNCalendarNotificationTrigger(dateMatching: dailyTime16, repeats: false)
let identifier16 = "Positive Message16"
let request16 = UNNotificationRequest(identifier: identifier16, content: content16, trigger: trigger16)
center.add(request16) { (error : Error?) in
if let theError = error {
print(theError.localizedDescription)
}
}
// Notification #18
let content17 = UNMutableNotificationContent()
content17.title = "Good Morning \(name)!"
content17.body = sendMessage()
content17.sound = UNNotificationSound.default()
var dailyTime17 = Calendar.current.dateComponents([.hour, .minute,], from: timePick.date)
dailyTime17.hour! += 408
let trigger17 = UNCalendarNotificationTrigger(dateMatching: dailyTime17, repeats: false)
let identifier17 = "Positive Message17"
let request17 = UNNotificationRequest(identifier: identifier17, content: content17, trigger: trigger17)
center.add(request17) { (error : Error?) in
if let theError = error {
print(theError.localizedDescription)
}
}
// Notification #19
let content18 = UNMutableNotificationContent()
content18.title = "Good Morning \(name)!"
content18.body = sendMessage()
content18.sound = UNNotificationSound.default()
var dailyTime18 = Calendar.current.dateComponents([.hour, .minute,], from: timePick.date)
dailyTime18.hour! += 432
let trigger18 = UNCalendarNotificationTrigger(dateMatching: dailyTime18, repeats: false)
let identifier18 = "Positive Message18"
let request18 = UNNotificationRequest(identifier: identifier18, content: content18, trigger: trigger18)
center.add(request18) { (error : Error?) in
if let theError = error {
print(theError.localizedDescription)
}
}
// Notification #20
let content19 = UNMutableNotificationContent()
content19.title = "Good Morning \(name)!"
content19.body = "Please open our app and update your time to keep recieving notifications!"
content19.sound = UNNotificationSound.default()
var dailyTime19 = Calendar.current.dateComponents([.hour, .minute,], from: timePick.date)
dailyTime19.hour! += 456
let trigger19 = UNCalendarNotificationTrigger(dateMatching: dailyTime18, repeats: false)
let identifier19 = "Positive Message19"
let request19 = UNNotificationRequest(identifier: identifier19, content: content19, trigger: trigger19)
center.add(request19) { (error : Error?) in
if let theError = error {
print(theError.localizedDescription)
}
}
}
// Connecting the button to the code
@IBAction func timeSet(_ sender: AnyObject) {
let center = UNUserNotificationCenter.current()
// Setting users name
name = nameText.text!
// Configuring time
timePick.timeZone = NSTimeZone.local
timePick.locale = NSLocale.current
// Scheduling notification
center.removeAllPendingNotificationRequests()
center.removeAllDeliveredNotifications()
scheduleNotif()
}
}
最佳答案
您将通知设置得很好。但请重新考虑您的代码。
就像在某些函数中尝试这样使用。我写的不是你的代码,只是一个例子来帮助你理解
func someFunction() {
for i in 1..<21 {
var notificationR = UILocalNotification()
notificationR = setUpNotification(notificationR)
notificationR.fireDate = Date.init(timeIntervalSinceNow: Double(i))
notificationR = publishLocalNotification(notificationR)
}
}
func setUpNotification(_ notification: UILocalNotification) -> UILocalNotification {
notification.alertBody = "Positive message"
notification.soundName = UILocalNotificationDefaultSoundName
notification.timeZone = TimeZone.current
notification.repeatInterval = NSCalendar.Unit.day
return notification
}
func publishLocalNotification(_ notification: UILocalNotification) -> UILocalNotification {
UIApplication.shared.applicationIconBadgeNumber = 0
UIApplication.shared.scheduleLocalNotification(notification)
return notification
}
关于ios - 时间增加一天后不会发送本地通知,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41421345/
我有一个应用程序应该在应用程序处于前台和后台(不在历史记录中)时显示提醒通知。 在前景情况下,我通过以下方法实现了这一点。 PendingIntent pendingIntent = PendingI
如何为我的 WPF 应用程序创建通知,例如浏览器上的通知,它们通过浏览器顶部的“工具栏”显示消息或通过在右下角向上/向下滑动的弹出窗口显示“MSN”样式通知屏幕。也许在应用程序中心淡入/淡出的面板可以
关闭。这个问题是opinion-based .它目前不接受答案。 想要改进这个问题? 更新问题,以便 editing this post 可以用事实和引用来回答它. 关闭 9 年前。 Improve
我正在使用 Redis 作为分布式缓存。我有不同的应用程序,它们只听特定的键。例如:App1 听 App1.*App2 监听 App2.* 等等。 我的应用程序使用以下模式接收通知:App1:“ ke
我正在尝试构建一个基于官方节点 docker 镜像的 docker 镜像,我想知道是否有某种方法可以在推送新版本的官方节点镜像时自动重建镜像。这样我的图像就不会基于过时的基础图像。 也许有类似 rss
我在一个项目中工作,我需要在添加或修改文件时在数据库中记录文件信息,以便它们保持同步。这些文件应该存储在 Nextcloud 服务器中,那么 Nextcloud 是否有办法通知这些更改(例如 webh
通知类中的方法via 如何根据用户的偏好动态变化,一个用户可能想通过电子邮件接收,而另一个用户则不想 public function via($notifiable) { return ['d
我有一个应用程序,我正在发送推送通知,如果用户登录到应用程序,这很好 - 但是,如果他们没有/如果他们没有在 X 分钟内阅读通知,我想给他们发送一封电子邮件. 我要解决的方法是使用 Laravel N
我正在使用 Django 的 contrib.comments 并想了解以下内容。 是否有任何实用程序或应用程序可以插入到某个应用程序中,当对某个项目发表评论时向您发送通知? 我并没有真正使用过那么多
我希望用户在启动应用程序之前接受协议(protocol)。所以在 appDelegate.m 中我有以下内容: - (BOOL)application:(UIApplication *)applica
我正在创建一个新指令,我想知道如何在 angular 从 DOM 中删除元素时收到通知。 我的目标是在删除元素时添加 jquery 动画。 最佳答案 如果您尝试对元素的移除进行动画处理,则需要在移除元
我正在编写一个应用程序,其工作方式与Apple的Weather.app非常相似:底部有一个UIPageControl,屏幕中间有一个UIScrollView。在我的代码中,我实现了 - (void)s
如何查明 iPhone 注册了哪些通知? 例如: notify_post("com.apple.springboard/Prefs"); 最佳答案 虽然这个问题的答案已经得到确认,但由于 @Nate
我的 Cocoa 应用程序中有一个 TextField。该文本字段有时会被填充,有时会为空。 我希望当字段为空时按钮被禁用。现在,每当我对 Core Data 执行某些操作时,我都会检查该字段,Tex
我的应用程序在其数据库中包含文档。用户可以打开文档,在这种情况下,文档将保存到临时文件夹并在用户计算机上打开。 我希望在这些临时文件之一发生更改时收到通知,并让用户将更改后的文档保存回数据库。 在 D
我目前正在开发一个网络应用程序,它不断对 php 进行 ajax 调用(轮询),以从数据库中提取新的“任务”,有点像 gmail/facebook 检查新电子邮件和消息的方式。当前的 JavaScri
我正在尝试让通知适用于我使用 Angular 5 和 Electron 制作的 Electron 应用程序。到目前为止,我的 index.html 文件中有以下代码: function doNo
我有一个录音/播放应用程序。它在后台运行。当它进入后台时,如果任何其他音频应用程序打开或开始使用音频资源,我想适本地处理我的应用程序。 iOS 提供了一种发送此类通知的方法,如在 ipod 播放器中看
关闭。这个问题需要多问focused 。目前不接受答案。 想要改进此问题吗?更新问题,使其仅关注一个问题 editing this post . 已关闭 4 年前。 Improve this ques
是否有 Subversion 的工具可以在对某些文件提交更改时自动通知我? 最佳答案 您可以创建一个 post-commit hook script “ Hook ”提交。 在钩子(Hook)脚本中,
我是一名优秀的程序员,十分优秀!