<bdo id='gDuU9'></bdo><ul id='gDuU9'></ul>

      <legend id='gDuU9'><style id='gDuU9'><dir id='gDuU9'><q id='gDuU9'></q></dir></style></legend>

        <tfoot id='gDuU9'></tfoot>
      1. <small id='gDuU9'></small><noframes id='gDuU9'>

        <i id='gDuU9'><tr id='gDuU9'><dt id='gDuU9'><q id='gDuU9'><span id='gDuU9'><b id='gDuU9'><form id='gDuU9'><ins id='gDuU9'></ins><ul id='gDuU9'></ul><sub id='gDuU9'></sub></form><legend id='gDuU9'></legend><bdo id='gDuU9'><pre id='gDuU9'><center id='gDuU9'></center></pre></bdo></b><th id='gDuU9'></th></span></q></dt></tr></i><div id='gDuU9'><tfoot id='gDuU9'></tfoot><dl id='gDuU9'><fieldset id='gDuU9'></fieldset></dl></div>

        在 Swift 2 中发出 HTTP POST 请求

        时间:2023-07-29
        <i id='IviU9'><tr id='IviU9'><dt id='IviU9'><q id='IviU9'><span id='IviU9'><b id='IviU9'><form id='IviU9'><ins id='IviU9'></ins><ul id='IviU9'></ul><sub id='IviU9'></sub></form><legend id='IviU9'></legend><bdo id='IviU9'><pre id='IviU9'><center id='IviU9'></center></pre></bdo></b><th id='IviU9'></th></span></q></dt></tr></i><div id='IviU9'><tfoot id='IviU9'></tfoot><dl id='IviU9'><fieldset id='IviU9'></fieldset></dl></div>

            <small id='IviU9'></small><noframes id='IviU9'>

                <tbody id='IviU9'></tbody>
              <legend id='IviU9'><style id='IviU9'><dir id='IviU9'><q id='IviU9'></q></dir></style></legend>
              • <bdo id='IviU9'></bdo><ul id='IviU9'></ul>

                • <tfoot id='IviU9'></tfoot>
                  本文介绍了在 Swift 2 中发出 HTTP POST 请求的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  在 Google 的 Firebase 网站上,为了发送下游消息,我必须做一个 HTTP Post 请求.这就是它所说的:

                  On Google's Firebase website, in order to send downstream messages, I have to do an HTTP Post request. This is what it says to do:

                  从服务器发送下游消息:

                  Sending downstream messages from the server:

                  为了处理或定位"下游消息,应用服务器设置为使用接收客户端应用程序的注册令牌.你可以发送带有预定义字段的通知消息,或自定义数据消息;有关详细信息,请参阅消息有效负载中的通知和数据有效载荷支持.本页中的示例显示如何发送数据消息在 HTTP 和 XMPP 协议中.

                  To address or "target" a downstream message, the app server sets to with the receiving client app's registration token. You can send notification messages with predefined fields, or custom data messages; see Notifications and data in the message payload for details on payload support. Examples in this page show how to send data messages in HTTP and XMPP protocols.

                  HTTP POST 请求

                  https://fcm.googleapis.com/fcm/send
                  Content-Type:application/json
                  Authorization:key=AIzaSyZ-1u...0GBYzPu7Udno5aA
                  
                  { "data": {
                     "score": "5x1",
                     "time": "15:10"
                    },
                    "to" : "bk3RNwTe3H0:CI2k_HHwgIpoDKCIZvvDMExUdFQ3P1..."
                  }
                  

                  如何在 Swift 2 中做到这一点?

                  How do I do this in Swift 2?

                  推荐答案

                  人们对这个问题投了反对票,这很可惜.我已经研究这个主题超过 4 个月了,没有其他 stackoverflow 问题/答案对我有帮助.

                  无论如何,我已经找到了专门针对 Google 的 Firebase Cloud 下游消息传递的解决方案.这会向 Google 服务器发送一条消息,该服务器通过 HTTP POST 请求将通知推送给所有用户.

                  Anyway, I've found the solution, specific to Google's Firebase Cloud downstream messaging. This sends a message to the Google server that pushes notifications to all users via a HTTP POST request.

                  在执行以下任何代码之前,请务必按照步骤为 Firebase 设置 iOS 客户端、安装所有正确的 pod 并获取您的证书、配置文件等.这里.

                  Before doing any of the code below, be sure to follow the steps to setting up an iOS client for Firebase, installing all the correct pods and getting your certificates, provisioning profile, etc. here.

                  视图控制器:

                  override func viewDidLoad() {
                      super.viewDidLoad()
                  
                      let url = NSURL(string: "https://fcm.googleapis.com/fcm/send")
                      let postParams: [String : AnyObject] = ["to": "<Your registration token>", "notification": ["body": "This is the body.", "title": "This is the title."]]
                  
                      let request = NSMutableURLRequest(URL: url!)
                      request.HTTPMethod = "POST"
                      request.setValue("application/json; charset=utf-8", forHTTPHeaderField: "Content-Type")
                      request.setValue("key=<Your Firebase Server Key>", forHTTPHeaderField: "Authorization")
                  
                      do 
                      {
                          request.HTTPBody = try NSJSONSerialization.dataWithJSONObject(postParams, options: NSJSONWritingOptions())
                          print("My paramaters: (postParams)")
                      }
                      catch
                      {
                          print("Caught an error: (error)")
                      }
                  
                      let task = NSURLSession.sharedSession().dataTaskWithRequest(request) { (data, response, error) in 
                  
                          if let realResponse = response as? NSHTTPURLResponse
                          {
                              if realResponse.statusCode != 200
                              {
                                  print("Not a 200 response")
                              }
                          }
                  
                          if let postString = NSString(data: data!, encoding: NSUTF8StringEncoding) as? String
                          {
                              print("POST: (postString)")
                          }
                      }
                  
                      task.resume()
                  }
                  

                  应用代理:

                  var window: UIWindow?
                  
                  func displayAlert(title: String, message: String) {
                  
                      let alert = UIAlertController(title: title, message: message, preferredStyle: .Alert)
                      alert.addAction(UIAlertAction(title: "Okay", style: .Default, handler: nil))
                      self.window?.rootViewController?.presentViewController(alert, animated: true, completion: nil)
                  }
                  
                  func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
                  
                      FIRApp.configure()
                  
                      let notificationTypes: UIUserNotificationType = [.Alert, .Badge, .Sound]
                      let pushNotifSettings = UIUserNotificationSettings(forTypes: notificationTypes, categories: nil)
                  
                      application.registerUserNotificationSettings(pushNotifSettings)
                      application.registerForRemoteNotifications()
                  
                      return true
                  }
                  
                  func application(application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: NSData) {
                  
                      print("Device Token = (deviceToken)")
                      FIRInstanceID.instanceID().setAPNSToken(deviceToken, type: FIRInstanceIDAPNSTokenType.Sandbox)
                      print("Registration token: (FIRInstanceID.instanceID().token()!)")
                  }
                  
                  func application(application: UIApplication, didFailToRegisterForRemoteNotificationsWithError error: NSError) {
                  
                      print(error)
                  }
                  
                  func application(application: UIApplication, didReceiveRemoteNotification userInfo: [NSObject : AnyObject]) {
                  
                      // For push notifications sent from within the Firebase console
                      if userInfo["google.c.a.c_l"] != nil
                      {
                          if let title = userInfo["google.c.a.c_l"] as? String
                          {
                              if let body = userInfo["aps"]!["alert"] as? String
                              {
                                  self.displayAlert(title, message: body)
                              }
                           }
                       }
                       // For push notifications sent from within the app via HTTP POST Request
                       else
                       {
                          if let title = userInfo["aps"]!["alert"]!!["title"] as? String
                          {
                              if let body = userInfo["aps"]!["alert"]!!["body"] as? String
                              {
                                  self.displayAlert(title, message: body)
                              }
                          }
                       }
                  }
                  
                  func applicationDidEnterBackground(application: UIApplication) {
                  
                      FIRMessaging.messaging().disconnect()
                      print("Disconnected from FCM")
                  }
                  

                  如果有人有任何问题,请随时提问!我也知道如何发送到主题.

                  If anybody has any questions, please feel free to ask! I also know how to send to topics.

                  谢谢!

                  这篇关于在 Swift 2 中发出 HTTP POST 请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:图片未显示在推送通知中 下一篇:如何从 Android 向主题发送 Firebase 消息

                  相关文章

                    <bdo id='0kXGY'></bdo><ul id='0kXGY'></ul>
                • <small id='0kXGY'></small><noframes id='0kXGY'>

                • <tfoot id='0kXGY'></tfoot>

                • <i id='0kXGY'><tr id='0kXGY'><dt id='0kXGY'><q id='0kXGY'><span id='0kXGY'><b id='0kXGY'><form id='0kXGY'><ins id='0kXGY'></ins><ul id='0kXGY'></ul><sub id='0kXGY'></sub></form><legend id='0kXGY'></legend><bdo id='0kXGY'><pre id='0kXGY'><center id='0kXGY'></center></pre></bdo></b><th id='0kXGY'></th></span></q></dt></tr></i><div id='0kXGY'><tfoot id='0kXGY'></tfoot><dl id='0kXGY'><fieldset id='0kXGY'></fieldset></dl></div>

                    <legend id='0kXGY'><style id='0kXGY'><dir id='0kXGY'><q id='0kXGY'></q></dir></style></legend>