|
| 1 | +//: [Previous](@previous) |
| 2 | + |
| 3 | +import PlaygroundSupport |
| 4 | +import Foundation |
| 5 | +import ParseSwift |
| 6 | + |
| 7 | +PlaygroundPage.current.needsIndefiniteExecution = true |
| 8 | +initializeParse() |
| 9 | + |
| 10 | +struct Installation: ParseInstallation { |
| 11 | + //: These are required by `ParseObject`. |
| 12 | + var objectId: String? |
| 13 | + var createdAt: Date? |
| 14 | + var updatedAt: Date? |
| 15 | + var ACL: ParseACL? |
| 16 | + var originalData: Data? |
| 17 | + |
| 18 | + //: These are required by `ParseInstallation`. |
| 19 | + var installationId: String? |
| 20 | + var deviceType: String? |
| 21 | + var deviceToken: String? |
| 22 | + var badge: Int? |
| 23 | + var timeZone: String? |
| 24 | + var channels: [String]? |
| 25 | + var appName: String? |
| 26 | + var appIdentifier: String? |
| 27 | + var appVersion: String? |
| 28 | + var parseVersion: String? |
| 29 | + var localeIdentifier: String? |
| 30 | + |
| 31 | + //: Your custom keys |
| 32 | + var customKey: String? |
| 33 | + |
| 34 | + //: Implement your own version of merge |
| 35 | + func merge(with object: Self) throws -> Self { |
| 36 | + var updated = try mergeParse(with: object) |
| 37 | + if updated.shouldRestoreKey(\.customKey, |
| 38 | + original: object) { |
| 39 | + updated.customKey = object.customKey |
| 40 | + } |
| 41 | + return updated |
| 42 | + } |
| 43 | +} |
| 44 | + |
| 45 | +/** |
| 46 | + We will begin by creating the payload information we want to |
| 47 | + send in the push notification. |
| 48 | + */ |
| 49 | +let helloAlert = ParsePushAppleAlert(body: "Hello from ParseSwift!") |
| 50 | +let applePayload = ParsePushPayloadApple(alert: helloAlert) |
| 51 | + .setBadge(1) |
| 52 | + |
| 53 | +/*: |
| 54 | + We now crate a query where the `objectId` |
| 55 | + is not null or undefined. |
| 56 | +*/ |
| 57 | +let installationQuery = Installation.query(isNotNull(key: "objectId")) |
| 58 | + |
| 59 | +//: Now create a new push notification using the payload and query. |
| 60 | +let push = ParsePush(payload: applePayload, query: installationQuery) |
| 61 | + |
| 62 | +//: Creating this property to use later in the playground. |
| 63 | +var pushStatusId = "" |
| 64 | + |
| 65 | +//: You can send the push notification whenever you are ready. |
| 66 | +push.send { result in |
| 67 | + switch result { |
| 68 | + case .success(let statusId): |
| 69 | + print("The push was created with id: \"\(statusId)\"") |
| 70 | + //: Update the stored property with the lastest status id. |
| 71 | + pushStatusId = statusId |
| 72 | + case .failure(let error): |
| 73 | + print("Couldn't create push: \(error)") |
| 74 | + } |
| 75 | +} |
| 76 | + |
| 77 | +//: You can fetch the status of notificaiton if you know it's id. |
| 78 | +push.fetchStatus(pushStatusId) { result in |
| 79 | + switch result { |
| 80 | + case .success(let pushStatus): |
| 81 | + print("The push status is: \"\(pushStatus)\"") |
| 82 | + case .failure(let error): |
| 83 | + print("Couldn't fetch push status: \(error)") |
| 84 | + } |
| 85 | +} |
| 86 | + |
| 87 | +/*: |
| 88 | + Lets create another Push, this time by incrementing the badge |
| 89 | + and using channels instead of a query. |
| 90 | + */ |
| 91 | +let helloAgainAlert = ParsePushAppleAlert(body: "Hello from ParseSwift again!") |
| 92 | +let applePayload2 = ParsePushPayloadApple(alert: helloAgainAlert) |
| 93 | + .incrementBadge() |
| 94 | + |
| 95 | +var push2 = ParsePush(payload: applePayload2) |
| 96 | +//: Set all channels the notificatioin should be published to. |
| 97 | +push2.channels = Set(["newDevices"]) |
| 98 | + |
| 99 | +//: You can send the push notification whenever you are ready. |
| 100 | +push2.send { result in |
| 101 | + switch result { |
| 102 | + case .success(let statusId): |
| 103 | + print("The push was created with id: \"\(statusId)\"") |
| 104 | + //: Update the stored property with the lastest status id. |
| 105 | + pushStatusId = statusId |
| 106 | + case .failure(let error): |
| 107 | + print("Couldn't create push: \(error)") |
| 108 | + } |
| 109 | +} |
| 110 | + |
| 111 | +/*: |
| 112 | + Similar to before, you can fetch the status of notificaiton |
| 113 | + if you know the id. |
| 114 | + */ |
| 115 | +push2.fetchStatus(pushStatusId) { result in |
| 116 | + switch result { |
| 117 | + case .success(let pushStatus): |
| 118 | + print("The push status is: \"\(pushStatus)\"") |
| 119 | + case .failure(let error): |
| 120 | + print("Couldn't fetch push status: \(error)") |
| 121 | + } |
| 122 | +} |
| 123 | + |
| 124 | +/*: |
| 125 | + You can also send push notifications using Firebase Cloud Messanger. |
| 126 | + */ |
| 127 | +let helloNotification = ParsePushFirebaseNotification(body: "Hello from ParseSwift using FCM!") |
| 128 | +let firebasePayload = ParsePushPayloadFirebase(notification: helloNotification) |
| 129 | + |
| 130 | +let push3 = ParsePush(payload: firebasePayload, query: installationQuery) |
| 131 | + |
| 132 | +//: You can send the push notification whenever you are ready. |
| 133 | +push3.send { result in |
| 134 | + switch result { |
| 135 | + case .success(let statusId): |
| 136 | + print("The Firebase push was created with id: \"\(statusId)\"") |
| 137 | + //: Update the stored property with the lastest status id. |
| 138 | + pushStatusId = statusId |
| 139 | + case .failure(let error): |
| 140 | + print("Couldn't create push: \(error)") |
| 141 | + } |
| 142 | +} |
| 143 | + |
| 144 | +/*: |
| 145 | + Similar to before, you can fetch the status of notificaiton |
| 146 | + if you know the id. |
| 147 | + */ |
| 148 | +push3.fetchStatus(pushStatusId) { result in |
| 149 | + switch result { |
| 150 | + case .success(let pushStatus): |
| 151 | + print("The Firebase push status is: \"\(pushStatus)\"") |
| 152 | + case .failure(let error): |
| 153 | + print("Couldn't fetch push status: \(error)") |
| 154 | + } |
| 155 | +} |
| 156 | + |
| 157 | +/*: |
| 158 | + If you have a mixed push environment and are querying |
| 159 | + multiple ParsePushStatus's you will can use the any |
| 160 | + payload, `ParsePushPayloadAny`. |
| 161 | + */ |
| 162 | +let query = ParsePushStatus<ParsePushPayloadAny> |
| 163 | + .query(isNotNull(key: "objectId")) |
| 164 | + |
| 165 | +/*: |
| 166 | + Be sure to add the `.userMasterKey option when doing |
| 167 | + anything with `ParsePushStatus` directly. |
| 168 | +*/ |
| 169 | +query.findAll(options: [.useMasterKey]) { result in |
| 170 | + switch result { |
| 171 | + case .success(let pushStatus): |
| 172 | + print("All matching statuses: \"\(pushStatus)\"") |
| 173 | + case .failure(let error): |
| 174 | + print("Couldn't perform query: \(error)") |
| 175 | + } |
| 176 | +} |
| 177 | + |
| 178 | +PlaygroundPage.current.finishExecution() |
| 179 | +//: [Next](@next) |
0 commit comments