【发布时间】:2020-09-16 23:28:16
【问题描述】:
我是 Swift 开发人员,但我不是后端开发人员。这实际上是下面以粗体显示的 3 个不同的问题,但它们都相互依赖。任何类似问题的堆栈溢出答案都足以让我开始
@followers
|
kim_userId // kimKardashian
-userId_0: 1
-... // every user in between
-userId_188_million: 1
现在我正在使用一种非常低效的方式来发送大量推送通知:
@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()
}
例如,金·卡戴珊 (Kim Kardashian) 在 Instagram 上拥有 1.88 亿关注者,当她发布一些内容时,它会立即发送给她的所有关注者。我目前正在做的方式不是要走的路。我很确定这是 Cloud Functions 的一种情况,但我对 Cloud Functions 的了解不够,所以我正在寻找从哪里开始。
-如何在 iOS 应用中连接 Cloud Functions?
-无论我必须从“追随者”参考中获取每个追随者,然后我必须从他们的“用户”参考中获取每个追随者的 deviceToken,我不知道从哪里开始强>
-如何在 Cloud Functions 中实际发送推送通知代码?我找到了这个answer but it's in javascript。我不懂 javascript,但我知道一点 Node.js
后VC:
@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
}
任何具有相似答案的链接都足以让我开始。我可以从那里进行更多挖掘,并根据我发现的任何内容提出更具体的问题
【问题讨论】:
标签: ios node.js swift google-cloud-functions