Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
cbaker6 committed Feb 27, 2025
1 parent 57c0d44 commit b7489c5
Show file tree
Hide file tree
Showing 7 changed files with 273 additions and 140 deletions.
118 changes: 41 additions & 77 deletions Sources/ParseSwift/Protocols/ParsePushApplePayload.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,23 +6,32 @@
// Copyright © 2024 Network Reconnaissance Lab. All rights reserved.
//

import Foundation

// swiftlint:disable line_length

protocol ParsePushApplePayload: ParsePushApplePayloadable {
protocol ParsePushAppleHeader {

/**
The background notification flag. If you are a writing an app using the Remote Notification
Background Mode introduced in iOS7 (a.k.a. “Background Push”), set this value to
1 to trigger a background update. For more informaiton, see [Apple's documentation](https://developer.apple.com/documentation/usernotifications/setting_up_a_remote_notification_server/pushing_background_updates_to_your_app).
- warning: For Apple OS's only. You also have to set `pushType` starting iOS 13
and watchOS 6.
The unique ID for the notification.
*/
var contentAvailable: Int? { get set }
var id: UUID? { get set }
/**
The notification service app extension flag. Set this value to 1 to trigger the system to pass the notification to your notification service app extension before delivery. Use your extension to modify the notification’s content. For more informaiton, see [Apple's documentation](https://developer.apple.com/documentation/usernotifications/modifying_content_in_newly_delivered_notifications).
- warning: You also have to set `pushType` starting iOS 13
and watchOS 6.
The unique ID for this request.
- note: Used for broadcast push notifications.
*/
var mutableContent: Int? { get set }
var requestId: UUID? { get set }
/**
A base64-encoded string that identifies the channel to publish the payload.
The channel ID is generated by sending channel creation request to APNs.
- note: Used for broadcast push notifications.
*/
var channelId: String? { get set }
/**
Multiple notifications with same collapse identifier are displayed to the user as a single
notification. The value should not exceed 64 bytes.
*/
var collapseId: String? { get set }
/**
The priority of the notification. Specify 10 to send the notification immediately.
Specify 5 to send the notification based on power considerations on the user’s device.
Expand All @@ -31,79 +40,34 @@ protocol ParsePushApplePayload: ParsePushApplePayloadable {
- warning: For Apple OS's only.
*/
var priority: Int? { get set }

var pushType: ParsePushPayloadApple.PushType? { get set }

var badge: AnyCodable? { get set }
var sound: AnyCodable? { get set }
}

extension ParsePushApplePayload {

/**
Set the name of a sound file in your app’s main bundle or in the Library/Sounds folder
of your app’s container directory. For information about how to prepare sounds, see
[UNNotificationSound](https://developer.apple.com/documentation/usernotifications/unnotificationsound).
- parameter sound: An instance of `ParsePushAppleSound`.
- returns: A mutated instance of `ParsePushPayloadApple` for easy chaining.
- warning: For Apple OS's only.
*/
public func setSound(_ sound: ParsePushAppleSound) -> Self {
var mutablePayload = self
mutablePayload.sound = AnyCodable(sound)
return mutablePayload
}

/**
Set the name of a sound file in your app’s main bundle or in the Library/Sounds folder
of your app’s container directory. Specify the string “default” to play the system
sound. Pass a string for **regular** notifications. For critical alerts, pass the sound
`ParsePushAppleSound` instead. For information about how to prepare sounds, see
[UNNotificationSound](https://developer.apple.com/documentation/usernotifications/unnotificationsound).
- parameter sound: A `String` or any `Codable` object that can be sent to APN.
- returns: A mutated instance of `ParsePushPayloadApple` for easy chaining.
- warning: For Apple OS's only.
The destination topic for the notification.
*/
public func setSound<V>(_ sound: V) -> Self where V: Codable {
var mutablePayload = self
mutablePayload.sound = AnyCodable(sound)
return mutablePayload
}

var topic: String? { get set }
/**
Get the sound using any type that conforms to `Codable`.
- returns: The sound casted to the inferred type.
- throws: An error of type `ParseError`.
The type of the notification. The value is alert or background. Specify alert when the
delivery of your notification displays an alert, plays a sound, or badges your app’s icon.
Specify background for silent notifications that do not interact with the user.
Defaults to alert if no value is set.
- warning: Required when delivering notifications to
devices running iOS 13 and later, or watchOS 6 and later. Ignored on earlier OS versions.
*/
public func getSound<V>() throws -> V where V: Codable {
guard let sound = sound?.value as? V else {
throw ParseError(code: .otherCause,
message: "Cannot be casted to the inferred type")
}
return sound
}
var pushType: ParsePushPayloadApple.PushType? { get set }
}

public protocol ParsePushApplePayload: ParsePushApplePayloadable {
/**
Set the badge to a specific value to display on your app's icon.
- parameter badge: The number to display in a badge on your app’s icon.
Specify 0 to remove the current badge, if any.
- returns: A mutated instance of `ParsePushPayloadApple` for easy chaining.
- warning: For Apple OS's only.
The background notification flag. If you are a writing an app using the Remote Notification
Background Mode introduced in iOS7 (a.k.a. “Background Push”), set this value to
1 to trigger a background update. For more informaiton, see [Apple's documentation](https://developer.apple.com/documentation/usernotifications/setting_up_a_remote_notification_server/pushing_background_updates_to_your_app).
- warning: For Apple OS's only. You also have to set `pushType` starting iOS 13
and watchOS 6.
*/
public func setBadge(_ number: Int) -> Self {
var mutablePayload = self
mutablePayload.badge = AnyCodable(number)
return mutablePayload
}

var contentAvailable: Int? { get set }
/**
Increment the badge value by 1 to display on your app's icon.
- warning: For Apple OS's only.
- returns: A mutated instance of `ParsePushPayloadApple` for easy chaining.
The notification service app extension flag. Set this value to 1 to trigger the system to pass the notification to your notification service app extension before delivery. Use your extension to modify the notification’s content. For more informaiton, see [Apple's documentation](https://developer.apple.com/documentation/usernotifications/modifying_content_in_newly_delivered_notifications).
- warning: You also have to set `pushType` starting iOS 13
and watchOS 6.
*/
public func incrementBadge() -> Self {
var mutablePayload = self
mutablePayload.badge = AnyCodable(ParseOperationIncrement(amount: 1))
return mutablePayload
}
var mutableContent: Int? { get set }
}
41 changes: 24 additions & 17 deletions Sources/ParseSwift/Protocols/ParsePushApplePayloadable.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,19 +17,8 @@ import Foundation
need to implement `CodingKeys`, see `ParsePushPayloadApple` for an example.
*/
public protocol ParsePushApplePayloadable: ParsePushPayloadable {
/**
The payload for displaying an alert.
*/
var alert: ParsePushAppleAlert? { get set }
/**
The destination topic for the notification.
*/
var topic: String? { get set }
/**
Multiple notifications with same collapse identifier are displayed to the user as a single
notification. The value should not exceed 64 bytes.
*/
var collapseId: String? { get set }

// MARK: Header and other high level information.
/**
The type of the notification. The value is alert or background. Specify alert when the
delivery of your notification displays an alert, plays a sound, or badges your app’s icon.
Expand All @@ -39,6 +28,28 @@ public protocol ParsePushApplePayloadable: ParsePushPayloadable {
devices running iOS 13 and later, or watchOS 6 and later. Ignored on earlier OS versions.
*/
var pushType: ParsePushPayloadApple.PushType? { get set }

// MARK: APS information.

/**
The background notification flag. If you are a writing an app using the Remote Notification
Background Mode introduced in iOS7 (a.k.a. “Background Push”), set this value to
1 to trigger a background update. For more informaiton, see [Apple's documentation](https://developer.apple.com/documentation/usernotifications/setting_up_a_remote_notification_server/pushing_background_updates_to_your_app).
- warning: For Apple OS's only. You also have to set `pushType` starting iOS 13
and watchOS 6.
*/
var contentAvailable: Int? { get set }
/**
The notification service app extension flag. Set this value to 1 to trigger the system to pass the notification to your notification service app extension before delivery. Use your extension to modify the notification’s content. For more informaiton, see [Apple's documentation](https://developer.apple.com/documentation/usernotifications/modifying_content_in_newly_delivered_notifications).
- warning: You also have to set `pushType` starting iOS 13
and watchOS 6.
*/
var mutableContent: Int? { get set }

/**
The payload for displaying an alert.
*/
var alert: ParsePushAppleAlert? { get set }
/**
The identifier of the `UNNotification​Category` for this push notification.
See Apple's
Expand Down Expand Up @@ -78,10 +89,6 @@ public protocol ParsePushApplePayloadable: ParsePushPayloadable {
notification summary. See [relevanceScore](https://developer.apple.com/documentation/usernotifications/unnotificationcontent/3821031-relevancescore).
*/
var relevanceScore: Double? { get set }
/**
Specify for the `mdm` field where applicable.
*/
var mdm: String? { get set }

init()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,28 +6,70 @@
// Copyright © 2024 Network Reconnaissance Lab. All rights reserved.
//

struct ParsePushAppleNotification<P: ParsePushApplePayload>: ParsePushPayloadable {
import Foundation

var aps: P?
struct ParsePushAppleNotification<P: ParsePushApplePayload>: ParsePushAppleHeader, ParsePushPayloadable {

struct APS: ParseTypeable {
var payload: P

enum CodingKeys: String, CodingKey {

Check warning on line 16 in Sources/ParseSwift/Types/ParsePushPayload/Apple/ParsePushAppleNotification.swift

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

Sources/ParseSwift/Types/ParsePushPayload/Apple/ParsePushAppleNotification.swift#L16

Types should be nested at most 1 level deep
case payload = "aps"
}
}

// Notification Header properties set by parse-server-push-adapter.
var collapseId: String?
var pushType: ParsePushPayloadApple.PushType?
var priority: Int?
var mdm: String?
var pushType: ParsePushPayloadApple.PushType?
var topic: String?

// Notification Header properties set directly.
var id: UUID?
var requestId: UUID?
var channelId: String?
var payload: APS?

public init() {}

public init(payload: P) {
self.aps = payload
self.collapseId = payload.collapseId
init(
id: UUID? = nil,
collapseId: String? = nil,
requestId: UUID? = nil,
channelId: String? = nil,
priority: Int? = nil,
topic: String? = nil,
payload: P
) {
self.id = id
self.collapseId = collapseId
self.requestId = requestId
self.channelId = channelId
self.priority = priority
self.topic = topic
self.pushType = payload.pushType
self.priority = payload.priority
self.mdm = payload.mdm
self.payload = APS(payload: payload)
}

enum CodingKeys: String, CodingKey {
case pushType = "push_type"
case collapseId = "collapse_id"
case mdm = "_mdm"
case aps, priority
case payload = "rawPayload"
case collapseId, priority, topic, id, requestId, channelId
}

}

extension ParsePushAppleNotification where P: ParsePushApplePayload & ParsePushAppleHeader {

init(
id: UUID? = nil,
payload: P
) {
self.id = id
self.collapseId = payload.collapseId
self.pushType = payload.pushType
self.priority = payload.priority
self.topic = payload.topic
self.payload = APS(payload: payload)
}
}
Loading

0 comments on commit b7489c5

Please sign in to comment.