当地通知每天或每周重复一次不准确的快速4.2。 Xcode 10

时间:2018-10-10 15:46:16

标签: ios swift usernotifications

我正在使用swift 4.2。 Xcode 10 请给我在本地使用的通知带来麻烦,但是我发现它在错误的日期和重复的时间有些重复

我正在尝试每周一次重复本地通知,所以我像苹果所说的那样使用

   dateComponents.weekday = 6   // Friday  >>>>> this is not worke

   dateComponents.hour = 14    // 14:00 hours >>> working good

   dateComponents.minute =  59   // 14:00 hours .  >>> working good

所以当我放dateComponents.weekday = any number

假设

1 =星期日     2 =星期一

3=`tuesday`

4=wednesday

5=thursday

6= friday

7=saturday

但是当我写通知时,通知日比我输入的要晚

 func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {



    let center = UNUserNotificationCenter.current()
    let options: UNAuthorizationOptions = [.alert, .sound];
    center.requestAuthorization(options: options) { (granted, error) in
        if !granted {
            print("Something went wrong")
        }
    }

    center.getNotificationSettings { (settings) in
        if settings.authorizationStatus != .authorized {
            // Notifications not allowed
        }
    }
    let content = UNMutableNotificationContent()

    content.title = "test"
    content.body = "test hello  "

    content.sound = UNNotificationSound.default()
  //   let trigger = UNTimeIntervalNotificationTrigger(timeInterval: 300, repeats: false)
    var dateComponents = DateComponents()
    dateComponents.calendar = Calendar.current
  // dateComponents.day = 3
       dateComponents.weekday = 6   // Friday
    dateComponents.hour = 14    // 14:00 hours
    dateComponents.minute =  59   // 14:00 hours

    let trigger = UNCalendarNotificationTrigger(
        dateMatching: dateComponents, repeats: true)
    let uuidString = UUID().uuidString
    let request = UNNotificationRequest(identifier: uuidString,
                                        content: content, trigger: trigger)

    // Schedule the request with the system.
    let notificationCenter = UNUserNotificationCenter.current()
    notificationCenter.add(request) { (error) in
        if error != nil {
            // Handle any errors.
        }
    }

    return true
}

func applicationWillResignActive(_ application: UIApplication) {
    // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
    // Use this method to pause ongoing tasks, disable timers, and invalidate graphics rendering callbacks. Games should use this method to pause the game.
}

0 个答案:

没有答案