特定の日に UNNotifications を使用して iOS 10 でユーザー通知を送信し、それを繰り返すにはどうすればよいですか?
ユーザーは、通知を受け取りたい日を数日選択できます。
ユーザーがたとえば、火曜日、木曜日、日曜日を選択した場合、これらの日にそれらを受け取る必要があります。
誰でもそれで私を助けることができますか?
敬具、
解決策: import UIKit import UserNotifications
class workoutNotificationVC: UIViewController, UNUserNotificationCenterDelegate {
// Outlets
@IBOutlet weak var mondayBtn: UIButton!
@IBOutlet weak var tuesdayBtn: UIButton!
@IBOutlet weak var wednesdayBtn: UIButton!
@IBOutlet weak var thursdayBtn: UIButton!
@IBOutlet weak var fridayBtn: UIButton!
@IBOutlet weak var saturdayBtn: UIButton!
@IBOutlet weak var sundayBtn: UIButton!
@IBOutlet weak var confirmNotification: UIButton!
// Vars
var isSelectedMonday = false
var isSelectedTuesday = false
var isSelectedWednesday = false
var isSelectedThursday = false
var isSelectedFriday = false
var isSelectedSaturday = false
var isSelectedSunday = false
var dayIntegers = [Int]()
var workoutNotificationsDays = [String]()
var parentVC: CustomTabBar!
override func viewDidLoad() {
super.viewDidLoad()
UNUserNotificationCenter.current().delegate = self
}
// IBAction
@IBAction func mondayBtn(_ sender: Any) {
if isSelectedMonday == true {
isSelectedMonday = false
mondayBtn.setImage(UIImage(named: "unchecked"), for: .normal)
}else{
isSelectedMonday = true
mondayBtn.setImage(UIImage(named: "checked"), for: .normal)
dayIntegers.append(2)
workoutNotificationsDays.append("Maandag")
}
}
@IBAction func tuesdayBtnPressed(_ sender: Any) {
if isSelectedTuesday == true {
isSelectedTuesday = false
tuesdayBtn.setImage(UIImage(named: "unchecked"), for: .normal)
}else{
isSelectedTuesday = true
tuesdayBtn.setImage(UIImage(named: "checked"), for: .normal)
dayIntegers.append(3)
workoutNotificationsDays.append("Dinsdag")
}
}
@IBAction func wednesdayBtnPressed(_ sender: Any) {
if isSelectedWednesday == true {
isSelectedWednesday = false
wednesdayBtn.setImage(UIImage(named: "unchecked"), for: .normal)
}else{
isSelectedWednesday = true
wednesdayBtn.setImage(UIImage(named: "checked"), for: .normal)
dayIntegers.append(4)
workoutNotificationsDays.append("Woensdag")
}
}
@IBAction func thursdayBtnPressed(_ sender: Any) {
if isSelectedThursday == true {
isSelectedThursday = false
thursdayBtn.setImage(UIImage(named: "unchecked"), for: .normal)
}else{
isSelectedThursday = true
thursdayBtn.setImage(UIImage(named: "checked"), for: .normal)
dayIntegers.append(5)
workoutNotificationsDays.append("Donderdag")
}
}
@IBAction func fridayBtnPressed(_ sender: Any) {
if isSelectedFriday == true {
isSelectedFriday = false
fridayBtn.setImage(UIImage(named: "unchecked"), for: .normal)
}else{
isSelectedFriday = true
fridayBtn.setImage(UIImage(named: "checked"), for: .normal)
dayIntegers.append(6)
workoutNotificationsDays.append("Vrijdag")
}
}
@IBAction func saturdayBtnPressed(_ sender: Any) {
if isSelectedSaturday == true {
isSelectedSaturday = false
saturdayBtn.setImage(UIImage(named: "unchecked"), for: .normal)
}else{
isSelectedSaturday = true
saturdayBtn.setImage(UIImage(named: "checked"), for: .normal)
dayIntegers.append(7)
workoutNotificationsDays.append("Zaterdag")
}
}
@IBAction func sundayBtnPressed(_ sender: Any) {
if isSelectedSunday == true {
isSelectedSunday = false
sundayBtn.setImage(UIImage(named: "unchecked"), for: .normal)
}else{
isSelectedSunday = true
sundayBtn.setImage(UIImage(named: "checked"), for: .normal)
dayIntegers.append(1)
workoutNotificationsDays.append("Zondag")
}
}
func sendNotificationTest() {
// Check what days were selected and send notifications on these days
for day in dayIntegers {
if day == 1 {
print("Je wilt een notificatie op Zondag")
// Send the notifcation at a specific time or date
var dateComponents = DateComponents()
dateComponents.weekday = 1
dateComponents.hour = 9
dateComponents.minute = 0
let content = UNMutableNotificationContent()
let requestIdentifier = "Sunday Notification"
content.title = "We are missing you"
content.subtitle = "You received 100 coins for Sunday"
content.body = "Your Notification for Sunday "
content.badge = 1
let trigger = UNCalendarNotificationTrigger(dateMatching: dateComponents, repeats: true)
// create request
let request = UNNotificationRequest(identifier: requestIdentifier, content: content, trigger: trigger)
UNUserNotificationCenter.current().add(request, withCompletionHandler: { error in
if (error != nil) {
print("there was an error with the notification")
}else{
print("we have sent your notification on Sunday!")
}
})
}else if day == 2 {
print("Je wilt een notificatie op Maandag")
// Send the notifcation at a specific time or date
var dateComponents = DateComponents()
dateComponents.weekday = 2
dateComponents.hour = 9
dateComponents.minute = 0
let content = UNMutableNotificationContent()
let requestIdentifier = "Monday Notification"
content.title = "We are missing you"
content.subtitle = "You received 100 coins for Monday"
content.body = "Your Notification for Monday "
content.badge = 1
let trigger = UNCalendarNotificationTrigger(dateMatching: dateComponents, repeats: true)
// create request
let request = UNNotificationRequest(identifier: requestIdentifier, content: content, trigger: trigger)
UNUserNotificationCenter.current().add(request, withCompletionHandler: { error in
if (error != nil) {
print("there was an error with the notification")
}else{
print("we have sent your notification on Monday!")
}
})
}else if day == 3 {
print("Je wilt een notificatie op Dinsdag")
// Send the notifcation at a specific time or date
var dateComponents = DateComponents()
dateComponents.weekday = 3
dateComponents.hour = 9
dateComponents.minute = 0
let content = UNMutableNotificationContent()
let requestIdentifier = "Tuesday Notification"
content.title = "We are missing you"
content.subtitle = "You received 54 coins for Tuesday"
content.body = "Your Notification for Tuesday "
content.badge = 1
let trigger = UNCalendarNotificationTrigger(dateMatching: dateComponents, repeats: true)
// create request
let request = UNNotificationRequest(identifier: requestIdentifier, content: content, trigger: trigger)
UNUserNotificationCenter.current().add(request, withCompletionHandler: { error in
if (error != nil) {
print("there was an error with the notification")
}else{
print("we have sent your notification on Tuesday!")
}
})
}else if day == 4 {
print("Je wilt een notificatie op Woensdag")
// Send the notifcation at a specific time or date
var dateComponents = DateComponents()
dateComponents.weekday = 4
dateComponents.hour = 9
dateComponents.minute = 0
let content = UNMutableNotificationContent()
let requestIdentifier = "Wednesday Notification"
content.title = "We are missing you"
content.subtitle = "You received 1000000 coins for Wednesday"
content.body = "Your Notification for Wednesday "
content.badge = 1
let trigger = UNCalendarNotificationTrigger(dateMatching: dateComponents, repeats: true)
// create request
let request = UNNotificationRequest(identifier: requestIdentifier, content: content, trigger: trigger)
UNUserNotificationCenter.current().add(request, withCompletionHandler: { error in
if (error != nil) {
print("there was an error with the notification")
}else{
print("we have sent your notification on wednesday!")
}
})
}else if day == 5 {
print("Je wilt een notificatie op Donderdag")
// Send the notifcation at a specific time or date
var dateComponents = DateComponents()
dateComponents.weekday = 5
dateComponents.hour = 9
dateComponents.minute = 0
let content = UNMutableNotificationContent()
let requestIdentifier = "Thursday Notification"
content.title = "We are missing you"
content.subtitle = "You received zero coins for Thursday"
content.body = "Your Notification for Thursday "
content.badge = 1
let trigger = UNCalendarNotificationTrigger(dateMatching: dateComponents, repeats: true)
// create request
let request = UNNotificationRequest(identifier: requestIdentifier, content: content, trigger: trigger)
UNUserNotificationCenter.current().add(request, withCompletionHandler: { error in
if (error != nil) {
print("there was an error with the notification")
}else{
print("we have sent your notification on Thursday!")
}
})
}else if day == 6 {
print("Je wilt een notificatie op Vrijdag")
// Send the notifcation at a specific time or date
var dateComponents = DateComponents()
dateComponents.weekday = 6
dateComponents.hour = 9
dateComponents.minute = 0
let content = UNMutableNotificationContent()
let requestIdentifier = "Friday Notification"
content.title = "We are missing you"
content.subtitle = "You received some coins for Friday"
content.body = "Your Notification for Friday "
content.badge = 1
let trigger = UNCalendarNotificationTrigger(dateMatching: dateComponents, repeats: true)
// create request
let request = UNNotificationRequest(identifier: requestIdentifier, content: content, trigger: trigger)
UNUserNotificationCenter.current().add(request, withCompletionHandler: { error in
if (error != nil) {
print("there was an error with the notification")
}else{
print("we have sent your notification on Friday!")
}
})
}else if day == 7 {
print("Je wilt een notificatie op Zaterdag")
// Send the notifcation at a specific time or date
var dateComponents = DateComponents()
dateComponents.weekday = 7
dateComponents.hour = 9
dateComponents.minute = 0
let content = UNMutableNotificationContent()
let requestIdentifier = "Saturday Notification"
content.title = "We are missing you"
content.subtitle = "You received some Really nice coins for Saturday"
content.body = "Your Notification for Saturday "
content.badge = 1
let trigger = UNCalendarNotificationTrigger(dateMatching: dateComponents, repeats: true)
// create request
let request = UNNotificationRequest(identifier: requestIdentifier, content: content, trigger: trigger)
UNUserNotificationCenter.current().add(request, withCompletionHandler: { error in
if (error != nil) {
print("there was an error with the notification")
}else{
print("we have sent your notification on Saturday!")
}
})
}
}
}
// shows notificatins in app
func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {
// reset icon badge number to no notification when opened
UIApplication.shared.applicationIconBadgeNumber = 0
// shows the notification in app
completionHandler([.alert, .sound])
}
@IBAction func confirmNotificationBtnPressed(_ sender: Any) {
sendNotificationTest()
// we have send the notifications to the users days
}
}