【问题标题】:Local Notification on weekly basis on button click单击按钮每周通知本地通知
【发布时间】:2016-08-17 19:57:02
【问题描述】:

如何在按钮操作上设置每周通知。

// 每周通知

-(IBAction)buttonAction:(id)sender{
    UILocalNotification* localNotification = [[UILocalNotification alloc] init];
    localNotification.fireDate =  [NSDate dateByAddingTimeInterval:60*60*24*7];
    localNotification.alertBody = @"Hello Message";
    localNotification.alertAction = @"Show me the item";
    [[UIApplication sharedApplication] scheduleLocalNotification:localNotification];
    localNotification.repeatInterval = NSWeekCalendarUnit;
}

每周申请都会通知消息“Hello Message”

【问题讨论】:

    标签: ios objective-c notifications uilocalnotification


    【解决方案1】:

    斯威夫特 2.2:

    添加:

    localNotification.repeatInterval = .Weekday
    

    斯威夫特 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 // ...
        }
    

    【讨论】:

      【解决方案2】:
      • 现在是 11 月 5 日星期六下午 2:00
      • 我有一个选择器设置为 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)"  )
                }
            }
        

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多