swift-server-community / APNSwift

📱HTTP/2 Apple Push Notification Service built with swift - send push notifications to iOS, iPadOS, tvOS, macOS, watchOS, visionOS, and Safari!
Apache License 2.0
693 stars 105 forks source link

support send raw payload data #185

Closed wangzhizhou closed 1 year ago

wangzhizhou commented 1 year ago

such as the json string payload could be transform to data and be sent

If the rawPayloadData be set, message would not been encoded into bytebuffer, the rawPayloadData as the whole post request body bytes.

If rawPayloadData is nil, the send logic is same as the 5.0.0, that is the message been encoded into post request body and be sent.

There is an example use it:

      var client = APNSClient(
          configuration: .init(
              authenticationMethod: .jwt(
                  privateKey: try .init(pemRepresentation: config.privateKey),
                  keyIdentifier: config.keyIdentifier,
                  teamIdentifier: config.teamIdentifier
              ),
              environment: config.apnsServerEnv.env
          ),
          eventLoopGroupProvider: .createNew,
          responseDecoder: JSONDecoder(),
          requestEncoder: JSONEncoder()
      )

      guard let payloadData = """
      {
        "aps": {
          "alert": {
            "body": "Body Content",
            "subtitle": "Subtitle Content",
            "title": "Simple Alert Content"
          }
        },
        "o_url": "https://www.baidu.com",
        "obj": {
          "key1": "value",
          "key2": 2
        }
      }
      """.data(using: .utf8)
      else {
          return false
      }

      var response: APNSResponse?
      switch config.pushType {
      case .alert:
          response = try await client?.sendAlertNotification(
              .init(
                  alert: .init(
                      title: .raw("Simple Alert"),
                      subtitle: .raw("Subtitle"),
                      body: .raw("Body")),
                  expiration: .immediately,
                  priority: .immediately,
                  topic: config.appBundleID,
                  rawPayloadData: payloadData
              ),
              deviceToken: config.deviceToken)
      case .background:
          response = try await client?.sendBackgroundNotification(
              .init(
                  expiration: .immediately,
                  topic: config.appBundleID,
                  rawPayloadData: payloadData
              ),
              deviceToken: config.deviceToken)
      case .voip:
          response = try await client?.sendVoIPNotification(
              .init(
                  priority: .immediately,
                  appID: config.appBundleID,
                  rawPayloadData: payloadData
              ),
              deviceToken: config.pushKitVoIPToken)
      case .fileprovider:
          response = try await client?.sendFileProviderNotification(
              .init(
                  expiration: .immediately,
                  appID: config.appBundleID,
                  rawPayloadData: payloadData
              ),
              deviceToken: config.pushKitFileProviderToken)
      }
kylebrowning commented 1 year ago

This is not something we are going to support going forward, if you want to send a raw payload, you can do so as mentioned here. https://github.com/swift-server-community/APNSwift/issues/179

wangzhizhou commented 1 year ago

Get it, Thx