我正在制作一个应用程序,用户可以创建具有标题,描述,日期和图片的“内存”。单击“保存”后,我希望该应用程序能够在他选择的日期通知用户开始事件。 我试过这段代码,但它不起作用。 我很高兴您是否可以修复我的代码或帮助我找到问题:)
future = sender.date (uidatePicker中的发件人)
(当然,我写了导入用户诺)
@IBAction func saveMemorey(_ sender: UIButton) {
// User Notification code
let center = UNUserNotificationCenter.current()
let content = UNMutableNotificationContent()
content.title = "New MEmorey!"
content.subtitle = "A New Event Starts Today:"
content.body = txtTitle.text!
content.sound = UNNotificationSound.default
content.threadIdentifier = "local-notifications temp"
let dateComponents = Calendar.current.dateComponents([.year, .month, .day, .hour, .minute, .second], from: future)
let trigger = UNCalendarNotificationTrigger(dateMatching: dateComponents, repeats: false)
let request = UNNotificationRequest(identifier: "content", content: content, trigger: trigger)
center.add(request) { (error) in
if error != nil {
print (error)
}
}
self.navigationController?.popViewController(animated: true) // Returns to the memories page after clicking 'save'
}
应用代理:
class AppDelegate: UIResponder, UIApplicationDelegate, UNUserNotificationCenterDelegate {
var window: UIWindow?
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
// Override point for customization after application launch.
FirebaseApp.configure()
let center = UNUserNotificationCenter.current()
let options : UNAuthorizationOptions = [.sound, .alert]
center.requestAuthorization(options: options) { (granted, error) in
if error != nil {
print (error)
}
}
center.delegate = self
return true
}
func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {
completionHandler([.alert, .badge, .sound])
}
future 相关:
class AddMemoryViewController: UIViewController,UIImagePickerControllerDelegate,UINavigationControllerDelegate {
var future = Date()
var dateToSet : Double = 0.0
// connections from storyboard to the code
@IBOutlet weak var countLabel: UILabel!
@IBAction func datePickerChanged(_ sender: UIDatePicker) {
future = sender.date
//Use midnight today as the starting date
guard let today = Calendar.current.date(bySettingHour: 0, minute: 0, second: 0, of: Date()) else { return }
//Calculate the number of days between today and the =user's chosen day.
let difference = Calendar.current.dateComponents([.day], from: today, to: future)
guard let days = difference.day else { return }
let ess = days > 1 ? "s" : ""
if (days > 0)
{
countLabel.text = "That date is \(days) day\(ess) away."
}
if (days < 0)
{
countLabel.text = " \(abs(days)) day\(ess) since the event."
}
if (days == 0)
{
countLabel.text = " The event is today!"
}
dateToSet = Double(self.future.millisecondsSince1970)
}
最佳答案
在AppDelegate中,您需要先向用户请求授权,才能在application(_: didFinishLaunchingWithOptions:) 方法,即
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
UNUserNotificationCenter.current().requestAuthorization(options: [.alert,.sound]) { (allowed, error) in
if allowed {
print("User has allowed notifications")
} else {
print("User has declined notifications")
}
}
return true
}
在上面的代码中,您可以根据需要提供选项。引用这个link了解更多可能的选项。
然后,一旦用户授权成功,您就可以使用您的代码安排通知。
编辑-1:
只是为了调试,通过将 future 值设置为来执行代码:
let future = Date(timeIntervalSinceNow: 10)
这会在 current Date() 的 10 秒 后触发 notification。
编辑 2:
saveMemory Action 是这样的,
@IBAction func saveMemorey(_ sender: UIButton) {
let content = UNMutableNotificationContent()
content.title = "New Memory!"
content.subtitle = "A New Event Starts Today:"
content.body = ""
content.sound = .default
let dateComponents = Calendar.current.dateComponents([.year, .month, .day, .hour, .minute, .second], from: future)
let trigger = UNCalendarNotificationTrigger(dateMatching: dateComponents, repeats: false)
let request = UNNotificationRequest(identifier: "content", content: content, trigger: trigger)
UNUserNotificationCenter.current().add(request) { (error) in
if error != nil {
print (error)
}
}
}
编辑 3:
下面是我如何使用 UIDatePicker 获取 future date
class VC: UIViewController {
@IBOutlet weak var datePicker: UIDatePicker!
var future: Date {
return self.datePicker.date
}
@IBAction func saveMemorey(_ sender: UIButton) {
//your code here....
}
//rest of the code...
}
在上面的代码中,future 是一个计算属性,返回 date 在 datePicker 在那个时间点。
关于Swift - 未显示特定日期的通知,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56633385/
我得到了一个包含嵌套链接的表单。编辑时链接字段为空的问题。这是我的表格: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
我主要使用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
所以我在关注Railscast,我注意到在html.erb文件中,ruby代码有一个微弱的背景高亮效果,以区别于其他代码HTML文档。我知道Ryan使用TextMate。我正在使用SublimeText3。我怎样才能达到同样的效果?谢谢! 最佳答案 为SublimeText安装ERB包。假设您安装了SublimeText包管理器*,只需点击cmd+shift+P即可获得命令菜单,然后键入installpackage并选择PackageControl:InstallPackage获取包管理器菜单。在该菜单中,键入ERB并在看到包时选择
我想设置一个默认日期,例如实际日期,我该如何设置?还有如何在组合框中设置默认值顺便问一下,date_field_tag和date_field之间有什么区别? 最佳答案 试试这个:将默认日期作为第二个参数传递。youcorrectlysetthedefaultvalueofcomboboxasshowninyourquestion. 关于ruby-on-rails-date_field_tag,如何设置默认日期?[rails上的ruby],我们在StackOverflow上找到一个类似的问
我试图在索引页中创建一个超链接,但它没有显示,也没有给出任何错误。这是我的index.html.erb代码。ListingarticlesTitleTextssss我检查了我的路线,我认为它们也没有问题。PrefixVerbURIPatternController#Actionwelcome_indexGET/welcome/index(.:format)welcome#indexarticlesGET/articles(.:format)articles#indexPOST/articles(.:format)articles#createnew_articleGET/article
我是rails的新手,想在form字段上应用验证。myviewsnew.html.erb.....模拟.rbclassSimulation{:in=>1..25,:message=>'Therowmustbebetween1and25'}end模拟Controller.rbclassSimulationsController我想检查模型类中row字段的整数范围,如果不在范围内则返回错误信息。我可以检查上面代码的范围,但无法返回错误消息提前致谢 最佳答案 关键是您使用的是模型表单,一种显示ActiveRecord模型实例属性的表单。c
我需要检查DateTime是否采用有效的ISO8601格式。喜欢:#iso8601?我检查了ruby是否有特定方法,但没有找到。目前我正在使用date.iso8601==date来检查这个。有什么好的方法吗?编辑解释我的环境,并改变问题的范围。因此,我的项目将使用jsapiFullCalendar,这就是我需要iso8601字符串格式的原因。我想知道更好或正确的方法是什么,以正确的格式将日期保存在数据库中,或者让ActiveRecord完成它们的工作并在我需要时间信息时对其进行操作。 最佳答案 我不太明白你的问题。我假设您想检查
我的日期格式如下:"%d-%m-%Y"(例如,今天的日期为07-09-2015),我想看看是不是在过去的七天内。谁能推荐一种方法? 最佳答案 你可以这样做:require"date"Date.today-7 关于ruby-检查日期是否在过去7天内,我们在StackOverflow上找到一个类似的问题: https://stackoverflow.com/questions/32438063/
这个问题在这里已经有了答案:Railsformattingdate(4个答案)关闭4年前。我想格式化Time.Now函数以显示YYYY-MM-DDHH:MM:SS而不是:“2018-03-0909:47:19+0000”该函数需要放在时间中.现在功能。require‘roo’require‘roo-xls’require‘byebug’file_name=ARGV.first||“Template.xlsx”excel_file=Roo::Spreadsheet.open(“./#{file_name}“,extension::xlsx)xml=Nokogiri::XML::Build