DatePicker本地通知(Xcode,swift2)(DatePicker Local Notification(Xcode,swift2))

编程入门 行业动态 更新时间:2024-10-28 12:25:26
DatePicker本地通知(Xcode,swift2)(DatePicker Local Notification(Xcode,swift2))

我正在为我的app使用localNotifications。我有一个日期选择器,用户从中选择通知的日期和时间,我还有3个分段控件(每日,每周,每月)。我已经完成了本地通知及其工作的编码。但是分段控件不起作用,如果一个人选择两次(下午1点)和第二次(下午2点)两次,然后通知显示在我不想要的两个时间,则会出现另一个问题。下面是代码

在appdelegate.swift中

let types:UIUserNotificationType = UIUserNotificationType.Alert | UIUserNotificationType.Badge | UIUserNotificationType.Sound let myAlarmSetting:UIUserNotificationSettings = UIUserNotificationSettings(forTypes: types, categories: categories as Set<NSObject>) UIApplication.sharedApplication().registerUserNotificationSettings(myAlarmSetting)

在notifications.swift中@IBAction func NotificationButtonTapped(sender:AnyObject){

var notifications:UILocalNotification = UILocalNotification() notifications.fireDate = datePicker.date notifications.timeZone = NSTimeZone.defaultTimeZone() notifications.applicationIconBadgeNumber = UIApplication.sharedApplication().applicationIconBadgeNumber + 1 notifications.soundName = UILocalNotificationDefaultSoundName switch(frequencysegmentedcontrol.selectedSegmentIndex){ case 0: notifications.repeatInterval = NSCalendarUnit.CalendarUnitDay break; case 1: notifications.repeatInterval = NSCalendarUnit.CalendarUnitWeekOfYear break; case 2: notifications.repeatInterval = NSCalendarUnit.CalendarUnitYear break; default: notifications.repeatInterval = NSCalendarUnit(0) break; } notifications.alertBody = "quote of the day" notifications.category = "First_category" UIApplication.sharedApplication().scheduleLocalNotification(notifications) } func application(application: UIApplication, didReceiveLocalNotification notification: UILocalNotification) { application.applicationIconBadgeNumber = 0 }

I am using localNotifications for my app.I have a datepicker from which a users picks date and time for the notifications and I also have 3 segmented controls(daily,weekly,monthly).I have done the coding for localnotification and its working.But the segmented control is not working and one more problem if a person chooses two times like one for(1 pm) and second for (2pm) and then the notifications are displaying in both time which i dont want.Below is the code

In appdelegate.swift

let types:UIUserNotificationType = UIUserNotificationType.Alert | UIUserNotificationType.Badge | UIUserNotificationType.Sound let myAlarmSetting:UIUserNotificationSettings = UIUserNotificationSettings(forTypes: types, categories: categories as Set<NSObject>) UIApplication.sharedApplication().registerUserNotificationSettings(myAlarmSetting)

In notifications.swift @IBAction func NotificationButtonTapped(sender: AnyObject) {

var notifications:UILocalNotification = UILocalNotification() notifications.fireDate = datePicker.date notifications.timeZone = NSTimeZone.defaultTimeZone() notifications.applicationIconBadgeNumber = UIApplication.sharedApplication().applicationIconBadgeNumber + 1 notifications.soundName = UILocalNotificationDefaultSoundName switch(frequencysegmentedcontrol.selectedSegmentIndex){ case 0: notifications.repeatInterval = NSCalendarUnit.CalendarUnitDay break; case 1: notifications.repeatInterval = NSCalendarUnit.CalendarUnitWeekOfYear break; case 2: notifications.repeatInterval = NSCalendarUnit.CalendarUnitYear break; default: notifications.repeatInterval = NSCalendarUnit(0) break; } notifications.alertBody = "quote of the day" notifications.category = "First_category" UIApplication.sharedApplication().scheduleLocalNotification(notifications) } func application(application: UIApplication, didReceiveLocalNotification notification: UILocalNotification) { application.applicationIconBadgeNumber = 0 }

最满意答案

我可以帮助您解决两次本地通知问题。

解:

问题是,当您添加任何本地通知时 - 首先,您需要检查是否安排了任何本地通知?

那么,我们如何检查?

好吧,你需要在notifications.userInfo存储一些数据。 例如

var userInfo = [String:String]() let example = "SomeNotification" userInfo["example"] = example notification.userInfo = userInfo

参考: https : //www.hackingwithswift.com/read/21/2/scheduling-notifications-uilocalnotification

因此,一旦更新相同通知的时间,您需要取消现有通知并使用新时间设置新通知。

var app:UIApplication = UIApplication.sharedApplication() for oneEvent in app.scheduledLocalNotifications { var notification = oneEvent as UILocalNotification let userInfoCurrent = notification.userInfo! as [String:AnyObject] let uid = userInfoCurrent["uid"]! as String if uid == uidtodelete { //Cancelling local notification app.cancelLocalNotification(notification) break; } }

参考: 删除特定的本地通知

I can help you with your two times Local notification problem.

Solution:

The problem is that when you add any local notification - First of all you need to check that if any local notification is scheduled or not?

So, how we can check?

Well, you need to store some data for that in notifications.userInfo. E.g.

var userInfo = [String:String]() let example = "SomeNotification" userInfo["example"] = example notification.userInfo = userInfo

Reference: https://www.hackingwithswift.com/read/21/2/scheduling-notifications-uilocalnotification

So, once you update the time for the same notification, you need to cancel the existing notification and set the new notification with new time.

var app:UIApplication = UIApplication.sharedApplication() for oneEvent in app.scheduledLocalNotifications { var notification = oneEvent as UILocalNotification let userInfoCurrent = notification.userInfo! as [String:AnyObject] let uid = userInfoCurrent["uid"]! as String if uid == uidtodelete { //Cancelling local notification app.cancelLocalNotification(notification) break; } }

Reference: Delete a particular local notification

更多推荐

本文发布于:2023-08-02 19:28:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1379376.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:通知   Xcode   DatePicker   Notification   Local

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!