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

查看:34
本文介绍了将Cloud Function连接到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:\n\(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.ref,我不确定从哪里开始

-实际上如何在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

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

推荐答案

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

您可以通过执行与调用任何API相同的操作来调用Cloud Functions,您将必须将Cloud Functions设计为Callable 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,然后我必须从其用户"内部获取每个关注者的deviceToken.ref,我不确定从何处开始,并且在Cloud Functions中实际上如何发送一次推送通知代码?我找到了这个答案,但是它在javascript中.我不懂javascript,但我确实了解一点Node.js

以下两个社区答案可以一起回答这两个问题,指出您应该将Firebase Cloud Messaging集成到iOS中应用程序,并提供有关该主题的完整文档的链接.另外,您可以在文档中找到自己的方式可以将通知发送到多个设备,在那里您还将找到需要使用Admin SDK在Cloud Function本身中使用的代码示例.

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函数的所有示例代码都使用这些语言.

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.

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

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