diff --git a/LocalNotifications_Over_iOS10.podspec b/LocalNotifications_Over_iOS10.podspec index 033ea82..6353c0d 100644 --- a/LocalNotifications_Over_iOS10.podspec +++ b/LocalNotifications_Over_iOS10.podspec @@ -16,7 +16,7 @@ Pod::Spec.new do |s| # s.name = "LocalNotifications_Over_iOS10" - s.version = "0.0.6" + s.version = "0.0.7" s.summary = "Local notifications scheduler in Swift compatible with over iOS10" # This description is used to generate tags and improve search results. diff --git a/LocalNotifications_Over_iOS10.xcodeproj/project.pbxproj b/LocalNotifications_Over_iOS10.xcodeproj/project.pbxproj index 1e36469..c14d342 100644 --- a/LocalNotifications_Over_iOS10.xcodeproj/project.pbxproj +++ b/LocalNotifications_Over_iOS10.xcodeproj/project.pbxproj @@ -17,6 +17,8 @@ 5F11D8D91F7A3D9200A43DF5 /* NotificationManager.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5F11D8D51F7A307700A43DF5 /* NotificationManager.swift */; }; 5F340ABD1F7A73DB00520C30 /* NotificationManagerTest.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5F340ABC1F7A73DB00520C30 /* NotificationManagerTest.swift */; }; 5F340ABF1F7A73F300520C30 /* NotificationObjectTest.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5F340ABE1F7A73F300520C30 /* NotificationObjectTest.swift */; }; + 5F5B1E9120A636EE00A8B95C /* UIHelper.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5F5B1E9020A636EE00A8B95C /* UIHelper.swift */; }; + 5F5B1E9420A637FA00A8B95C /* AttachmentIdentifier.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5F5B1E9320A637FA00A8B95C /* AttachmentIdentifier.swift */; }; 6F704805F86459ADB92E8BAF /* Pods_LocalNotifications_Over_iOS10.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = F5848FD45ED2F74325DF0F78 /* Pods_LocalNotifications_Over_iOS10.framework */; }; /* End PBXBuildFile section */ @@ -46,6 +48,8 @@ 5F340ABC1F7A73DB00520C30 /* NotificationManagerTest.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = NotificationManagerTest.swift; sourceTree = ""; }; 5F340ABE1F7A73F300520C30 /* NotificationObjectTest.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = NotificationObjectTest.swift; sourceTree = ""; }; 5F340AC51F7A810000520C30 /* PrefixHeader.pch */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = PrefixHeader.pch; sourceTree = ""; }; + 5F5B1E9020A636EE00A8B95C /* UIHelper.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = UIHelper.swift; sourceTree = ""; }; + 5F5B1E9320A637FA00A8B95C /* AttachmentIdentifier.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = AttachmentIdentifier.swift; sourceTree = ""; }; C8E8B39885B3BFD04254F0B7 /* Pods-LocalNotifications_Over_iOS10.release.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-LocalNotifications_Over_iOS10.release.xcconfig"; path = "Pods/Target Support Files/Pods-LocalNotifications_Over_iOS10/Pods-LocalNotifications_Over_iOS10.release.xcconfig"; sourceTree = ""; }; DAC1C0E65D7B5F308079A101 /* Pods-LocalNotifications_Over_iOS10.debug.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-LocalNotifications_Over_iOS10.debug.xcconfig"; path = "Pods/Target Support Files/Pods-LocalNotifications_Over_iOS10/Pods-LocalNotifications_Over_iOS10.debug.xcconfig"; sourceTree = ""; }; F5848FD45ED2F74325DF0F78 /* Pods_LocalNotifications_Over_iOS10.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = Pods_LocalNotifications_Over_iOS10.framework; sourceTree = BUILT_PRODUCTS_DIR; }; @@ -119,7 +123,8 @@ isa = PBXGroup; children = ( 5F11D8D51F7A307700A43DF5 /* NotificationManager.swift */, - 5F11D8D71F7A30B400A43DF5 /* NotificationObject.swift */, + 5F5B1E9220A637FA00A8B95C /* Models */, + 5F5B1E8F20A636EE00A8B95C /* Helpers */, ); path = NotificationManager; sourceTree = ""; @@ -134,6 +139,23 @@ path = LocalNotifications_Over_iOS10Tests; sourceTree = ""; }; + 5F5B1E8F20A636EE00A8B95C /* Helpers */ = { + isa = PBXGroup; + children = ( + 5F5B1E9020A636EE00A8B95C /* UIHelper.swift */, + ); + path = Helpers; + sourceTree = ""; + }; + 5F5B1E9220A637FA00A8B95C /* Models */ = { + isa = PBXGroup; + children = ( + 5F5B1E9320A637FA00A8B95C /* AttachmentIdentifier.swift */, + 5F11D8D71F7A30B400A43DF5 /* NotificationObject.swift */, + ); + path = Models; + sourceTree = ""; + }; 88D9150D3DB913EEC078ED8C /* Frameworks */ = { isa = PBXGroup; children = ( @@ -322,9 +344,11 @@ files = ( 53353AE31D89F00E0069804D /* UIViewController+Extension.swift in Sources */, 5F11D8D81F7A30B400A43DF5 /* NotificationObject.swift in Sources */, + 5F5B1E9420A637FA00A8B95C /* AttachmentIdentifier.swift in Sources */, 5F11D8D91F7A3D9200A43DF5 /* NotificationManager.swift in Sources */, 53353ACD1D89C7AD0069804D /* ViewController.swift in Sources */, 53353ACB1D89C7AD0069804D /* AppDelegate.swift in Sources */, + 5F5B1E9120A636EE00A8B95C /* UIHelper.swift in Sources */, ); runOnlyForDeploymentPostprocessing = 0; }; diff --git a/LocalNotifications_Over_iOS10/Assets.xcassets/kandinsky.imageset/Contents.json b/LocalNotifications_Over_iOS10/Assets.xcassets/kandinsky.imageset/Contents.json new file mode 100644 index 0000000..5750dad --- /dev/null +++ b/LocalNotifications_Over_iOS10/Assets.xcassets/kandinsky.imageset/Contents.json @@ -0,0 +1,12 @@ +{ + "images" : [ + { + "idiom" : "universal", + "filename" : "kandinsky.jpg" + } + ], + "info" : { + "version" : 1, + "author" : "xcode" + } +} \ No newline at end of file diff --git a/LocalNotifications_Over_iOS10/Assets.xcassets/kandinsky.imageset/kandinsky.jpg b/LocalNotifications_Over_iOS10/Assets.xcassets/kandinsky.imageset/kandinsky.jpg new file mode 100644 index 0000000..c369fe4 Binary files /dev/null and b/LocalNotifications_Over_iOS10/Assets.xcassets/kandinsky.imageset/kandinsky.jpg differ diff --git a/LocalNotifications_Over_iOS10/Base.lproj/Main.storyboard b/LocalNotifications_Over_iOS10/Base.lproj/Main.storyboard index 7affa4d..2a16e5c 100644 --- a/LocalNotifications_Over_iOS10/Base.lproj/Main.storyboard +++ b/LocalNotifications_Over_iOS10/Base.lproj/Main.storyboard @@ -1,11 +1,11 @@ - + - + @@ -28,10 +28,10 @@ + + + diff --git a/LocalNotifications_Over_iOS10/NotificationManager/Helpers/UIHelper.swift b/LocalNotifications_Over_iOS10/NotificationManager/Helpers/UIHelper.swift new file mode 100644 index 0000000..1f6aa0a --- /dev/null +++ b/LocalNotifications_Over_iOS10/NotificationManager/Helpers/UIHelper.swift @@ -0,0 +1,35 @@ +// +// UIHelper.swift +// iOS10_LocalNotifications +// +// Created by Cau Pierre Jonny on 2017-09-26. +// Copyright © 2017 Cau Pierre Jonny . All rights reserved. +// + +import UIKit + +/// `UIHelper` helper class. +/// +final class UIHelper { + + class func saveImage(name: String) -> URL? { + + guard let image = UIImage(named: name) else { return nil } + + let data = UIImagePNGRepresentation(image) + + let path = FileManager.default.urls(for: FileManager.SearchPathDirectory.cachesDirectory, in: FileManager.SearchPathDomainMask.userDomainMask)[0] + + do { + + let fileURL = path.appendingPathComponent("\(name)") + _ = try data?.write(to: fileURL) + + return fileURL + + } catch { + + return nil + } + } +} diff --git a/LocalNotifications_Over_iOS10/NotificationManager/Models/AttachmentIdentifier.swift b/LocalNotifications_Over_iOS10/NotificationManager/Models/AttachmentIdentifier.swift new file mode 100644 index 0000000..fbd1f27 --- /dev/null +++ b/LocalNotifications_Over_iOS10/NotificationManager/Models/AttachmentIdentifier.swift @@ -0,0 +1,37 @@ +// +// AttachmentIdentifier.swift +// iOS10_LocalNotifications +// +// Created by Cau Pierre Jonny on 2017-09-26. +// Copyright © 2017 Cau Pierre Jonny . All rights reserved. +// + +import Foundation + +@objc public class AttachmentIdentifier: NSObject { + + private static let instance = { + return AttachmentIdentifier() + }() + + @objc public static func shared() -> AttachmentIdentifier { + + return AttachmentIdentifier.instance + } + + private override init() { + //This prevents others from using the default '()' initializer for this class. + } + + @objc public func request() -> String { return "requestIdentifier"} + @objc public func reply() -> String { return "replyIdentifier"} + @objc public func share() -> String { return "shareIdentifier"} + @objc public func follow() -> String { return "followIdentifier"} + @objc public func destructive() -> String { return "destructiveIdentifier"} + @objc public func direction() -> String { return "directionIdentifier"} + @objc public func category() -> String { return "categoryIdentifier"} + @objc public func image() -> String { return "imageIdentifier"} + @objc public func video() -> String { return "videoIdentifier"} + @objc public func customContent() -> String { return "customContentIdentifier"} + +} diff --git a/LocalNotifications_Over_iOS10/NotificationManager/NotificationObject.swift b/LocalNotifications_Over_iOS10/NotificationManager/Models/NotificationObject.swift similarity index 91% rename from LocalNotifications_Over_iOS10/NotificationManager/NotificationObject.swift rename to LocalNotifications_Over_iOS10/NotificationManager/Models/NotificationObject.swift index c31602a..565b6c0 100644 --- a/LocalNotifications_Over_iOS10/NotificationManager/NotificationObject.swift +++ b/LocalNotifications_Over_iOS10/NotificationManager/Models/NotificationObject.swift @@ -28,10 +28,12 @@ import UserNotifications var repeats : Repeats var date:Date var userInfo: [AnyHashable: Any] = [:] + var media : String? + var mediaUrl :String? var attachment : Array? - @objc public init(notification: NotificationType, id: String, title: String, subtitle: String, body: String, badgeCount: NSNumber?, repeats : Repeats, date:Date, userInfo: [AnyHashable: Any] = [:]) { + @objc public init(notification: NotificationType, id: String, title: String, subtitle: String, body: String, badgeCount: NSNumber?, repeats : Repeats, date:Date, userInfo: [AnyHashable: Any] = [:], media: String? = nil, mediaUrl : String? = nil) { self.notification = notification switch notification { case .noneType: @@ -50,6 +52,8 @@ import UserNotifications self.repeats = repeats self.date = date self.userInfo = userInfo + self.media = media + self.mediaUrl = mediaUrl super.init() } diff --git a/LocalNotifications_Over_iOS10/NotificationManager/NotificationManager.swift b/LocalNotifications_Over_iOS10/NotificationManager/NotificationManager.swift index 8fcde61..803f46b 100644 --- a/LocalNotifications_Over_iOS10/NotificationManager/NotificationManager.swift +++ b/LocalNotifications_Over_iOS10/NotificationManager/NotificationManager.swift @@ -64,6 +64,12 @@ import SwiftDate } } + @available(iOS 10.0, *) + @objc public func setupCategories(_ categories: Set) { + let center = UNUserNotificationCenter.current() + center.setNotificationCategories(categories) + } + @available(iOS 10.0, *) @objc public func getNotificationSettings(completion: ((_ value: UNAuthorizationStatus) -> Void)? = nil) { let center = UNUserNotificationCenter.current() @@ -110,15 +116,33 @@ import SwiftDate @objc public func checkStatus() { } + @available(iOS 10.0, *) + @objc public func action(id: String, title: String, options: UNNotificationActionOptions = []) -> UNNotificationAction { + + let action = UNNotificationAction(identifier: id, title: title, options: options) + + return action + } + + @available(iOS 10.0, *) + @objc public func category(identifier: String, action:[UNNotificationAction], intentIdentifiers: [String], options: UNNotificationCategoryOptions = []) -> UNNotificationCategory { + + let category = UNNotificationCategory(identifier: identifier, actions: action, intentIdentifiers: intentIdentifiers, options: options) + + return category + } + // MARK: Private @available(iOS 10.0, *) private func scheduleUNUserNotification(notificationObj:NotificationObject) { requestAuthorization { _ in - let content = NotificationManager.shared().createNotificationContent(notificationObj: notificationObj) + var content = NotificationManager.shared().createNotificationContent(notificationObj: notificationObj) let trigger = NotificationManager.shared().calendarNotificationTrigger(notificationObj.notification, notificationObj.date,notificationObj.repeats) + content = NotificationManager.shared().customCategory(notificationObj, content) + let request = UNNotificationRequest(identifier: notificationObj.id, content: content, trigger: trigger) UNUserNotificationCenter.current().add(request) { error in @@ -133,7 +157,7 @@ import SwiftDate } @available(iOS 10.0, *) - private func createNotificationContent(notificationObj:NotificationObject) -> UNNotificationContent { + private func createNotificationContent(notificationObj:NotificationObject) -> UNMutableNotificationContent { let content = UNMutableNotificationContent() content.title = notificationObj.title content.subtitle = notificationObj.subtitle @@ -145,8 +169,6 @@ import SwiftDate content.attachments = attachementFiles as! [UNNotificationAttachment] } - //content.categoryIdentifier = "CPJReminderCategory" - return content } @@ -203,6 +225,26 @@ import SwiftDate } + @available(iOS 10.0, *) + private func customCategory( _ notificationObj:NotificationObject , _ content : UNMutableNotificationContent ) -> UNMutableNotificationContent { + + if let name = notificationObj.mediaUrl, let media = notificationObj.media, let url = UIHelper.saveImage(name: name) { + print("url is \(url)") + + let attachment = try? UNNotificationAttachment(identifier: media, + url: url, + options: [:]) + + if let attachment = attachment { + content.attachments.removeAll(keepingCapacity: true) + content.attachments.append(attachment) + } + content.categoryIdentifier = media + + } + return content + } + @available(iOS 10.0, *) private func createLocationNotificationTrigger() -> UNLocationNotificationTrigger { let cynnyOfficeRegion = CLCircularRegion(center: CLLocationCoordinate2D(latitude:43.7825, longitude: 11.2594), radius: 10, identifier: "Cynny") @@ -231,7 +273,7 @@ extension NotificationManager: UNUserNotificationCenterDelegate { public func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) { NSLog("User Notification Center will present notification") //show popup on foreground in app - completionHandler(.alert) + completionHandler( [.alert, .badge, .sound]) } } diff --git a/LocalNotifications_Over_iOS10/ViewController.swift b/LocalNotifications_Over_iOS10/ViewController.swift index f80e203..3feae59 100644 --- a/LocalNotifications_Over_iOS10/ViewController.swift +++ b/LocalNotifications_Over_iOS10/ViewController.swift @@ -12,12 +12,23 @@ class ViewController: UIViewController { override func viewDidLoad() { super.viewDidLoad() + setupCustomNotification() } - @IBAction func scheduleNotificationAction(_ sender: AnyObject) { - let notificationObj = NotificationObject(notification: .eventScheduleType, id: "alert", title: "iOS Presentation", subtitle: "Friday September 16th", body: "Remember to finalize your presentation for tomorrow!", badgeCount: nil, repeats: .minutely, date: Date(), userInfo: [:]) + + @IBAction func scheduleSimpleNotificationAction(_ sender: AnyObject) { + + let notificationObj = NotificationObject(notification: .eventScheduleType, id: "simple", title: "iOS New Api Ntification", subtitle: "Simple Notification", body: "Remember to finalize your presentation for tomorrow!", badgeCount: nil, repeats: .minutely, date: Date(), userInfo: [:]) + NotificationManager.shared().scheduleNotification(notificationObj:notificationObj) } - @IBAction func howManyScheduledAction(_ sender: AnyObject) { + + @IBAction func scheduleCustomNotificationAction(_ sender: AnyObject) { + + let notificationObj = NotificationObject(notification: .eventScheduleType, id: "custom", title: "iOS New Api Ntification", subtitle: "Custom Notification", body: "Remember to finalize your presentation for tomorrow!", badgeCount: nil, repeats: .minutely, date: Date(), userInfo: [:], media: AttachmentIdentifier.shared().image(), mediaUrl:"kandinsky.jpg" ) + + NotificationManager.shared().scheduleNotification(notificationObj:notificationObj) + } + @IBAction func howManyScheduledAction(_ sender: AnyObject) { if #available(iOS 10.0, *) { NotificationManager.shared().pending { [weak self] pending in self?.showAlert(message: "\(pending) pending notifications") @@ -26,7 +37,7 @@ class ViewController: UIViewController { showAlert(message: "Pending notifications are only available in iOS 10") } } - @IBAction func howManyDeliveredAction(_ sender: AnyObject) { + @IBAction func howManyDeliveredAction(_ sender: AnyObject) { if #available(iOS 10.0, *) { NotificationManager.shared().delivered { [weak self] deliveredCount in self?.showAlert(message: "\(deliveredCount) delivered notifications") @@ -35,20 +46,43 @@ class ViewController: UIViewController { showAlert(message: "Delivered notifications are only available in iOS 10") } } - @IBAction func removeAllPending(_ sender: AnyObject) { + @IBAction func removeAllPending(_ sender: AnyObject) { if #available(iOS 10.0, *) { NotificationManager.shared().removeAllPending() } else { showAlert(message: "Remove all pending notifications are only available in iOS 10") } } - @IBAction func removeAllDelivery(_ sender: AnyObject) { + @IBAction func removeAllDelivery(_ sender: AnyObject) { if #available(iOS 10.0, *) { NotificationManager.shared().removeAlldelivery() } else { showAlert(message: "Remove all pending notifications are only available in iOS 10") } } + + func setupCustomNotification() { + + // define actions + if #available(iOS 10.0, *) { +// let ac1 = NotificationManager.shared().action(id: UNIdentifiers.reply, title: "Reply") + let ac2 = NotificationManager.shared().action(id: AttachmentIdentifier.shared().share(), title: "Share") +// let ac3 = NotificationManager.shared().action(id: UNIdentifiers.follow, title: "Follow") +// let ac4 = NotificationManager.shared().action(id: UNIdentifiers.destructive, title: "Cancel", options: .destructive) +// let ac5 = NotificationManager.shared().action(id: UNIdentifiers.direction, title: "Get Direction") +// + // define categories +// let cat1 = NotificationManager.shared().category(identifier: UNIdentifiers.category, action: [ac1, ac2, ac3, ac4], intentIdentifiers: []) +// let cat2 = NotificationManager.shared().category(identifier: UNIdentifiers.customContent, action: [ac5, ac4], intentIdentifiers: []) + let cat3 = NotificationManager.shared().category(identifier: AttachmentIdentifier.shared().image(), action: [ac2], intentIdentifiers: [], options: .allowInCarPlay) + NotificationManager.shared().setupCategories([ cat3]) + } else { + // Fallback on earlier versions + } + + // Registers your app’s notification types and the custom actions that they support. + + } } extension Date { diff --git a/LocalNotifications_Over_iOS10/kandinsky.jpg b/LocalNotifications_Over_iOS10/kandinsky.jpg new file mode 100644 index 0000000..c369fe4 Binary files /dev/null and b/LocalNotifications_Over_iOS10/kandinsky.jpg differ