Firebase,Swift:根据设备令牌向特定用户发送推送通知。

3
我有一个Firebase Swift聊天应用程序,我想向特定用户发送推送通知。我已经获取并可以访问用户的设备令牌。
所有的参考文献都提到需要有一个“Web应用程序”来管理此功能,但我没有找到任何具体的示例。
以下是需要回答的问题:
1. 是否必须拥有Web应用程序来管理Firebase的推送通知?
2. 如果是这样,有没有关于如何实现的示例?
谢谢您!

你可以使用Cloud Functions或者自己的服务器。使用Cloud Functions的示例:https://firebase.google.com/docs/functions/use-cases#notify_users_when_something_interesting_happens - nathan
我已经在另一个问题中回答过这个问题了,如果你需要帮助,可以随时查看一下。 https://dev59.com/11oU5IYBdhLWcg3wamca#37684936 - ZassX
@ZassX 感谢您抽出时间回答,但是您提供的链接答案是错误的 - 我现在使用 Cloud Functions 已经解决了这个问题(感谢 @nathan)。 - XCode Warrier
@XCodeWarrier 没问题。我并没有说这是正确的答案,它只是一个你可以考虑使用的替代方案。仅仅是一个想法。很高兴你解决了问题! PS:你应该更新你的答案并将你的解决方案粘贴在这里,这样可以帮助其他遇到同样问题的人。 :) - ZassX
2个回答

8
  • First do the all configuration for Firebase Cloud Messaging. Can follow this link https://firebase.google.com/docs/cloud-messaging/ios/client
  • Now you need to access Firebase registration token for your associated user whom you want to send a push notification. You can get this token by following below steps:
    1. Add 'Firebase/Messaging' pod to your project.
    2. import FirebaseMessaging in your AppDelegate.swift file.
    3. Conform MessagingDelegate protocol to your AppDelegate class.
    4. Write didRefreshRegistrationToken delegate method and get your user's registration token:
  • Now you are ready to send push notification to your specific user. Send notification as below:

    func sendPushNotification(payloadDict: [String: Any]) {
       let url = URL(string: "https://fcm.googleapis.com/fcm/send")!
       var request = URLRequest(url: url)
       request.setValue("application/json", forHTTPHeaderField: "Content-Type")
       // get your **server key** from your Firebase project console under **Cloud Messaging** tab
       request.setValue("key=enter your server key", forHTTPHeaderField: "Authorization")
       request.httpMethod = "POST"
       request.httpBody = try? JSONSerialization.data(withJSONObject: payloadDict, options: [])
       let task = URLSession.shared.dataTask(with: request) { data, response, error in
          guard let data = data, error == nil else {
            print(error ?? "")
            return
          }
          if let httpStatus = response as? HTTPURLResponse, httpStatus.statusCode != 200 {
            print("statusCode should be 200, but is \(httpStatus.statusCode)")
            print(response ?? "")
          }
          print("Notfication sent successfully.")
          let responseString = String(data: data, encoding: .utf8)
          print(responseString ?? "")
       }
       task.resume()
    }
    
  • Now call above function as:

    let userToken = "your specific user's firebase registration token"
    let notifPayload: [String: Any] = ["to": userToken,"notification": ["title":"You got a new meassage.","body":"This message is sent for you","badge":1,"sound":"default"]]
    self.sendPushNotification(payloadDict: notifPayload)
    

这对于发送通知而不制作Web应用程序非常有效,非常有帮助! - Mikkel Cortnum
这还能用吗?当我执行该函数时,什么也没有发生。编辑。控制台显示:通知已成功发送。 {"multicast_id":269539832962150,"success":1,"failure":0,"canonical_ids":0,"results":[{"message_id":"1657369222891"}]}。但是我的iPhone上没有推送。 - submariner

0
不需要拥有 Web 应用程序才能使用 Firebase 管理推送通知。 请参考 Ashvini 的回答,它提供了一种从您的 iOS 应用程序直接发送消息到其他设备的方法。
您还可以使用 Firebase 控制台手动向多个用户同时发送推送消息。
这里是通过 Web 应用程序/服务器发送消息的文档。Firebase 文档

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接