0

I am creating an app that has profiles stored as records on CloudKit. When one user adds another user. The Requester is added to the requestees. Request list. (this is where I would like a notification with the requesters name going to the requestee).

Then once the requestee accepts they are added to the requesters friendlist property on CloudKit and visa-versa.

However all the documentation I have found only shows how to send a notification anytime the CKRecord/Profile is updated in anyway. Which is what I currently have implemented but too many notifications are sent (I.e. when they change their profile name, avatar, etc...) Here is my current implementation:

func subscribeToNotifications(profile: OKGNProfile) async {
    
    let predicate = NSPredicate(format: "name == %@", profile.name)
    let subscription = CKQuerySubscription(recordType: "OKGNProfile", predicate: predicate, subscriptionID: "friendRequestAddedToDatabase", options: .firesOnRecordUpdate)
    
    let notification = CKSubscription.NotificationInfo()
    notification.title = "Friend Request"
    notification.alertBody = "Open friend feed in app to see new friend request from \(profile.name)!"
    notification.soundName = "default"
        
    subscription.notificationInfo = notification

    
    CKContainer.default().publicCloudDatabase.save(subscription) { returnedSub, returnedError in
        if let error = returnedError {
            print(error)
        } else {
            print("✅ sucessfully subscribed to notifications")
        }
    }
} 
jnpdx
  • 45,847
  • 6
  • 64
  • 94
BlakePat
  • 75
  • 7

0 Answers0