Spring Break Update

It’s been a little while since my last update due to Spring Break. I thought that I would be able to do this project entirely as a watchOS application but found this won’t be the case — I’ll have to create a partner iOS application.  This makes sense for multiple reasons such as a watch’s limited screen size (and thus limitation on displayed information) and pushing watch notifications. 

Before spring break I was following some of Apple’s scheduling local notifications documentation to test pushing notifications to users. 

Scheduling a Notification Locally from Your App | Apple Developer Documentation 

https://developer.apple.com/documentation/watchkit/notifications/enabling_and_receiving_notifications

https://developer.apple.com/documentation/usernotifications/asking_permission_to_use_notifications

https://developer.apple.com/documentation/usernotifications/untimeintervalnotificationtrigger

https://www.hackingwithswift.com/example-code/system/how-to-set-local-alerts-using-unnotificationcenter

I had been working on a function “heartRateNotification” that would get called in the heart rate query. In this function I would attempt to display a watch notification saying if a user’s HR was above or below average.

func heartRateNotification() { //left off here..enters but no notification show on watch...
        print("in heartRateNotification..count: \(countHRNotifs)")

        if countHRNotifs == 20{
            print("should send notification to user...")
            let center = UNUserNotificationCenter.current()

            let content = UNMutableNotificationContent()
            content.title = "Stress Update"
            content.subtitle = "Avg HR is: \(self.averageHeartRate)"
            
            if heartRate > averageHeartRate{
                content.body = "current HR \(self.heartRate) above avg!"
            }
            else{
                content.body = "current HR \(self.heartRate) below avg!"
            }
            
        
            content.categoryIdentifier = "alarm"
            content.userInfo = ["customData": "fizzbuzz"]
            content.sound = UNNotificationSound.default

            //deliver notification after specified time interval...60 seconds * 1 for one minute..
            //notification only goes once ...repeats:false
            let trigger = UNTimeIntervalNotificationTrigger(timeInterval: (1*60), repeats: false)

            let request = UNNotificationRequest(identifier: UUID().uuidString, content: content, trigger: trigger)
            center.add(request)
            
            //**EVENTUALLY SOLVE THIS WITH TIME INTERVAL/REPEATS on timeintervalnotification trigger
            countHRNotifs = 0 //reset count to space out notifications user gets
        }
        else{
            countHRNotifs += 1 //work way back up to 20
        }
        
    }//end heartRateNotification
case HKQuantityType.quantityType(forIdentifier: .heartRate):
                let heartRateUnit = HKUnit.count().unitDivided(by:   HKUnit.minute())
                self.heartRate = statistics.mostRecentQuantity()?.doubleValue(for: heartRateUnit) ?? 0
                self.averageHeartRate = statistics.averageQuantity()?.doubleValue(for: heartRateUnit) ?? 0
                print("curr HR: \(self.heartRate)")
                self.heartRateNotification() //call to notification fcn!

 I was unable to get any notifications to appear…my thoughts on this are that the documentation I was following was for pushing notifications on an iOS device and not a watch. To test this, I’m going to create an iOS application that is paired with the watch app and see if I am able to get notifications to show. If this does not work there may simply be an error in my code, or I will need to attempt to use Apple Push Notifications (APNs).   

Registering Your App with APNs | Apple Developer Documentation 

However, APN requires a server and may involve a lot of unnecessary work.  Instead, I could idealistically utilize the iOS app and use it to display metrics gathered from my watch application’s session and give appropriate feedback/suggestions from there.  

A lot of these thoughts are just speculating on what’s next, so for now I’ll take it one step at a time. No matter what I’d like to create an iOS application to display not only metrics but also tips about stress information and suggestions on what to do in stressful scenarios.  Getting push notifications to work with an app would be a nice add-on to this. 

Leave a Reply

Your email address will not be published.

css.php