-
-
Notifications
You must be signed in to change notification settings - Fork 42
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
1) Only display local notifications if using old version of OctoPod p…
…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
Showing
18 changed files
with
429 additions
and
34 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.