Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 34 additions & 0 deletions Sources/APNSCore/MDM/APNSClient+MDM.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
//===----------------------------------------------------------------------===//
//
// This source file is part of the APNSwift open source project
//
// Copyright (c) 2022 the APNSwift project authors
// Licensed under Apache License v2.0
//
// See LICENSE.txt for license information
// See CONTRIBUTORS.txt for the list of APNSwift project authors
//
// SPDX-License-Identifier: Apache-2.0
//
//===----------------------------------------------------------------------===//

extension APNSClientProtocol {
@discardableResult
@inlinable
public func sendMDMNotification(
_ notification: APNSMDMNotification,
deviceToken: String
) async throws -> APNSResponse {
let request = APNSRequest(
message: notification,
deviceToken: deviceToken,
pushType: .mdm,
expiration: nil,
priority: nil,
apnsID: notification.apnsID,
topic: notification.topic,
collapseID: nil
)
return try await send(request)
}
}
49 changes: 49 additions & 0 deletions Sources/APNSCore/MDM/APNSMDMNotification.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
//===----------------------------------------------------------------------===//
//
// This source file is part of the APNSwift open source project
//
// Copyright (c) 2022 the APNSwift project authors
// Licensed under Apache License v2.0
//
// See LICENSE.txt for license information
// See CONTRIBUTORS.txt for the list of APNSwift project authors
//
// SPDX-License-Identifier: Apache-2.0
//
//===----------------------------------------------------------------------===//

#if canImport(FoundationEssentials)
import struct FoundationEssentials.UUID
#else
import struct Foundation.UUID
#endif

public struct APNSMDMNotification: APNSMessage {
@usableFromInline
enum CodingKeys: String, CodingKey {
case pushMagic = "mdm"
}

public var pushMagic: String

public var apnsID: UUID?

public var topic: String

@inlinable
public init(
topic: String,
pushMagic: String,
apnsID: UUID? = nil
) {
self.topic = topic
self.pushMagic = pushMagic
self.apnsID = apnsID
}

@inlinable
public func encode(to encoder: Encoder) throws {
var container = encoder.container(keyedBy: CodingKeys.self)
try container.encode(self.pushMagic, forKey: .pushMagic)
}
}
29 changes: 29 additions & 0 deletions Tests/APNSTests/APNSClientIntegrationTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -356,6 +356,35 @@ struct APNSClientIntegrationTests {
}
}

// MARK: - MDM Notifications

@Test func `Send MDM notification`() async throws {
try await TestFixtures.withClient { server, client in
let notification = APNSMDMNotification(
topic: "com.apple.mgmt.External.3b3d5f2a-4b7c-4e1d-9f0a-2c9d8e7f6a5b",
pushMagic: "PushMagicValue"
)

_ = try await client.sendMDMNotification(
notification,
deviceToken: "aaaa1111aaaa1111aaaa1111aaaa1111aaaa1111aaaa1111aaaa1111aaaa1111"
)

let sent = try #require(server.getSentNotifications().first)
#expect(sent.pushType == "mdm")
#expect(sent.topic == "com.apple.mgmt.External.3b3d5f2a-4b7c-4e1d-9f0a-2c9d8e7f6a5b")

let expectedJSONString = """
{"mdm":"PushMagicValue"}
"""
let jsonObject1 = try #require(JSONSerialization.jsonObject(with: sent.payload) as? NSDictionary)
let jsonObject2 = try #require(
JSONSerialization.jsonObject(with: Data(expectedJSONString.utf8)) as? NSDictionary
)
#expect(jsonObject1 == jsonObject2)
}
}

// MARK: - Accessory Notifications

@Test func `Send accessory notification`() async throws {
Expand Down
36 changes: 36 additions & 0 deletions Tests/APNSTests/MDM/APNSMDMNotificationTests.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
//===----------------------------------------------------------------------===//
//
// This source file is part of the APNSwift open source project
//
// Copyright (c) 2022 the APNSwift project authors
// Licensed under Apache License v2.0
//
// See LICENSE.txt for license information
// See CONTRIBUTORS.txt for the list of APNSwift project authors
//
// SPDX-License-Identifier: Apache-2.0
//
//===----------------------------------------------------------------------===//

import APNSCore
import Foundation
import Testing

struct APNSMDMNotificationTests {
@Test func encode() throws {
let notification = APNSMDMNotification(
topic: "com.apple.mgmt.External.3b3d5f2a-4b7c-4e1d-9f0a-2c9d8e7f6a5b",
pushMagic: "PushMagicValue"
)
#expect(notification.topic == "com.apple.mgmt.External.3b3d5f2a-4b7c-4e1d-9f0a-2c9d8e7f6a5b")

let data = try JSONEncoder().encode(notification)

let expectedJSONString = """
{"mdm":"PushMagicValue"}
"""
let jsonObject1 = try #require(JSONSerialization.jsonObject(with: data) as? NSDictionary)
let jsonObject2 = try #require(JSONSerialization.jsonObject(with: Data(expectedJSONString.utf8)) as? NSDictionary)
#expect(jsonObject1 == jsonObject2)
}
}
Loading