将云功能连接到 iOS 应用程序以立即发送大量推送通知 [英] Connect a Cloud Function to an iOS app to send mass push notifications at once

查看:17
本文介绍了将云功能连接到 iOS 应用程序以立即发送大量推送通知的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是 Swift 开发人员,但我不是后端开发人员.这实际上是下面以粗体显示的 3 个不同的问题,但它们都相互依赖.任何类似问题的堆栈溢出答案都足以让我开始

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:
(jsonDataDict))")
                }
            }
        } catch let err as NSError {
            print(err.debugDescription)
        }
    }
    task.resume()
}

例如,金·卡戴珊 (Kim Kardashian) 在 Instagram 上拥有 1.88 亿关注者,当她发布一些内容时,它会立即发送给她的所有关注者.我目前正在做的方式不是要走的路.我很确定这是 Cloud Functions 的情况,但我对 Cloud Functions 的了解不够,所以我正在研究从哪里开始.

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.

-如何在 iOS 应用中连接 Cloud Functions?

-无论如何我都必须从追随者"中获得每个追随者.ref 然后我必须从他们的用户"中获取每个关注者的 deviceToken参考,我不知道从哪里开始

-如何在 Cloud Functions 中实际发送推送通知代码?我找到了这个答案,但它在javascript中.我不懂 javascript,但我知道一点 Node.js

-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

后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
}

任何具有相似答案的链接都足以让我开始.我可以从那里进行更多挖掘,并根据我发现的任何内容提出更具体的问题

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

推荐答案

如何在 iOS 应用中连接 Cloud Functions?

您可以通过执行与调用任何 API 相同的操作来调用 Cloud Functions,您必须将 Cloud Functions 设计为可调用函数,您可以在其中找到有关它们的更多详细信息以及如何设置它们Swift 和其他语言,在此文档中.

You can call Cloud Functions by doing the same thing that you would do calling any API, you will have to design your Cloud Functions to be Callable Functions, you can find more details on them and how to set it all up, in Swift and other languages, in this Documentation.

无论我必须从追随者"中获得每个追随者.ref 然后我必须从他们的用户"中获取每个关注者的 deviceTokenref,我不知道从哪里开始我如何在 Cloud Functions 中实际发送推送通知代码?我找到了这个答案,但它在 javascript 中.我不懂 javascript,但我知道一点 Node.js

这个社区回答可以一起回答这两个问题,该回答指出您应该将 Firebase Cloud Messaging 集成到您的 iOS 中应用程序,带有指向该主题的完整文档的链接.此外,您可以在此文档中找到您如何可以向多个设备发送通知,您还可以在其中找到需要在 Cloud Function 本身中使用 Admin SDK 的代码示例.

These 2 questions can be answered together by this community answer that states that you should integrate Firebase Cloud Messaging into your iOS app, with a link to the full documentation on that subject. Also, you can find in this Documentation how you can send notifications to multiple devices, in there you will also find an example of the code that you will need to use in the Cloud Function itself using the Admin SDK.

注意:Cloud Functions 可以用 Node.js、Go、Java 和 Python 编写,Cloud Functions 的所有示例代码都使用这些语言.

NOTE: Cloud Functions can be written in Node.js, Go, Java and Python, and all the sample codes for the Cloud functions are in those languages.

这篇关于将云功能连接到 iOS 应用程序以立即发送大量推送通知的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆