Skip to content

Commit

Permalink
1) Only display local notifications if using old version of OctoPod p…
Browse files Browse the repository at this point in the history
…lugin or plugin is not installed. Send language and printer name configuration to plugin that is now required for new version of plugin. 2) Getting reading for release 2.5

Closes #196
  • Loading branch information
gdombiak committed Jun 26, 2019
1 parent de908e0 commit 64ad598
Show file tree
Hide file tree
Showing 18 changed files with 429 additions and 34 deletions.
4 changes: 2 additions & 2 deletions OctoPod Intent/Info.plist
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@
<key>CFBundlePackageType</key>
<string>XPC!</string>
<key>CFBundleShortVersionString</key>
<string>2.4</string>
<string>2.5</string>
<key>CFBundleVersion</key>
<string>2</string>
<string>1</string>
<key>NSAppTransportSecurity</key>
<dict>
<key>NSAllowsArbitraryLoads</key>
Expand Down
36 changes: 36 additions & 0 deletions OctoPod Notification Extension/Info.plist
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>CFBundleDevelopmentRegion</key>
<string>$(DEVELOPMENT_LANGUAGE)</string>
<key>CFBundleDisplayName</key>
<string>OctoPod Notification Extension</string>
<key>CFBundleExecutable</key>
<string>$(EXECUTABLE_NAME)</string>
<key>CFBundleIdentifier</key>
<string>$(PRODUCT_BUNDLE_IDENTIFIER)</string>
<key>CFBundleInfoDictionaryVersion</key>
<string>6.0</string>
<key>CFBundleName</key>
<string>$(PRODUCT_NAME)</string>
<key>CFBundlePackageType</key>
<string>XPC!</string>
<key>CFBundleShortVersionString</key>
<string>2.5</string>
<key>CFBundleVersion</key>
<string>1</string>
<key>NSAppTransportSecurity</key>
<dict>
<key>NSAllowsArbitraryLoads</key>
<true/>
</dict>
<key>NSExtension</key>
<dict>
<key>NSExtensionPointIdentifier</key>
<string>com.apple.usernotifications.service</string>
<key>NSExtensionPrincipalClass</key>
<string>$(PRODUCT_MODULE_NAME).NotificationService</string>
</dict>
</dict>
</plist>
58 changes: 58 additions & 0 deletions OctoPod Notification Extension/NotificationService.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
import UserNotifications

/**
Extension used when OctoPod plugin for OctoPrint sends a push notification that includes an image. This extension will fetch the image and add
it as an attachment to the received notification. iOS will display the notification including the fetched image.
*/
class NotificationService: UNNotificationServiceExtension {

var contentHandler: ((UNNotificationContent) -> Void)?
var bestAttemptContent: UNMutableNotificationContent?

override func didReceive(_ request: UNNotificationRequest, withContentHandler contentHandler: @escaping (UNNotificationContent) -> Void) {
self.contentHandler = contentHandler
bestAttemptContent = (request.content.mutableCopy() as? UNMutableNotificationContent)

if let bestAttemptContent = bestAttemptContent {
// Fetch image from provided URL and add it as an attachment to the notification that iOS will display
if let url = request.content.userInfo["media-url"] as? String, let fetchURL = URL(string: url) {
do {
let imageData = try Data(contentsOf: fetchURL)
if let attachment = self.saveImageToDisk(data: imageData, options: nil) {
bestAttemptContent.attachments = [attachment]
}
} catch let error {
NSLog("Error fetching image from provided URL: \(error)")
}
}

contentHandler(bestAttemptContent)
}
}

override func serviceExtensionTimeWillExpire() {
// Called just before the extension will be terminated by the system.
// Use this as an opportunity to deliver your "best attempt" at modified content, otherwise the original push payload will be used.
if let contentHandler = contentHandler, let bestAttemptContent = bestAttemptContent {
contentHandler(bestAttemptContent)
}
}

// MARK: - Private functions

fileprivate func saveImageToDisk(data: Data, options: [NSObject : AnyObject]?) -> UNNotificationAttachment? {
let directory = URL(fileURLWithPath: NSTemporaryDirectory()).appendingPathComponent(ProcessInfo.processInfo.globallyUniqueString, isDirectory: true)
let fileIdentifier = "image.jpg"
do {
try FileManager.default.createDirectory(at: directory, withIntermediateDirectories: true, attributes: nil)
let fileURL = directory.appendingPathComponent(fileIdentifier)
try data.write(to: fileURL, options: [])
return try UNNotificationAttachment(identifier: fileIdentifier, url: fileURL, options: options)
} catch let error {
NSLog("Error creating attachment from image: \(error)")
}

return nil
}

}
4 changes: 2 additions & 2 deletions OctoPod Watch Extension/Info.plist
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@
<key>CFBundlePackageType</key>
<string>XPC!</string>
<key>CFBundleShortVersionString</key>
<string>2.4</string>
<string>2.5</string>
<key>CFBundleVersion</key>
<string>2</string>
<string>1</string>
<key>CLKComplicationPrincipalClass</key>
<string>$(PRODUCT_MODULE_NAME).ComplicationController</string>
<key>CLKComplicationSupportedFamilies</key>
Expand Down
4 changes: 2 additions & 2 deletions OctoPod Watch/Info.plist
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@
<key>CFBundlePackageType</key>
<string>APPL</string>
<key>CFBundleShortVersionString</key>
<string>2.4</string>
<string>2.5</string>
<key>CFBundleVersion</key>
<string>2</string>
<string>1</string>
<key>UISupportedInterfaceOrientations</key>
<array>
<string>UIInterfaceOrientationPortrait</string>
Expand Down
4 changes: 2 additions & 2 deletions OctoPod Widget/Info.plist
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@
<key>CFBundlePackageType</key>
<string>XPC!</string>
<key>CFBundleShortVersionString</key>
<string>2.4</string>
<string>2.5</string>
<key>CFBundleVersion</key>
<string>2</string>
<string>1</string>
<key>NSAppTransportSecurity</key>
<dict>
<key>NSAllowsArbitraryLoads</key>
Expand Down
Loading

0 comments on commit 64ad598

Please sign in to comment.