├── .gitignore ├── LICENSE ├── NotificationsExample.xcworkspace └── contents.xcworkspacedata ├── Package.swift ├── README.md ├── Sources └── main.swift └── notificationstest ├── NotificationsTestApp.xcodeproj └── project.pbxproj └── notificationstest ├── AppDelegate.swift ├── Assets.xcassets └── AppIcon.appiconset │ └── Contents.json ├── Base.lproj ├── LaunchScreen.storyboard └── Main.storyboard ├── Info.plist ├── ViewController.swift └── notificationstest.entitlements /.gitignore: -------------------------------------------------------------------------------- 1 | # Xcode 2 | # 3 | # gitignore contributors: remember to update Global/Xcode.gitignore, Objective-C.gitignore & Swift.gitignore 4 | 5 | ## Build generated 6 | build/ 7 | DerivedData/ 8 | 9 | ## Various settings 10 | *.pbxuser 11 | !default.pbxuser 12 | *.mode1v3 13 | !default.mode1v3 14 | *.mode2v3 15 | !default.mode2v3 16 | *.perspectivev3 17 | !default.perspectivev3 18 | xcuserdata/ 19 | 20 | ## Other 21 | *.moved-aside 22 | *.xcuserstate 23 | 24 | ## Obj-C/Swift specific 25 | *.hmap 26 | *.ipa 27 | *.dSYM.zip 28 | *.dSYM 29 | 30 | ## Playgrounds 31 | timeline.xctimeline 32 | playground.xcworkspace 33 | 34 | # Swift Package Manager 35 | # 36 | # Add this line if you want to avoid checking in source code from Swift Package Manager dependencies. 37 | # Packages/ 38 | .build/ 39 | 40 | # CocoaPods 41 | # 42 | # We recommend against adding the Pods directory to your .gitignore. However 43 | # you should judge for yourself, the pros and cons are mentioned at: 44 | # https://guides.cocoapods.org/using/using-cocoapods.html#should-i-check-the-pods-directory-into-source-control 45 | # 46 | # Pods/ 47 | 48 | # Carthage 49 | # 50 | # Add this line if you want to avoid checking in source code from Carthage dependencies. 51 | # Carthage/Checkouts 52 | 53 | Carthage/Build 54 | 55 | # fastlane 56 | # 57 | # It is recommended to not store the screenshots in the git repo. Instead, use fastlane to re-generate the 58 | # screenshots whenever they are needed. 59 | # For more information about the recommended setup visit: 60 | # https://github.com/fastlane/fastlane/blob/master/fastlane/docs/Gitignore.md 61 | 62 | fastlane/report.xml 63 | fastlane/Preview.html 64 | fastlane/screenshots 65 | fastlane/test_output 66 | 67 | .DS_Store 68 | *.p8 69 | *.pem 70 | *.cer 71 | *.p12 72 | Packages/ 73 | buildlinux 74 | Perfect-NotificationsExample.xcodeproj/ 75 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Apache License 2 | Version 2.0, January 2004 3 | http://www.apache.org/licenses/ 4 | 5 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 6 | 7 | 1. Definitions. 8 | 9 | "License" shall mean the terms and conditions for use, reproduction, 10 | and distribution as defined by Sections 1 through 9 of this document. 11 | 12 | "Licensor" shall mean the copyright owner or entity authorized by 13 | the copyright owner that is granting the License. 14 | 15 | "Legal Entity" shall mean the union of the acting entity and all 16 | other entities that control, are controlled by, or are under common 17 | control with that entity. For the purposes of this definition, 18 | "control" means (i) the power, direct or indirect, to cause the 19 | direction or management of such entity, whether by contract or 20 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 21 | outstanding shares, or (iii) beneficial ownership of such entity. 22 | 23 | "You" (or "Your") shall mean an individual or Legal Entity 24 | exercising permissions granted by this License. 25 | 26 | "Source" form shall mean the preferred form for making modifications, 27 | including but not limited to software source code, documentation 28 | source, and configuration files. 29 | 30 | "Object" form shall mean any form resulting from mechanical 31 | transformation or translation of a Source form, including but 32 | not limited to compiled object code, generated documentation, 33 | and conversions to other media types. 34 | 35 | "Work" shall mean the work of authorship, whether in Source or 36 | Object form, made available under the License, as indicated by a 37 | copyright notice that is included in or attached to the work 38 | (an example is provided in the Appendix below). 39 | 40 | "Derivative Works" shall mean any work, whether in Source or Object 41 | form, that is based on (or derived from) the Work and for which the 42 | editorial revisions, annotations, elaborations, or other modifications 43 | represent, as a whole, an original work of authorship. For the purposes 44 | of this License, Derivative Works shall not include works that remain 45 | separable from, or merely link (or bind by name) to the interfaces of, 46 | the Work and Derivative Works thereof. 47 | 48 | "Contribution" shall mean any work of authorship, including 49 | the original version of the Work and any modifications or additions 50 | to that Work or Derivative Works thereof, that is intentionally 51 | submitted to Licensor for inclusion in the Work by the copyright owner 52 | or by an individual or Legal Entity authorized to submit on behalf of 53 | the copyright owner. For the purposes of this definition, "submitted" 54 | means any form of electronic, verbal, or written communication sent 55 | to the Licensor or its representatives, including but not limited to 56 | communication on electronic mailing lists, source code control systems, 57 | and issue tracking systems that are managed by, or on behalf of, the 58 | Licensor for the purpose of discussing and improving the Work, but 59 | excluding communication that is conspicuously marked or otherwise 60 | designated in writing by the copyright owner as "Not a Contribution." 61 | 62 | "Contributor" shall mean Licensor and any individual or Legal Entity 63 | on behalf of whom a Contribution has been received by Licensor and 64 | subsequently incorporated within the Work. 65 | 66 | 2. Grant of Copyright License. Subject to the terms and conditions of 67 | this License, each Contributor hereby grants to You a perpetual, 68 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 69 | copyright license to reproduce, prepare Derivative Works of, 70 | publicly display, publicly perform, sublicense, and distribute the 71 | Work and such Derivative Works in Source or Object form. 72 | 73 | 3. Grant of Patent License. Subject to the terms and conditions of 74 | this License, each Contributor hereby grants to You a perpetual, 75 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 76 | (except as stated in this section) patent license to make, have made, 77 | use, offer to sell, sell, import, and otherwise transfer the Work, 78 | where such license applies only to those patent claims licensable 79 | by such Contributor that are necessarily infringed by their 80 | Contribution(s) alone or by combination of their Contribution(s) 81 | with the Work to which such Contribution(s) was submitted. If You 82 | institute patent litigation against any entity (including a 83 | cross-claim or counterclaim in a lawsuit) alleging that the Work 84 | or a Contribution incorporated within the Work constitutes direct 85 | or contributory patent infringement, then any patent licenses 86 | granted to You under this License for that Work shall terminate 87 | as of the date such litigation is filed. 88 | 89 | 4. Redistribution. You may reproduce and distribute copies of the 90 | Work or Derivative Works thereof in any medium, with or without 91 | modifications, and in Source or Object form, provided that You 92 | meet the following conditions: 93 | 94 | (a) You must give any other recipients of the Work or 95 | Derivative Works a copy of this License; and 96 | 97 | (b) You must cause any modified files to carry prominent notices 98 | stating that You changed the files; and 99 | 100 | (c) You must retain, in the Source form of any Derivative Works 101 | that You distribute, all copyright, patent, trademark, and 102 | attribution notices from the Source form of the Work, 103 | excluding those notices that do not pertain to any part of 104 | the Derivative Works; and 105 | 106 | (d) If the Work includes a "NOTICE" text file as part of its 107 | distribution, then any Derivative Works that You distribute must 108 | include a readable copy of the attribution notices contained 109 | within such NOTICE file, excluding those notices that do not 110 | pertain to any part of the Derivative Works, in at least one 111 | of the following places: within a NOTICE text file distributed 112 | as part of the Derivative Works; within the Source form or 113 | documentation, if provided along with the Derivative Works; or, 114 | within a display generated by the Derivative Works, if and 115 | wherever such third-party notices normally appear. The contents 116 | of the NOTICE file are for informational purposes only and 117 | do not modify the License. You may add Your own attribution 118 | notices within Derivative Works that You distribute, alongside 119 | or as an addendum to the NOTICE text from the Work, provided 120 | that such additional attribution notices cannot be construed 121 | as modifying the License. 122 | 123 | You may add Your own copyright statement to Your modifications and 124 | may provide additional or different license terms and conditions 125 | for use, reproduction, or distribution of Your modifications, or 126 | for any such Derivative Works as a whole, provided Your use, 127 | reproduction, and distribution of the Work otherwise complies with 128 | the conditions stated in this License. 129 | 130 | 5. Submission of Contributions. Unless You explicitly state otherwise, 131 | any Contribution intentionally submitted for inclusion in the Work 132 | by You to the Licensor shall be under the terms and conditions of 133 | this License, without any additional terms or conditions. 134 | Notwithstanding the above, nothing herein shall supersede or modify 135 | the terms of any separate license agreement you may have executed 136 | with Licensor regarding such Contributions. 137 | 138 | 6. Trademarks. This License does not grant permission to use the trade 139 | names, trademarks, service marks, or product names of the Licensor, 140 | except as required for reasonable and customary use in describing the 141 | origin of the Work and reproducing the content of the NOTICE file. 142 | 143 | 7. Disclaimer of Warranty. Unless required by applicable law or 144 | agreed to in writing, Licensor provides the Work (and each 145 | Contributor provides its Contributions) on an "AS IS" BASIS, 146 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 147 | implied, including, without limitation, any warranties or conditions 148 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 149 | PARTICULAR PURPOSE. You are solely responsible for determining the 150 | appropriateness of using or redistributing the Work and assume any 151 | risks associated with Your exercise of permissions under this License. 152 | 153 | 8. Limitation of Liability. In no event and under no legal theory, 154 | whether in tort (including negligence), contract, or otherwise, 155 | unless required by applicable law (such as deliberate and grossly 156 | negligent acts) or agreed to in writing, shall any Contributor be 157 | liable to You for damages, including any direct, indirect, special, 158 | incidental, or consequential damages of any character arising as a 159 | result of this License or out of the use or inability to use the 160 | Work (including but not limited to damages for loss of goodwill, 161 | work stoppage, computer failure or malfunction, or any and all 162 | other commercial damages or losses), even if such Contributor 163 | has been advised of the possibility of such damages. 164 | 165 | 9. Accepting Warranty or Additional Liability. While redistributing 166 | the Work or Derivative Works thereof, You may choose to offer, 167 | and charge a fee for, acceptance of support, warranty, indemnity, 168 | or other liability obligations and/or rights consistent with this 169 | License. However, in accepting such obligations, You may act only 170 | on Your own behalf and on Your sole responsibility, not on behalf 171 | of any other Contributor, and only if You agree to indemnify, 172 | defend, and hold each Contributor harmless for any liability 173 | incurred by, or claims asserted against, such Contributor by reason 174 | of your accepting any such warranty or additional liability. 175 | 176 | END OF TERMS AND CONDITIONS 177 | 178 | APPENDIX: How to apply the Apache License to your work. 179 | 180 | To apply the Apache License to your work, attach the following 181 | boilerplate notice, with the fields enclosed by brackets "{}" 182 | replaced with your own identifying information. (Don't include 183 | the brackets!) The text should be enclosed in the appropriate 184 | comment syntax for the file format. We also recommend that a 185 | file or class name and description of purpose be included on the 186 | same "printed page" as the copyright notice for easier 187 | identification within third-party archives. 188 | 189 | Copyright {yyyy} {name of copyright owner} 190 | 191 | Licensed under the Apache License, Version 2.0 (the "License"); 192 | you may not use this file except in compliance with the License. 193 | You may obtain a copy of the License at 194 | 195 | http://www.apache.org/licenses/LICENSE-2.0 196 | 197 | Unless required by applicable law or agreed to in writing, software 198 | distributed under the License is distributed on an "AS IS" BASIS, 199 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 200 | See the License for the specific language governing permissions and 201 | limitations under the License. 202 | -------------------------------------------------------------------------------- /NotificationsExample.xcworkspace/contents.xcworkspacedata: -------------------------------------------------------------------------------- 1 | 2 | 4 | 6 | 7 | 9 | 10 | 11 | -------------------------------------------------------------------------------- /Package.swift: -------------------------------------------------------------------------------- 1 | // Generated automatically by Perfect Assistant Application 2 | // Date: 2016-09-29 23:32:34 +0000 3 | import PackageDescription 4 | let package = Package( 5 | name: "Perfect-NotificationsExample", 6 | targets: [], 7 | dependencies: [ 8 | Package.Dependency.Package(url: "https://github.com/PerfectlySoft/Perfect-HTTP.git", majorVersion: 2), 9 | Package.Dependency.Package(url: "https://github.com/PerfectlySoft/Perfect-HTTPServer.git", majorVersion: 2), 10 | Package.Dependency.Package(url: "https://github.com/PerfectlySoft/Perfect-Notifications.git", majorVersion: 2) 11 | ] 12 | ) 13 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Perfect Notifications Example 2 | 3 |

4 | 5 | Get Involed with Perfect! 6 | 7 |

8 | 9 |

10 | 11 | Star Perfect On Github 12 | 13 | 14 | Stack Overflow 15 | 16 | 17 | Follow Perfect on Twitter 18 | 19 | 20 | Join the Perfect Slack 21 | 22 |

23 | 24 |

25 | 26 | Swift 3.0 27 | 28 | 29 | Platforms OS X | Linux 30 | 31 | 32 | License Apache 33 | 34 | 35 | PerfectlySoft Twitter 36 | 37 | 38 | Slack Status 39 | 40 |

41 | 42 | ## Perfect Notifications Example 43 | 44 | This example shows how to use the [Perfect Notifications](https://github.com/PerfectlySoft/Perfect-Notifications) package to send remote notifications to an iOS device. The example consists of a simple iOS app and a Perfect HTTP server combination which illustrate the following concepts: 45 | 46 | * Registering for notifications in iOS 47 | * Sending obtained device id to the server 48 | * Configuring the server to send notifications 49 | * Creating an API endpoint to receive device ids 50 | * Creating an API endpoint to trigger notifications to all known devices 51 | 52 | ## Compatibility with Swift 53 | 54 | This project builds with **Xcode 8.2**. The server itself can be built on macOS or with the **Swift 3.0.2** toolchain on Ubuntu. 55 | 56 | ## Building & Running 57 | 58 | Both the client and server project can be opened through the "NotificationsExample.xcworkspace" file. First, however, the server project must be generated. 59 | 60 | In a terminal, cd to the location where you cloned this project and enter: 61 | 62 | ``` 63 | swift package generate-xcodeproj 64 | ``` 65 | 66 | This will generate the server project. Now open the "NotificationsExample.xcworkspace" Xcode workspace. This workspace contains both the client and the server projects. The client is named "NotificationsTestApp" and the server is "Perfect-NotificationsExample". 67 | 68 | ### Configuration 69 | 70 | Before you can build and run either app you must obtain an "APNs Auth Key" from Apple as described here: [Obtain APNs Auth Key](https://github.com/PerfectlySoft/Perfect-Notifications/blob/master/README.md#obtain-apns-auth-key). Once you have your private key file, the key id, team id and your app id you can use these properties to configure the example. 71 | 72 | It's important to note that APNs notifications do not work when running apps in the device simulator. You must run the app on a physical device. 73 | 74 | You **must** configure the client project with the following: 75 | 76 | 1. Bundle Identifier 77 | * This id must be obtained through Apple's developer portal. 78 | 2. Team 79 | * You must select the team. 80 | 3. The IP address where the server is running. 81 | * You will find the place to enter this address in `AppDelegate.swift`. 82 | * This will generally be your local network Mac IP address. An address such as 127.0.0.1 or 0.0.0.0 will **not** work. 83 | 84 | In the server project's `main.swift` file, you will need to enter your app id, team id, key id and the name of the private key file. By default the key files are named "APNSAuthKey_\(apnsKeyIdentifier).p8", where `apnsKeyIdentifier` is the key id value. 85 | 86 | After configuring these values the server and client projects should each compile. 87 | 88 | This example assumes that your "p8" private key file is located in your project directory. Use a full complete path to this file if it is located at any other path or if you do not want to set your Xcode debug working directory as described below. 89 | 90 | ### Running 91 | 92 | Before running either app you should set the Xcode debug working directory to the location of your project. This allows the server to properly locate your "p8" private key file and any other files which are in the project directory. Ensure the server target is selected in the Xcode targets popup and press shift-cmd-comma. This will bring up the Xcode scheme editor. With the "Run" and "Options" tabs selected, click "Use custom working directory" and select the path to your project folder. 93 | 94 | Switch the Xcode target to the iOS client app. Build and load the app onto your test iOS device. You shuld see a "Test Notification" button the the center view (you may also get an error message if you server is not yet running, which can be ignored). 95 | 96 | Now switch back to the server target and click run in Xcode. This will launch the server in the debugger. You can view server output in the Xcode debugger output view. 97 | 98 | ## Testing Functionality 99 | 100 | With the client iOS app properly configured and running in a physical device and the server configured and running on your Mac, press the iOS app's "Test Notifications" button. You should see your app register with the server and then trigger a notification. The notification should appear on the device and the server will output the responses from APNs. 101 | 102 | For extra bonus points load the app on multiple devices and have them spam each other with notifications. 103 | 104 | ## Issues 105 | 106 | We are transitioning to using JIRA for all bugs and support related issues, therefore the GitHub issues has been disabled. 107 | 108 | If you find a mistake, bug, or any other helpful suggestion you'd like to make on the docs please head over to [http://jira.perfect.org:8080/servicedesk/customer/portal/1](http://jira.perfect.org:8080/servicedesk/customer/portal/1) and raise it. 109 | 110 | A comprehensive list of open issues can be found at [http://jira.perfect.org:8080/projects/ISS/issues](http://jira.perfect.org:8080/projects/ISS/issues) 111 | 112 | 113 | 114 | ## Further Information 115 | For more information on the Perfect project, please visit [perfect.org](http://perfect.org). 116 | -------------------------------------------------------------------------------- /Sources/main.swift: -------------------------------------------------------------------------------- 1 | // 2 | // main.swift 3 | // Perfect-NotificationsExample 4 | // 5 | // Created by Kyle Jessup on 2017-02-01. 6 | // Copyright (C) 2017 PerfectlySoft, Inc. 7 | // 8 | //===----------------------------------------------------------------------===// 9 | // 10 | // This source file is part of the Perfect.org open source project 11 | // 12 | // Copyright (c) 2015 - 2017 PerfectlySoft Inc. and the Perfect project authors 13 | // Licensed under Apache License v2.0 14 | // 15 | // See http://perfect.org/licensing.html for license information 16 | // 17 | //===----------------------------------------------------------------------===// 18 | // 19 | 20 | import PerfectNotifications 21 | import PerfectHTTPServer 22 | import PerfectHTTP 23 | 24 | // This is the App id, the APNS topic and we use it as the NotificationPusher configuration key 25 | let notificationsTestId = "your.app.id" 26 | 27 | let apnsTeamIdentifier = "YOUR TEAM ID" 28 | let apnsKeyIdentifier = "YOUR KEY ID" 29 | let apnsPrivateKey = "APNSAuthKey_\(apnsKeyIdentifier).p8" 30 | 31 | NotificationPusher.addConfigurationAPNS(name: notificationsTestId, 32 | production: false, 33 | keyId: apnsKeyIdentifier, 34 | teamId: apnsTeamIdentifier, 35 | privateKeyPath: apnsPrivateKey) 36 | 37 | class NotificationsExample { 38 | var deviceIds = [String]() 39 | 40 | func receiveDeviceId(request: HTTPRequest, response: HTTPResponse) { 41 | guard let deviceId = request.urlVariables["deviceid"] else { 42 | response.status = .badRequest 43 | return response.completed() 44 | } 45 | print("Adding device id:\(deviceId)") 46 | if !deviceIds.contains(deviceId) { 47 | deviceIds.append(deviceId) 48 | } 49 | try? response.setBody(json: [:]).completed() 50 | } 51 | 52 | func listDeviceIds(request: HTTPRequest, response: HTTPResponse) { 53 | try? response.setBody(json: ["deviceIds":self.deviceIds]).completed() 54 | } 55 | 56 | func notifyDevices(request: HTTPRequest, response: HTTPResponse) { 57 | print("Sending notification to all devices: \(deviceIds)") 58 | NotificationPusher(apnsTopic: notificationsTestId) 59 | .pushAPNS(configurationName: notificationsTestId, 60 | deviceTokens: deviceIds, 61 | notificationItems: [ 62 | .alertBody("Hello!"), 63 | .sound("default")]) { 64 | responses in 65 | print("\(responses)") 66 | response.completed() 67 | } 68 | } 69 | } 70 | 71 | var example = NotificationsExample() 72 | 73 | let routes = [ 74 | Route(method: .post, uri: "/id/add/{deviceid}", handler: example.receiveDeviceId), 75 | Route(method: .post, uri: "/id/notify", handler: example.notifyDevices), 76 | Route(method: .get, uri: "/id/list", handler: example.listDeviceIds) 77 | ] 78 | 79 | do { 80 | // Launch the HTTP server 81 | try HTTPServer.launch(name: "localhost", port: 8181, routes: routes) 82 | } catch { 83 | print("Unknown error thrown: \(error)") 84 | } 85 | -------------------------------------------------------------------------------- /notificationstest/NotificationsTestApp.xcodeproj/project.pbxproj: -------------------------------------------------------------------------------- 1 | // !$*UTF8*$! 2 | { 3 | archiveVersion = 1; 4 | classes = { 5 | }; 6 | objectVersion = 46; 7 | objects = { 8 | 9 | /* Begin PBXBuildFile section */ 10 | F89C53D01E3A900F0079CE70 /* AppDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = F89C53CF1E3A900F0079CE70 /* AppDelegate.swift */; }; 11 | F89C53D21E3A900F0079CE70 /* ViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = F89C53D11E3A900F0079CE70 /* ViewController.swift */; }; 12 | F89C53D51E3A900F0079CE70 /* Main.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = F89C53D31E3A900F0079CE70 /* Main.storyboard */; }; 13 | F89C53D71E3A900F0079CE70 /* Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = F89C53D61E3A900F0079CE70 /* Assets.xcassets */; }; 14 | F89C53DA1E3A900F0079CE70 /* LaunchScreen.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = F89C53D81E3A900F0079CE70 /* LaunchScreen.storyboard */; }; 15 | /* End PBXBuildFile section */ 16 | 17 | /* Begin PBXFileReference section */ 18 | F89C53CC1E3A900F0079CE70 /* NotificationsTestApp.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = NotificationsTestApp.app; sourceTree = BUILT_PRODUCTS_DIR; }; 19 | F89C53CF1E3A900F0079CE70 /* AppDelegate.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AppDelegate.swift; sourceTree = ""; }; 20 | F89C53D11E3A900F0079CE70 /* ViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ViewController.swift; sourceTree = ""; }; 21 | F89C53D41E3A900F0079CE70 /* Base */ = {isa = PBXFileReference; lastKnownFileType = file.storyboard; name = Base; path = Base.lproj/Main.storyboard; sourceTree = ""; }; 22 | F89C53D61E3A900F0079CE70 /* Assets.xcassets */ = {isa = PBXFileReference; lastKnownFileType = folder.assetcatalog; path = Assets.xcassets; sourceTree = ""; }; 23 | F89C53D91E3A900F0079CE70 /* Base */ = {isa = PBXFileReference; lastKnownFileType = file.storyboard; name = Base; path = Base.lproj/LaunchScreen.storyboard; sourceTree = ""; }; 24 | F89C53DB1E3A900F0079CE70 /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = ""; }; 25 | F8C9139B1E4101DB00C3AF1A /* notificationstest.entitlements */ = {isa = PBXFileReference; lastKnownFileType = text.plist.entitlements; path = notificationstest.entitlements; sourceTree = ""; }; 26 | /* End PBXFileReference section */ 27 | 28 | /* Begin PBXFrameworksBuildPhase section */ 29 | F89C53C91E3A900F0079CE70 /* Frameworks */ = { 30 | isa = PBXFrameworksBuildPhase; 31 | buildActionMask = 2147483647; 32 | files = ( 33 | ); 34 | runOnlyForDeploymentPostprocessing = 0; 35 | }; 36 | /* End PBXFrameworksBuildPhase section */ 37 | 38 | /* Begin PBXGroup section */ 39 | F89C53C31E3A900F0079CE70 = { 40 | isa = PBXGroup; 41 | children = ( 42 | F89C53CE1E3A900F0079CE70 /* notificationstest */, 43 | F89C53CD1E3A900F0079CE70 /* Products */, 44 | ); 45 | sourceTree = ""; 46 | }; 47 | F89C53CD1E3A900F0079CE70 /* Products */ = { 48 | isa = PBXGroup; 49 | children = ( 50 | F89C53CC1E3A900F0079CE70 /* NotificationsTestApp.app */, 51 | ); 52 | name = Products; 53 | sourceTree = ""; 54 | }; 55 | F89C53CE1E3A900F0079CE70 /* notificationstest */ = { 56 | isa = PBXGroup; 57 | children = ( 58 | F8C9139B1E4101DB00C3AF1A /* notificationstest.entitlements */, 59 | F89C53CF1E3A900F0079CE70 /* AppDelegate.swift */, 60 | F89C53D11E3A900F0079CE70 /* ViewController.swift */, 61 | F89C53D31E3A900F0079CE70 /* Main.storyboard */, 62 | F89C53D61E3A900F0079CE70 /* Assets.xcassets */, 63 | F89C53D81E3A900F0079CE70 /* LaunchScreen.storyboard */, 64 | F89C53DB1E3A900F0079CE70 /* Info.plist */, 65 | ); 66 | path = notificationstest; 67 | sourceTree = ""; 68 | }; 69 | /* End PBXGroup section */ 70 | 71 | /* Begin PBXNativeTarget section */ 72 | F89C53CB1E3A900F0079CE70 /* NotificationsTestApp */ = { 73 | isa = PBXNativeTarget; 74 | buildConfigurationList = F89C53DE1E3A900F0079CE70 /* Build configuration list for PBXNativeTarget "NotificationsTestApp" */; 75 | buildPhases = ( 76 | F89C53C81E3A900F0079CE70 /* Sources */, 77 | F89C53C91E3A900F0079CE70 /* Frameworks */, 78 | F89C53CA1E3A900F0079CE70 /* Resources */, 79 | ); 80 | buildRules = ( 81 | ); 82 | dependencies = ( 83 | ); 84 | name = NotificationsTestApp; 85 | productName = notificationstest; 86 | productReference = F89C53CC1E3A900F0079CE70 /* NotificationsTestApp.app */; 87 | productType = "com.apple.product-type.application"; 88 | }; 89 | /* End PBXNativeTarget section */ 90 | 91 | /* Begin PBXProject section */ 92 | F89C53C41E3A900F0079CE70 /* Project object */ = { 93 | isa = PBXProject; 94 | attributes = { 95 | LastSwiftUpdateCheck = 0820; 96 | LastUpgradeCheck = 0820; 97 | ORGANIZATIONNAME = PerfectlySoft; 98 | TargetAttributes = { 99 | F89C53CB1E3A900F0079CE70 = { 100 | CreatedOnToolsVersion = 8.2.1; 101 | ProvisioningStyle = Automatic; 102 | SystemCapabilities = { 103 | com.apple.Push = { 104 | enabled = 1; 105 | }; 106 | }; 107 | }; 108 | }; 109 | }; 110 | buildConfigurationList = F89C53C71E3A900F0079CE70 /* Build configuration list for PBXProject "NotificationsTestApp" */; 111 | compatibilityVersion = "Xcode 3.2"; 112 | developmentRegion = English; 113 | hasScannedForEncodings = 0; 114 | knownRegions = ( 115 | en, 116 | Base, 117 | ); 118 | mainGroup = F89C53C31E3A900F0079CE70; 119 | productRefGroup = F89C53CD1E3A900F0079CE70 /* Products */; 120 | projectDirPath = ""; 121 | projectRoot = ""; 122 | targets = ( 123 | F89C53CB1E3A900F0079CE70 /* NotificationsTestApp */, 124 | ); 125 | }; 126 | /* End PBXProject section */ 127 | 128 | /* Begin PBXResourcesBuildPhase section */ 129 | F89C53CA1E3A900F0079CE70 /* Resources */ = { 130 | isa = PBXResourcesBuildPhase; 131 | buildActionMask = 2147483647; 132 | files = ( 133 | F89C53DA1E3A900F0079CE70 /* LaunchScreen.storyboard in Resources */, 134 | F89C53D71E3A900F0079CE70 /* Assets.xcassets in Resources */, 135 | F89C53D51E3A900F0079CE70 /* Main.storyboard in Resources */, 136 | ); 137 | runOnlyForDeploymentPostprocessing = 0; 138 | }; 139 | /* End PBXResourcesBuildPhase section */ 140 | 141 | /* Begin PBXSourcesBuildPhase section */ 142 | F89C53C81E3A900F0079CE70 /* Sources */ = { 143 | isa = PBXSourcesBuildPhase; 144 | buildActionMask = 2147483647; 145 | files = ( 146 | F89C53D21E3A900F0079CE70 /* ViewController.swift in Sources */, 147 | F89C53D01E3A900F0079CE70 /* AppDelegate.swift in Sources */, 148 | ); 149 | runOnlyForDeploymentPostprocessing = 0; 150 | }; 151 | /* End PBXSourcesBuildPhase section */ 152 | 153 | /* Begin PBXVariantGroup section */ 154 | F89C53D31E3A900F0079CE70 /* Main.storyboard */ = { 155 | isa = PBXVariantGroup; 156 | children = ( 157 | F89C53D41E3A900F0079CE70 /* Base */, 158 | ); 159 | name = Main.storyboard; 160 | sourceTree = ""; 161 | }; 162 | F89C53D81E3A900F0079CE70 /* LaunchScreen.storyboard */ = { 163 | isa = PBXVariantGroup; 164 | children = ( 165 | F89C53D91E3A900F0079CE70 /* Base */, 166 | ); 167 | name = LaunchScreen.storyboard; 168 | sourceTree = ""; 169 | }; 170 | /* End PBXVariantGroup section */ 171 | 172 | /* Begin XCBuildConfiguration section */ 173 | F89C53DC1E3A900F0079CE70 /* Debug */ = { 174 | isa = XCBuildConfiguration; 175 | buildSettings = { 176 | ALWAYS_SEARCH_USER_PATHS = NO; 177 | CLANG_ANALYZER_NONNULL = YES; 178 | CLANG_CXX_LANGUAGE_STANDARD = "gnu++0x"; 179 | CLANG_CXX_LIBRARY = "libc++"; 180 | CLANG_ENABLE_MODULES = YES; 181 | CLANG_ENABLE_OBJC_ARC = YES; 182 | CLANG_WARN_BOOL_CONVERSION = YES; 183 | CLANG_WARN_CONSTANT_CONVERSION = YES; 184 | CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR; 185 | CLANG_WARN_DOCUMENTATION_COMMENTS = YES; 186 | CLANG_WARN_EMPTY_BODY = YES; 187 | CLANG_WARN_ENUM_CONVERSION = YES; 188 | CLANG_WARN_INFINITE_RECURSION = YES; 189 | CLANG_WARN_INT_CONVERSION = YES; 190 | CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR; 191 | CLANG_WARN_SUSPICIOUS_MOVE = YES; 192 | CLANG_WARN_UNREACHABLE_CODE = YES; 193 | CLANG_WARN__DUPLICATE_METHOD_MATCH = YES; 194 | "CODE_SIGN_IDENTITY[sdk=iphoneos*]" = "iPhone Developer"; 195 | COPY_PHASE_STRIP = NO; 196 | DEBUG_INFORMATION_FORMAT = dwarf; 197 | ENABLE_STRICT_OBJC_MSGSEND = YES; 198 | ENABLE_TESTABILITY = YES; 199 | GCC_C_LANGUAGE_STANDARD = gnu99; 200 | GCC_DYNAMIC_NO_PIC = NO; 201 | GCC_NO_COMMON_BLOCKS = YES; 202 | GCC_OPTIMIZATION_LEVEL = 0; 203 | GCC_PREPROCESSOR_DEFINITIONS = ( 204 | "DEBUG=1", 205 | "$(inherited)", 206 | ); 207 | GCC_WARN_64_TO_32_BIT_CONVERSION = YES; 208 | GCC_WARN_ABOUT_RETURN_TYPE = YES_ERROR; 209 | GCC_WARN_UNDECLARED_SELECTOR = YES; 210 | GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE; 211 | GCC_WARN_UNUSED_FUNCTION = YES; 212 | GCC_WARN_UNUSED_VARIABLE = YES; 213 | IPHONEOS_DEPLOYMENT_TARGET = 10.2; 214 | MTL_ENABLE_DEBUG_INFO = YES; 215 | ONLY_ACTIVE_ARCH = YES; 216 | SDKROOT = iphoneos; 217 | SWIFT_ACTIVE_COMPILATION_CONDITIONS = DEBUG; 218 | SWIFT_OPTIMIZATION_LEVEL = "-Onone"; 219 | TARGETED_DEVICE_FAMILY = "1,2"; 220 | }; 221 | name = Debug; 222 | }; 223 | F89C53DD1E3A900F0079CE70 /* Release */ = { 224 | isa = XCBuildConfiguration; 225 | buildSettings = { 226 | ALWAYS_SEARCH_USER_PATHS = NO; 227 | CLANG_ANALYZER_NONNULL = YES; 228 | CLANG_CXX_LANGUAGE_STANDARD = "gnu++0x"; 229 | CLANG_CXX_LIBRARY = "libc++"; 230 | CLANG_ENABLE_MODULES = YES; 231 | CLANG_ENABLE_OBJC_ARC = YES; 232 | CLANG_WARN_BOOL_CONVERSION = YES; 233 | CLANG_WARN_CONSTANT_CONVERSION = YES; 234 | CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR; 235 | CLANG_WARN_DOCUMENTATION_COMMENTS = YES; 236 | CLANG_WARN_EMPTY_BODY = YES; 237 | CLANG_WARN_ENUM_CONVERSION = YES; 238 | CLANG_WARN_INFINITE_RECURSION = YES; 239 | CLANG_WARN_INT_CONVERSION = YES; 240 | CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR; 241 | CLANG_WARN_SUSPICIOUS_MOVE = YES; 242 | CLANG_WARN_UNREACHABLE_CODE = YES; 243 | CLANG_WARN__DUPLICATE_METHOD_MATCH = YES; 244 | "CODE_SIGN_IDENTITY[sdk=iphoneos*]" = "iPhone Developer"; 245 | COPY_PHASE_STRIP = NO; 246 | DEBUG_INFORMATION_FORMAT = "dwarf-with-dsym"; 247 | ENABLE_NS_ASSERTIONS = NO; 248 | ENABLE_STRICT_OBJC_MSGSEND = YES; 249 | GCC_C_LANGUAGE_STANDARD = gnu99; 250 | GCC_NO_COMMON_BLOCKS = YES; 251 | GCC_WARN_64_TO_32_BIT_CONVERSION = YES; 252 | GCC_WARN_ABOUT_RETURN_TYPE = YES_ERROR; 253 | GCC_WARN_UNDECLARED_SELECTOR = YES; 254 | GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE; 255 | GCC_WARN_UNUSED_FUNCTION = YES; 256 | GCC_WARN_UNUSED_VARIABLE = YES; 257 | IPHONEOS_DEPLOYMENT_TARGET = 10.2; 258 | MTL_ENABLE_DEBUG_INFO = NO; 259 | SDKROOT = iphoneos; 260 | SWIFT_OPTIMIZATION_LEVEL = "-Owholemodule"; 261 | TARGETED_DEVICE_FAMILY = "1,2"; 262 | VALIDATE_PRODUCT = YES; 263 | }; 264 | name = Release; 265 | }; 266 | F89C53DF1E3A900F0079CE70 /* Debug */ = { 267 | isa = XCBuildConfiguration; 268 | buildSettings = { 269 | ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon; 270 | CODE_SIGN_ENTITLEMENTS = notificationstest/notificationstest.entitlements; 271 | DEVELOPMENT_TEAM = ""; 272 | INFOPLIST_FILE = notificationstest/Info.plist; 273 | LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks"; 274 | PRODUCT_BUNDLE_IDENTIFIER = your.app.id; 275 | PRODUCT_NAME = "$(TARGET_NAME)"; 276 | SWIFT_VERSION = 3.0; 277 | }; 278 | name = Debug; 279 | }; 280 | F89C53E01E3A900F0079CE70 /* Release */ = { 281 | isa = XCBuildConfiguration; 282 | buildSettings = { 283 | ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon; 284 | CODE_SIGN_ENTITLEMENTS = notificationstest/notificationstest.entitlements; 285 | DEVELOPMENT_TEAM = ""; 286 | INFOPLIST_FILE = notificationstest/Info.plist; 287 | LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks"; 288 | PRODUCT_BUNDLE_IDENTIFIER = your.app.id; 289 | PRODUCT_NAME = "$(TARGET_NAME)"; 290 | SWIFT_VERSION = 3.0; 291 | }; 292 | name = Release; 293 | }; 294 | /* End XCBuildConfiguration section */ 295 | 296 | /* Begin XCConfigurationList section */ 297 | F89C53C71E3A900F0079CE70 /* Build configuration list for PBXProject "NotificationsTestApp" */ = { 298 | isa = XCConfigurationList; 299 | buildConfigurations = ( 300 | F89C53DC1E3A900F0079CE70 /* Debug */, 301 | F89C53DD1E3A900F0079CE70 /* Release */, 302 | ); 303 | defaultConfigurationIsVisible = 0; 304 | defaultConfigurationName = Release; 305 | }; 306 | F89C53DE1E3A900F0079CE70 /* Build configuration list for PBXNativeTarget "NotificationsTestApp" */ = { 307 | isa = XCConfigurationList; 308 | buildConfigurations = ( 309 | F89C53DF1E3A900F0079CE70 /* Debug */, 310 | F89C53E01E3A900F0079CE70 /* Release */, 311 | ); 312 | defaultConfigurationIsVisible = 0; 313 | defaultConfigurationName = Release; 314 | }; 315 | /* End XCConfigurationList section */ 316 | }; 317 | rootObject = F89C53C41E3A900F0079CE70 /* Project object */; 318 | } 319 | -------------------------------------------------------------------------------- /notificationstest/notificationstest/AppDelegate.swift: -------------------------------------------------------------------------------- 1 | // 2 | // AppDelegate.swift 3 | // notificationstest 4 | // 5 | // Created by Kyle Jessup on 2017-01-26. 6 | // Copyright (C) 2017 PerfectlySoft, Inc. 7 | // 8 | //===----------------------------------------------------------------------===// 9 | // 10 | // This source file is part of the Perfect.org open source project 11 | // 12 | // Copyright (c) 2015 - 2017 PerfectlySoft Inc. and the Perfect project authors 13 | // Licensed under Apache License v2.0 14 | // 15 | // See http://perfect.org/licensing.html for license information 16 | // 17 | //===----------------------------------------------------------------------===// 18 | // 19 | 20 | import UIKit 21 | import UserNotifications 22 | 23 | let host = 24 | let port = 8181 // adjust the port if desired. it must match the server listen port. 25 | let apiAdd = "/id/add/" // + deviceID 26 | let apiList = "/id/list" 27 | let apiNotify = "/id/notify" 28 | 29 | @UIApplicationMain 30 | class AppDelegate: UIResponder, UIApplicationDelegate, UNUserNotificationCenterDelegate { 31 | 32 | static var myself: AppDelegate? 33 | 34 | var window: UIWindow? 35 | var deviceToken: Data? 36 | var urlTask: URLSessionDataTask? 37 | 38 | func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool { 39 | AppDelegate.myself = self 40 | 41 | let center = UNUserNotificationCenter.current() 42 | // ask to send notifications 43 | // you should answer YES on the test devices 44 | center.requestAuthorization(options: [.alert, .sound, .badge]) { 45 | granted, error in 46 | print("Requested permission for notifications: \(granted)") 47 | UIApplication.shared.registerForRemoteNotifications() 48 | center.delegate = self 49 | } 50 | return true 51 | } 52 | 53 | func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) { 54 | self.deviceToken = deviceToken 55 | // send your device id to the server 56 | sendAddDevice { 57 | } 58 | } 59 | 60 | func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Swift.Void) { 61 | completionHandler(.alert) 62 | } 63 | 64 | func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Swift.Void) { 65 | completionHandler() 66 | } 67 | 68 | // call the server endpoint which registers this device by its unique id 69 | func sendAddDevice(completion: @escaping () -> ()) { 70 | guard let deviceToken = deviceToken else { 71 | return 72 | } 73 | let hex = deviceToken.hexString 74 | let urlString = "http://\(host):\(port)\(apiAdd)\(hex)" 75 | guard let url = URL(string: urlString) else { 76 | return 77 | } 78 | var request = URLRequest(url: url) 79 | request.httpMethod = "POST" 80 | request.httpBody = nil 81 | urlTask = URLSession.shared.dataTask(with: request) { 82 | response, data, error in 83 | guard nil == error else { 84 | print("Error registering device id: \(error)") 85 | DispatchQueue.main.async { 86 | let alert = UIAlertController(title: "Error", message: "Error registering device id: \(error?.localizedDescription ?? "no msg")", preferredStyle: .alert) 87 | alert.addAction(UIAlertAction(title: "Bye!", style: .default, handler: { _ in exit(-1) })) 88 | self.window?.rootViewController?.present(alert, animated: true) 89 | } 90 | return 91 | } 92 | completion() 93 | } 94 | urlTask?.resume() 95 | } 96 | 97 | // call the server endpoint to trigger test notifications 98 | // a notification will be sent to all registered devices 99 | func sendNotification() -> Bool { 100 | sendAddDevice { // add our device again just to be sure at least one device is registered 101 | guard let url = URL(string: "http://\(host):\(port)\(apiNotify)") else { 102 | return 103 | } 104 | var request = URLRequest(url: url) 105 | request.httpMethod = "POST" 106 | request.httpBody = nil 107 | self.urlTask = URLSession.shared.dataTask(with: request) { 108 | response, data, error in 109 | guard nil == error else { 110 | print("Error requesting notification: \(error)") 111 | return 112 | } 113 | } 114 | self.urlTask?.resume() 115 | } 116 | return true 117 | } 118 | } 119 | 120 | // utility 121 | extension UInt8 { 122 | var hexString: String { 123 | var s = "" 124 | let b = self >> 4 125 | s.append(String(UnicodeScalar(b > 9 ? b - 10 + 65 : b + 48))) 126 | let b2 = self & 0x0F 127 | s.append(String(UnicodeScalar(b2 > 9 ? b2 - 10 + 65 : b2 + 48))) 128 | return s 129 | } 130 | } 131 | 132 | 133 | extension Data { 134 | var hexString: String { 135 | guard count > 0 else { 136 | return "" 137 | } 138 | let deviceIdLen = count 139 | let deviceIdBytes = self.withUnsafeBytes { 140 | ptr in 141 | return UnsafeBufferPointer(start: ptr, count: self.count) 142 | } 143 | var hexStr = "" 144 | for n in 0.. 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | -------------------------------------------------------------------------------- /notificationstest/notificationstest/Base.lproj/Main.storyboard: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | -------------------------------------------------------------------------------- /notificationstest/notificationstest/Info.plist: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | CFBundleDevelopmentRegion 6 | en 7 | CFBundleExecutable 8 | $(EXECUTABLE_NAME) 9 | CFBundleIdentifier 10 | $(PRODUCT_BUNDLE_IDENTIFIER) 11 | CFBundleInfoDictionaryVersion 12 | 6.0 13 | CFBundleName 14 | $(PRODUCT_NAME) 15 | CFBundlePackageType 16 | APPL 17 | CFBundleShortVersionString 18 | 1.0 19 | CFBundleVersion 20 | 1 21 | LSRequiresIPhoneOS 22 | 23 | NSAppTransportSecurity 24 | 25 | NSAllowsArbitraryLoads 26 | 27 | 28 | UILaunchStoryboardName 29 | LaunchScreen 30 | UIMainStoryboardFile 31 | Main 32 | UIRequiredDeviceCapabilities 33 | 34 | armv7 35 | 36 | UISupportedInterfaceOrientations 37 | 38 | UIInterfaceOrientationPortrait 39 | UIInterfaceOrientationLandscapeLeft 40 | UIInterfaceOrientationLandscapeRight 41 | 42 | UISupportedInterfaceOrientations~ipad 43 | 44 | UIInterfaceOrientationPortrait 45 | UIInterfaceOrientationPortraitUpsideDown 46 | UIInterfaceOrientationLandscapeLeft 47 | UIInterfaceOrientationLandscapeRight 48 | 49 | 50 | 51 | -------------------------------------------------------------------------------- /notificationstest/notificationstest/ViewController.swift: -------------------------------------------------------------------------------- 1 | // 2 | // ViewController.swift 3 | // notificationstest 4 | // 5 | // Created by Kyle Jessup on 2017-01-26. 6 | // Copyright © 2017 PerfectlySoft. All rights reserved. 7 | // 8 | 9 | import UIKit 10 | 11 | class ViewController: UIViewController { 12 | 13 | override func viewDidLoad() { 14 | super.viewDidLoad() 15 | // Do any additional setup after loading the view, typically from a nib. 16 | } 17 | 18 | override func didReceiveMemoryWarning() { 19 | super.didReceiveMemoryWarning() 20 | // Dispose of any resources that can be recreated. 21 | } 22 | 23 | @IBAction 24 | func sendNotification(_ sender: AnyObject?) { 25 | _ = AppDelegate.myself?.sendNotification() 26 | } 27 | } 28 | 29 | -------------------------------------------------------------------------------- /notificationstest/notificationstest/notificationstest.entitlements: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | aps-environment 6 | development 7 | 8 | 9 | --------------------------------------------------------------------------------