每周触发通知Swift 3

时间:2017-03-19 04:10:18

标签: swift3 notifications calendar weekday

我正在努力制定一个时间表,在这个时间表中,我需要记住我在某个时间星期一上课的所有周。问题是,如果我在打印变量triggerWeekly时分配工作日= 1(星期日)它告诉我工作日= 2,所以通过执行测试我没有收到这样的通知。我需要知道为什么会发生这种情况

<?php

$a=[2,3,4,5,6,7,8,9,10];

$b=[1,1,3,2,1,2,6,8,8];

?>

array_combine($b,$a);

Expected result as

<?php

/*
Value '1' occurs at index 0, 1 and 4 in $b
Those indices map to values 2, 3 and 6 in $a
*/
$result=[1=>[2,3,6],3=>4,2=>[5,7],6=>8,8=>[9,10]];

?>

1 个答案:

答案 0 :(得分:7)

您可以将触发器设置为在每个星期一凌晨1点05分重复,如下所示:

import UserNotifications

let trigger = UNCalendarNotificationTrigger(dateMatching: DateComponents(hour: 1, minute: 5, weekday: 2), repeats: true)
print(trigger.nextTriggerDate() ?? "nil")

let content = UNMutableNotificationContent()
content.title = "title"
content.body = "body"
// make sure you give each request a unique identifier. (nextTriggerDate description)
let request = UNNotificationRequest(identifier: "identify", content: content, trigger: trigger)
UNUserNotificationCenter.current().add(request) { error in
    if let error = error {
        print(error)
        return
    }
    print("scheduled")
}

在尝试安排通知之前,请不要忘记要求用户安排通知的权限:

UNUserNotificationCenter.current().requestAuthorization(options:[.badge, .alert, .sound]) { granted, error in
    if granted {
        print("authorized")
    }
}