当检测到信标信号时,我的信标通知会随时触发。我如何限制它每天只接收一次通知?
我实现了日期检查,但这没有帮助。 if/else 检查工作正常,但信标通知被触发而无视此检查
非常感谢任何解决方法的建议!
这里是我的代码片段:
class ViewController: UIViewController, CLLocationManagerDelegate {
let locationManager = CLLocationManager()
let date = Date()
let calendar = Calendar.current
override func viewDidLoad() {
super.viewDidLoad()
locationManager.delegate = self
locationManager.requestAlwaysAuthorization()
locationManager.desiredAccuracy = 10
locationManager.distanceFilter = 100
locationManager.startUpdatingLocation()
let year = calendar.component(.year, from: date)
let month = calendar.component(.month, from: date)
let day = calendar.component(.day, from: date)
let currentDate = "\(year)\(month)\(day)"
var savedDate = UserDefaults.standard.string(forKey: "savedDate") ?? "12345"
print(currentDate)
print(savedDate)
if (savedDate == currentDate){
print("same date - no action")
} else {
savedDate = currentDate
UserDefaults.standard.set(savedDate, forKey: "savedDate")
print(savedDate + " execute program")
let beaconRegion = CLBeaconRegion(proximityUUID: UUID(uuidString: "13D9F4C7-A68D-46F4-8D35-4BA7F64BC417")!, identifier: "estimote")
beaconRegion.notifyOnEntry = true
beaconRegion.notifyOnExit = false
let content = UNMutableNotificationContent()
content.title = "? Daily beacon check! ?"
content.subtitle = "Receive a new info every day!"
content.body = "ONLY ONCE A DAY WE SUPPLY INFO!"
content.sound = .default
content.badge = 1
let trigger = UNLocationNotificationTrigger(region: beaconRegion, repeats: true)
let identifier = "estimote"
let request = UNNotificationRequest.init(identifier: identifier, content: content, trigger: trigger)
UNUserNotificationCenter.current().removeAllPendingNotificationRequests()
UNUserNotificationCenter.current().delegate = self as? UNUserNotificationCenterDelegate
UNUserNotificationCenter.current().add(request, withCompletionHandler: { (error) in
})
self.locationManager.startRangingBeacons(in: beaconRegion)
}
}
最佳答案
通过设置
let trigger = UNLocationNotificationTrigger(region: beaconRegion, repeats: true)
let request = UNNotificationRequest.init(identifier: identifier, content: content, trigger: trigger)
您基本上是在告诉程序关闭并在满足 beaconRegion 条件时自动发送通知。如果您总是想要通知并让系统为您处理,这很方便。但是你不可能在那个时候进行干预,因为它是自动运行的。
您可以做的是手动处理通知,并在每次检测到信标时检查是否应该发送通知。为此使用 locationManager(didRangeBeacons beacons:in region:) 委托(delegate)函数:
let locationManager = CLLocationManager()
override func viewDidLoad() {
super.viewDidLoad()
// Setup beacon tracking once
locationManager.delegate = self
locationManager.requestAlwaysAuthorization()
locationManager.desiredAccuracy = 10
locationManager.distanceFilter = 100
locationManager.startUpdatingLocation()
let beaconRegion = CLBeaconRegion(proximityUUID: UUID(uuidString: "13D9F4C7-A68D-46F4-8D35-4BA7F64BC417")!, identifier: "estimote")
beaconRegion.notifyOnEntry = true
beaconRegion.notifyOnExit = false
// Start looking for beacons
self.locationManager.startRangingBeacons(in: beaconRegion)
}
// Handle beacons in range
func locationManager(_ manager: CLLocationManager, didRangeBeacons beacons: [CLBeacon], in region: CLBeaconRegion) {
let now = Date()
let formatter = DateFormatter()
formatter.dateFormat = "yyyyMMdd"
let nowString = formatter.string(from: now)
// Check if notified ever (lastTime != nil) and if notified today
if let lastTime = UserDefaults.standard.string(forKey: "savedDate"), lastTime == nowString {
// Already notified today, skip
print("same date - no action")
return
}
// Your notification code
let content = UNMutableNotificationContent()
content.title = "? Daily beacon check! ?"
content.subtitle = "Receive a new info every day!"
content.body = "ONLY ONCE A DAY WE SUPPLY INFO!"
content.sound = UNNotificationSound.default()
content.badge = 1
let identifier = "estimote"
let request = UNNotificationRequest.init(identifier: identifier, content: content, trigger: nil)
UNUserNotificationCenter.current().add(request, withCompletionHandler: { (error) in
if error != nil {
print("Error showing notification: \(error!.localizedDescription)")
} else {
print("Notification shown")
}
})
UserDefaults.standard.set(nowString, forKey: "savedDate")
}
关于swift - 如何在 Swift 中每天只显示一次信标通知?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52348786/
出于纯粹的兴趣,我很好奇如何按顺序创建PI,而不是在过程结果之后生成数字,而是让数字在过程本身生成时显示。如果是这种情况,那么数字可以自行产生,我可以对以前看到的数字实现垃圾收集,从而创建一个无限系列。结果只是在Pi系列之后每秒生成一个数字。这是我通过互联网筛选的结果:这是流行的计算机友好算法,类机器算法:defarccot(x,unity)xpow=unity/xn=1sign=1sum=0loopdoterm=xpow/nbreakifterm==0sum+=sign*(xpow/n)xpow/=x*xn+=2sign=-signendsumenddefcalc_pi(digits
我得到了一个包含嵌套链接的表单。编辑时链接字段为空的问题。这是我的表格:Editingkategori{:action=>'update',:id=>@konkurrancer.id})do|f|%>'Trackingurl',:style=>'width:500;'%>'Editkonkurrence'%>|我的konkurrencer模型:has_one:link我的链接模型:classLink我的konkurrancer编辑操作:defedit@konkurrancer=Konkurrancer.find(params[:id])@konkurrancer.link_attrib
大约一年前,我决定确保每个包含非唯一文本的Flash通知都将从模块中的方法中获取文本。我这样做的最初原因是为了避免一遍又一遍地输入相同的字符串。如果我想更改措辞,我可以在一个地方轻松完成,而且一遍又一遍地重复同一件事而出现拼写错误的可能性也会降低。我最终得到的是这样的:moduleMessagesdefformat_error_messages(errors)errors.map{|attribute,message|"Error:#{attribute.to_s.titleize}#{message}."}enddeferror_message_could_not_find(obje
如何在buildr项目中使用Ruby?我在很多不同的项目中使用过Ruby、JRuby、Java和Clojure。我目前正在使用我的标准Ruby开发一个模拟应用程序,我想尝试使用Clojure后端(我确实喜欢功能代码)以及JRubygui和测试套件。我还可以看到在未来的不同项目中使用Scala作为后端。我想我要为我的项目尝试一下buildr(http://buildr.apache.org/),但我注意到buildr似乎没有设置为在项目中使用JRuby代码本身!这看起来有点傻,因为该工具旨在统一通用的JVM语言并且是在ruby中构建的。除了将输出的jar包含在一个独特的、仅限ruby
我正在使用的第三方API的文档状态:"[O]urAPIonlyacceptspaddedBase64encodedstrings."什么是“填充的Base64编码字符串”以及如何在Ruby中生成它们。下面的代码是我第一次尝试创建转换为Base64的JSON格式数据。xa=Base64.encode64(a.to_json) 最佳答案 他们说的padding其实就是Base64本身的一部分。它是末尾的“=”和“==”。Base64将3个字节的数据包编码为4个编码字符。所以如果你的输入数据有长度n和n%3=1=>"=="末尾用于填充n%
我主要使用Ruby来执行此操作,但到目前为止我的攻击计划如下:使用gemsrdf、rdf-rdfa和rdf-microdata或mida来解析给定任何URI的数据。我认为最好映射到像schema.org这样的统一模式,例如使用这个yaml文件,它试图描述数据词汇表和opengraph到schema.org之间的转换:#SchemaXtoschema.orgconversion#data-vocabularyDV:name:namestreet-address:streetAddressregion:addressRegionlocality:addressLocalityphoto:i
使用带有Rails插件的vim,您可以创建一个迁移文件,然后一次性打开该文件吗?textmate也可以这样吗? 最佳答案 你可以使用rails.vim然后做类似的事情::Rgeneratemigratonadd_foo_to_bar插件将打开迁移生成的文件,这正是您想要的。我不能代表textmate。 关于ruby-使用VimRails,您可以创建一个新的迁移文件并一次性打开它吗?,我们在StackOverflow上找到一个类似的问题: https://sta
在选择我想要运行操作的频率时,唯一的选项是“每天”、“每小时”和“每10分钟”。谢谢!我想为我的Rails3.1应用程序运行调度程序。 最佳答案 这不是一个优雅的解决方案,但您可以安排它每天运行,并在实际开始工作之前检查日期是否为当月的第一天。 关于ruby-如何每月在Heroku运行一次Scheduler插件?,我们在StackOverflow上找到一个类似的问题: https://stackoverflow.com/questions/8692687/
exe应该在我打开页面时运行。异步进程需要运行。有什么方法可以在ruby中使用两个参数异步运行exe吗?我已经尝试过ruby命令-system()、exec()但它正在等待过程完成。我需要用参数启动exe,无需等待进程完成是否有任何rubygems会支持我的问题? 最佳答案 您可以使用Process.spawn和Process.wait2:pid=Process.spawn'your.exe','--option'#Later...pid,status=Process.wait2pid您的程序将作为解释器的子进程执行。除
鉴于我有以下迁移:Sequel.migrationdoupdoalter_table:usersdoadd_column:is_admin,:default=>falseend#SequelrunsaDESCRIBEtablestatement,whenthemodelisloaded.#Atthispoint,itdoesnotknowthatusershaveais_adminflag.#Soitfails.@user=User.find(:email=>"admin@fancy-startup.example")@user.is_admin=true@user.save!ende