按钮点击每周发送本地通知

时间:2016-08-17 19:57:03

标签: ios objective-c notifications uilocalnotification

如何按钮动作每周设置通知。

//每周通知

ALL

每周应用程序都会通知消息“Hello Message”

2 个答案:

答案 0 :(得分:1)

Swift 2.2:

添加:

localNotification.repeatInterval = .Weekday

Swift 3.0:

    let content: UNMutableNotificationContent = UNMutableNotificationContent()

    content.title = "Title"

    content.subtitle = "Subtitle"

    content.body = "Hello Message"

    let calendar = Calendar.current

    let alarmTime = Date().addingTimeInterval(60*60*24*7)
    let alarmTimeComponents = calendar.dateComponents([.weekday], from: alarmTime)

    let trigger = UNCalendarNotificationTrigger(dateMatching: alarmTimeComponents, repeats: true)

    let request = UNNotificationRequest(identifier: workoutAlarmIdentifier,
                                        content: content,
                                        trigger: trigger)

    UNUserNotificationCenter.current().add(request)
    {
        (error) in // ...
    }

答案 1 :(得分:0)

  • 现在是11月5日星期六下午2点
  • 我的选择器设置为11月5日星期六下午2:10
  • 本地通知将于11月5日星期六下午2:10显示
  • 通知将于11月12日星期六下午2:10继续通知

        let ok = UNNotificationAction(identifier: "OKIdentifier", title: "Open", options: [.foreground])
        let cancel = UNNotificationAction(identifier: "CancelIdentifier", title: "Dismiss", options: [])
        let category = UNNotificationCategory(identifier: "message", actions: [ok, cancel], intentIdentifiers: [], options: [])
        UNUserNotificationCenter.current().setNotificationCategories([category])
    
        let content = UNMutableNotificationContent()
        content.body = "open the SAMPLE app"
        content.categoryIdentifier = "message"
    
        let alarmTime = datePicker.date
        let alarmTimeComponent = Calendar.current.dateComponents([.hour, .minute, .weekday], from: alarmTime)
        let trigger = UNCalendarNotificationTrigger(dateMatching: alarmTimeComponent, repeats: true)
    
        let request = UNNotificationRequest(identifier:"five second", content: content, trigger: trigger)
        UNUserNotificationCenter.current().add(request){(error) in
            if (error != nil){
                //handle here
                print("error : \(error)"  )
            }
        }