Skip to content

Commit 93b9fe7

Browse files
authored
Add semantic safe API for location notifications (#147)
# Motivation We want to provide new APIs that are semantically safe when sending location notifications to APNs. # Modification The PR adds new types for the location notification and a convenience method to send it notifications with the APNSClient. # Result We can now send location notifications.
1 parent 0418312 commit 93b9fe7

File tree

2 files changed

+124
-0
lines changed

2 files changed

+124
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
//===----------------------------------------------------------------------===//
2+
//
3+
// This source file is part of the APNSwift open source project
4+
//
5+
// Copyright (c) 2022 the APNSwift project authors
6+
// Licensed under Apache License v2.0
7+
//
8+
// See LICENSE.txt for license information
9+
// See CONTRIBUTORS.txt for the list of APNSwift project authors
10+
//
11+
// SPDX-License-Identifier: Apache-2.0
12+
//
13+
//===----------------------------------------------------------------------===//
14+
15+
import NIOCore
16+
import Logging
17+
18+
extension APNSClient {
19+
/// Sends a location request notification to APNs.
20+
///
21+
/// - Parameters:
22+
/// - notification: The notification to send.
23+
///
24+
/// - deviceToken: The hexadecimal bytes that identify the user’s device. Your app receives the bytes for this device token
25+
/// when registering for remote notifications.
26+
///
27+
/// - deadline: Point in time by which sending the notification to APNs must complete.
28+
///
29+
/// - logger: The logger to use for sending this notification.
30+
@discardableResult
31+
@inlinable
32+
func sendLocationNotification(
33+
_ notification: APNSLocationNotification,
34+
deviceToken: String,
35+
deadline: NIODeadline,
36+
logger: Logger = _noOpLogger
37+
) async throws -> APNSResponse {
38+
try await self.send(
39+
// This is just to make the compiler work
40+
payload: Int?.none,
41+
deviceToken: deviceToken,
42+
pushType: .location,
43+
expiration: .none, // TODO: Figure out if expiration has any impact here
44+
priority: notification.priority,
45+
topic: notification.topic,
46+
deadline: deadline,
47+
logger: logger
48+
)
49+
}
50+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
//===----------------------------------------------------------------------===//
2+
//
3+
// This source file is part of the APNSwift open source project
4+
//
5+
// Copyright (c) 2022 the APNSwift project authors
6+
// Licensed under Apache License v2.0
7+
//
8+
// See LICENSE.txt for license information
9+
// See CONTRIBUTORS.txt for the list of APNSwift project authors
10+
//
11+
// SPDX-License-Identifier: Apache-2.0
12+
//
13+
//===----------------------------------------------------------------------===//
14+
15+
import struct Foundation.UUID
16+
17+
/// A location notification.
18+
public struct APNSLocationNotification {
19+
/// A canonical UUID that identifies the notification. If there is an error sending the notification,
20+
/// APNs uses this value to identify the notification to your server. The canonical form is 32 lowercase hexadecimal digits,
21+
/// displayed in five groups separated by hyphens in the form 8-4-4-4-12. An example UUID is as follows:
22+
/// `123e4567-e89b-12d3-a456-42665544000`.
23+
///
24+
/// If you omit this, a new UUID is created by APNs and returned in the response.
25+
public var apnsID: UUID?
26+
27+
/// The topic for the notification. In general, the topic is your app’s bundle ID/app ID suffixed with `.location-query`.
28+
public var topic: String
29+
30+
/// The priority of the notification.
31+
public var priority: APNSPriority
32+
33+
/// Initializes a new ``APNSLocationNotification``.
34+
///
35+
/// - Important: Your dynamic payload will get encoded to the root of the JSON payload that is send to APNs.
36+
/// It is **important** that you do not encode anything with the key `aps`
37+
///
38+
/// - Parameters:
39+
/// - priority: The priority of the notification.
40+
/// - appID: Your app’s bundle ID/app ID. This will be suffixed with `.location-query`.
41+
/// - apnsID: A canonical UUID that identifies the notification.
42+
@inlinable
43+
public init(
44+
priority: APNSPriority,
45+
appID: String,
46+
apnsID: UUID? = nil
47+
) {
48+
self.init(
49+
priority: priority,
50+
topic: appID + ".location-query",
51+
apnsID: apnsID
52+
)
53+
}
54+
55+
/// Initializes a new ``APNSLocationNotification``.
56+
///
57+
/// - Important: Your dynamic payload will get encoded to the root of the JSON payload that is send to APNs.
58+
/// It is **important** that you do not encode anything with the key `aps`
59+
///
60+
/// - Parameters:
61+
/// - priority: The priority of the notification.
62+
/// - topic: The topic for the notification. In general, the topic is your app’s bundle ID/app ID suffixed with `.location-query`.
63+
/// - apnsID: A canonical UUID that identifies the notification.
64+
@inlinable
65+
public init(
66+
priority: APNSPriority,
67+
topic: String,
68+
apnsID: UUID? = nil
69+
) {
70+
self.priority = priority
71+
self.topic = topic
72+
self.apnsID = apnsID
73+
}
74+
}

0 commit comments

Comments
 (0)