Skip to content

Commit

Permalink
Fix Notifications (#33)
Browse files Browse the repository at this point in the history
  • Loading branch information
PSchmiedmayer authored Mar 15, 2023
1 parent 3d7ad13 commit 9d1b390
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 42 deletions.
4 changes: 0 additions & 4 deletions PAWS/Supporting Files/Info.plist
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,5 @@
<key>UISceneConfigurations</key>
<dict/>
</dict>
<key>UIBackgroundModes</key>
<array>
<string>remote-notification</string>
</array>
</dict>
</plist>
2 changes: 0 additions & 2 deletions PAWS/Supporting Files/PAWS.entitlements
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>aps-environment</key>
<string>development</string>
<key>com.apple.developer.healthkit</key>
<true/>
<key>com.apple.developer.healthkit.access</key>
Expand Down
68 changes: 32 additions & 36 deletions PAWSModules/Sources/PAWSOnboardingFlow/NotificationSetup.swift
Original file line number Diff line number Diff line change
Expand Up @@ -55,42 +55,38 @@ struct NotificationSetup: View {
func notificationTrigger() {
let center = UNUserNotificationCenter.current()
center.requestAuthorization(options: [.alert, .sound, .badge]) { granted, error in

if let error = error {
// Handle the error here.
}

// Enable or disable features based on the authorization.
}

let content = UNMutableNotificationContent()
content.title = "Friendly reminder to record your ECG!"
content.body = "Thank you for participating in the PAWS study!"

// Configure the recurring date.

let dateComponents = Calendar.current.dateComponents([.hour, .minute], from: selectedTime)

for index in 0...6 {
let scheduleDate = Date.now.addingTimeInterval(86400 * Double(index))
guard let notificationDate = Calendar.current.nextDate(after: scheduleDate, matching: dateComponents, matchingPolicy: .nextTime) else {
return
}

let trigger = UNTimeIntervalNotificationTrigger(timeInterval: notificationDate.timeIntervalSince(.now), repeats: false)


// Create the request
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.
}
if let error {
print("Error scheduling notification: \(error)")
} else {
let content = UNMutableNotificationContent()
content.title = "Friendly reminder to record your ECG!"
content.body = "Thank you for participating in the PAWS study!"

// Configure the recurring date.

let dateComponents = Calendar.current.dateComponents([.hour, .minute], from: selectedTime)

for index in 0...6 {
let scheduleDate = Date.now.addingTimeInterval(86400 * Double(index))
guard let notificationDate = Calendar.current.nextDate(after: scheduleDate, matching: dateComponents, matchingPolicy: .nextTime) else {
continue
}

let trigger = UNTimeIntervalNotificationTrigger(timeInterval: notificationDate.timeIntervalSince(.now), repeats: false)


// Create the request
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.
}
}
}
}
}
}
Expand Down

0 comments on commit 9d1b390

Please sign in to comment.