Push-Definitions/Sources/PushMessageDefinitions/PushMessage.swift
2022-06-30 09:10:47 +02:00

157 lines
5.2 KiB
Swift
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import APNSwift
import Foundation
/**
A push message to send to other devices.
This structure contains the content of the notification, as well as all data to send the notification to one or more recipients.
*/
public struct PushMessage: Codable {
/**
A list of push tokens for all recipients of the message
*/
public let recipients: [PushToken]
/**
The notification content.
*/
public let payload: APNSPayload
/**
The value of this header must accurately reflect the contents of your notifications payload.
If theres a mismatch, or if the header is missing on required systems, APNs may return an error, delay the delivery of the notification, or drop it altogether.
- Note: Required for watchOS 6 and later; recommended for macOS, iOS, tvOS, and iPadOS
*/
public let pushType: APNSClient.PushType
/**
The date at which the notification is no longer valid.
If the value is not `nil`, APNs stores the notification and tries to deliver it at least once, repeating the attempt as needed until the specified date. If the value is `nil`, APNs attempts to deliver the notification only once and doesnt store it.
A single APNs attempt may involve retries over multiple network interfaces and connections of the destination device. Often these retries span over some time period, depending on the network characteristics. In addition, a push notification may take some time on the network after APNs sends it to the device. APNs uses best efforts to honor the expiry date without any guarantee. If the value is not `nil`, the notification may be delivered after the mentioned date. If the value is `nil`, the notification may be delivered with some delay.
*/
public let expiration: Date?
/**
The priority of the notification.
Set `lowPriority = false` to send the notification immediately.
Set `lowPriority = true` to send the notification based on power considerations on the users device.
*/
public let lowPriority: Bool
/**
An identifier you use to coalesce multiple notifications into a single notification for the user.
Typically, each notification request causes a new notification to be displayed on the users device. When sending the same notification more than once, use the same value in this header to coalesce the requests. The value of this key must not exceed 64 bytes.
*/
public let collapseIdentifier: String?
/**
Create a new push message.
- Parameter recipients: A list of push tokens for all recipients of the message
- Parameter payload: The message content and metadata
- Parameter pushType: The content type of the message
- Parameter topic: The topic id
- Parameter expiration: An optional expiration date for the message
- Parameter lowPriority: Specify `true` to deliver message with lower priority
- Parameter collapseIdentifier: An optional identifier to group push notifications
*/
public init(recipients: [PushToken],
payload: APNSPayload,
pushType: APNSClient.PushType,
expiration: Date? = nil,
lowPriority: Bool = false,
collapseIdentifier: String? = nil) {
self.recipients = recipients
self.payload = payload
self.pushType = pushType
self.expiration = expiration
self.lowPriority = lowPriority
self.collapseIdentifier = collapseIdentifier
}
}
extension APNSClient.PushType: Codable {
struct PushTypeDecodingError: Error {
}
public func encode(to encoder: Encoder) throws {
var container = encoder.singleValueContainer()
try container.encode(rawValue)
}
public init(from decoder: Decoder) throws {
let container = try decoder.singleValueContainer()
let raw = try container.decode(UInt8.self)
switch raw {
case 1:
self = .alert
case 2:
self = .background
case 3:
self = .mdm
case 4:
self = .voip
case 5:
self = .fileprovider
case 6:
self = .complication
default:
throw PushTypeDecodingError()
}
}
private var rawValue: UInt8 {
switch self {
case .alert:
return 1
case .background:
return 2
case .mdm:
return 3
case .voip:
return 4
case .fileprovider:
return 5
case .complication:
return 6
//case .location:
// return ".location-query"
default:
return 0
}
}
}
extension APNSClient.PushType {
/// The extension to add to the push topic depending on the notification type
public var topicExtension: String {
switch self {
case .alert, .background:
return ""
case .mdm:
return ""
case .voip:
return ".voip"
case .fileprovider:
return ".pushkit.fileprovider"
//case .location:
// return ".location-query"
case .complication:
return ".complication"
default:
return ""
}
}
}