I'm a Swift dev and I'm not a backend dev. This is actually 3 different questions in bold below but they all are dependent upon each other. Any stack overflow answers to similar questions would be enough to get me started
@followers
|
kim_userId // kimKardashian
-userId_0: 1
-... // every user in between
-userId_188_million: 1
Right now I'm using a very inefficient way to send a mass push notification:
@IBAction func postButtonTapped(button: UIButton) {
let postsRef = Database.database().reference().child("posts").child(kim_userId).child(postId)
postsRef.updateChildValues(postDictionary, withCompletionBlock: { (err, _)
if let error = error { return }
// post was successful now send a push notification to all of these followers
self.fetchFollowers(for: kim_userId, send: postId)
})
}
func fetchFollowers(for userId: String, send newPostId: String) {
let followersRef = Database.database().reference().child("followers").child(userId)
followersRef.observe(.childAdded) { (snapshot) in
let userId = snapshot.key
self.fetchDeviceToken(forFollower: userId, send: newPostId)
}
}
func fetchDeviceToken(forFollower userId: String, send newPostId: String) {
let usersRef = Database.database().reference().child("users").child(userId)
usersRef.observeSingleEvent(of .value) { (snapshot) in
guard let dict = snapshot.value as? [String: Any] else { return }
guard let deviceToken = dict["deviceToken"] as? String else { return }
self.sendPushNotification(toFollower: userId, with: deviceToken, send: newPostId)
}
}
func sendPushNotification(toFollower: userId, with deviceToken: String, send newPostId: String) {
var apsDict = [String: Any]()
// newPostId and whatever other values added to the dictionary
guard let url = URL(string: "https://fcm.googleapis.com/fcm/send") else { return }
var request = URLRequest(url: url)
request.httpMethod = "POST"
request.httpBody = try? JSONSerialization.data(withJSONObject: apsDict, options: [])
request.setValue("application/json", forHTTPHeaderField: "Content-Type")
request.setValue("key=\(my_serverKey...)", forHTTPHeaderField: "Authorization")
let task = URLSession.shared.dataTask(with: request) { (data, response, error) in
do {
if let jsonData = data {
if let jsonDataDict = try JSONSerialization.jsonObject(with: jsonData, options: JSONSerialization.ReadingOptions.allowFragments) as? [String: AnyObject] {
print("Received data:\n\(jsonDataDict))")
}
}
} catch let err as NSError {
print(err.debugDescription)
}
}
task.resume()
}
For eg Kim Kardashian has 188 million followers on Instagram, when she posts something it goes out to all of her followers at once. The way I'm currently doing it is not the way to go. I'm pretty sure this is a situation for Cloud Functions but I do not know enough about Cloud Functions so I am looking at where to start.
-how do I connect with Cloud Functions from within an iOS app?
-no matter what I have to get each follower from the "followers" ref and then I have to get each follower's deviceToken from within their "users" ref, I'm not sure where to start here
-how do I actually send a push notification code once inside Cloud Functions? I found this answer but it's in javascript. I don't know javascript but I do know a tad bit of Node.js
PostVC:
@IBAction func postButtonTapped(button: UIButton) {
let postsRef = Database.database().reference().child("posts").child(kim_userId).child(postId)
postsRef.updateChildValues(postDictionary, withCompletionBlock: { (err, _)
if let error = error { return }
// post was successful now connect to Cloud Functions so that a mass push notification can be sent
self.codeToConnectWithCloudFunctions(for: kim_userId, send: postId)
})
}
func codeToConnectWithCloudFunctions(for userId: String, send newPostId: String) {
// 1. how do I get each of her followers
// 2. how do I get each of their deviceTokens
// 3. how do I send the push notification
}
Any links with similar answers are enough to get me started. I can do more digging from there and ask a more specific question based on whatever I find