Skip to content

Commit

Permalink
add feature attach media with use categories
Browse files Browse the repository at this point in the history
  • Loading branch information
Pierre jonny cau committed May 11, 2018
1 parent 7f056e7 commit c14344d
Show file tree
Hide file tree
Showing 11 changed files with 216 additions and 19 deletions.
2 changes: 1 addition & 1 deletion LocalNotifications_Over_iOS10.podspec
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
26 changes: 25 additions & 1 deletion LocalNotifications_Over_iOS10.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -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 */

Expand Down Expand Up @@ -46,6 +48,8 @@
5F340ABC1F7A73DB00520C30 /* NotificationManagerTest.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = NotificationManagerTest.swift; sourceTree = "<group>"; };
5F340ABE1F7A73F300520C30 /* NotificationObjectTest.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = NotificationObjectTest.swift; sourceTree = "<group>"; };
5F340AC51F7A810000520C30 /* PrefixHeader.pch */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = PrefixHeader.pch; sourceTree = "<group>"; };
5F5B1E9020A636EE00A8B95C /* UIHelper.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = UIHelper.swift; sourceTree = "<group>"; };
5F5B1E9320A637FA00A8B95C /* AttachmentIdentifier.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = AttachmentIdentifier.swift; sourceTree = "<group>"; };
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 = "<group>"; };
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 = "<group>"; };
F5848FD45ED2F74325DF0F78 /* Pods_LocalNotifications_Over_iOS10.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = Pods_LocalNotifications_Over_iOS10.framework; sourceTree = BUILT_PRODUCTS_DIR; };
Expand Down Expand Up @@ -119,7 +123,8 @@
isa = PBXGroup;
children = (
5F11D8D51F7A307700A43DF5 /* NotificationManager.swift */,
5F11D8D71F7A30B400A43DF5 /* NotificationObject.swift */,
5F5B1E9220A637FA00A8B95C /* Models */,
5F5B1E8F20A636EE00A8B95C /* Helpers */,
);
path = NotificationManager;
sourceTree = "<group>";
Expand All @@ -134,6 +139,23 @@
path = LocalNotifications_Over_iOS10Tests;
sourceTree = "<group>";
};
5F5B1E8F20A636EE00A8B95C /* Helpers */ = {
isa = PBXGroup;
children = (
5F5B1E9020A636EE00A8B95C /* UIHelper.swift */,
);
path = Helpers;
sourceTree = "<group>";
};
5F5B1E9220A637FA00A8B95C /* Models */ = {
isa = PBXGroup;
children = (
5F5B1E9320A637FA00A8B95C /* AttachmentIdentifier.swift */,
5F11D8D71F7A30B400A43DF5 /* NotificationObject.swift */,
);
path = Models;
sourceTree = "<group>";
};
88D9150D3DB913EEC078ED8C /* Frameworks */ = {
isa = PBXGroup;
children = (
Expand Down Expand Up @@ -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;
};
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"images" : [
{
"idiom" : "universal",
"filename" : "kandinsky.jpg"
}
],
"info" : {
"version" : 1,
"author" : "xcode"
}
}
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
19 changes: 14 additions & 5 deletions LocalNotifications_Over_iOS10/Base.lproj/Main.storyboard
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
<?xml version="1.0" encoding="UTF-8"?>
<document type="com.apple.InterfaceBuilder3.CocoaTouch.Storyboard.XIB" version="3.0" toolsVersion="13196" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" useTraitCollections="YES" colorMatched="YES" initialViewController="BYZ-38-t0r">
<document type="com.apple.InterfaceBuilder3.CocoaTouch.Storyboard.XIB" version="3.0" toolsVersion="14109" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" useTraitCollections="YES" colorMatched="YES" initialViewController="BYZ-38-t0r">
<device id="retina4_7" orientation="portrait">
<adaptation id="fullscreen"/>
</device>
<dependencies>
<deployment identifier="iOS"/>
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="13173"/>
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="14088"/>
<capability name="documents saved in the Xcode 8 format" minToolsVersion="8.0"/>
</dependencies>
<scenes>
Expand All @@ -28,10 +28,10 @@
<nil key="highlightedColor"/>
</label>
<button opaque="NO" contentMode="scaleToFill" contentHorizontalAlignment="center" contentVerticalAlignment="center" buttonType="roundedRect" lineBreakMode="middleTruncation" translatesAutoresizingMaskIntoConstraints="NO" id="wZX-j4-jE1">
<rect key="frame" x="108" y="248.5" width="159" height="30"/>
<state key="normal" title="Schedule a Notification"/>
<rect key="frame" x="85.5" y="248.5" width="204" height="30"/>
<state key="normal" title="Schedule Custom Notification"/>
<connections>
<action selector="scheduleNotificationAction:" destination="BYZ-38-t0r" eventType="touchUpInside" id="qxp-oa-hBe"/>
<action selector="scheduleCustomNotificationAction:" destination="BYZ-38-t0r" eventType="touchUpInside" id="T9r-W3-NZA"/>
</connections>
</button>
<button opaque="NO" contentMode="scaleToFill" contentHorizontalAlignment="center" contentVerticalAlignment="center" buttonType="roundedRect" lineBreakMode="middleTruncation" translatesAutoresizingMaskIntoConstraints="NO" id="qr0-vF-EOL">
Expand Down Expand Up @@ -62,14 +62,23 @@
<action selector="removeAllDelivery:" destination="BYZ-38-t0r" eventType="touchUpInside" id="4vL-CI-Ls3"/>
</connections>
</button>
<button opaque="NO" contentMode="scaleToFill" contentHorizontalAlignment="center" contentVerticalAlignment="center" buttonType="roundedRect" lineBreakMode="middleTruncation" translatesAutoresizingMaskIntoConstraints="NO" id="CsI-1t-Xgb">
<rect key="frame" x="89" y="178" width="198" height="30"/>
<state key="normal" title="Schedule Simple Notification"/>
<connections>
<action selector="scheduleSimpleNotificationAction:" destination="BYZ-38-t0r" eventType="touchUpInside" id="QJS-3g-98c"/>
</connections>
</button>
</subviews>
<color key="backgroundColor" red="1" green="1" blue="1" alpha="1" colorSpace="custom" customColorSpace="sRGB"/>
<constraints>
<constraint firstItem="CsI-1t-Xgb" firstAttribute="centerX" secondItem="8bC-Xf-vdC" secondAttribute="centerX" id="4hn-5P-d2l"/>
<constraint firstItem="gp6-wH-AOy" firstAttribute="top" secondItem="3nm-I6-mIX" secondAttribute="bottom" constant="40" id="7M9-cx-BGH"/>
<constraint firstItem="Uwa-sY-byy" firstAttribute="top" secondItem="qr0-vF-EOL" secondAttribute="bottom" constant="40" id="7Vp-zX-i0z"/>
<constraint firstItem="gY3-fP-Jql" firstAttribute="centerX" secondItem="8bC-Xf-vdC" secondAttribute="centerX" id="Gkg-vK-dCi"/>
<constraint firstItem="qr0-vF-EOL" firstAttribute="top" secondItem="wZX-j4-jE1" secondAttribute="bottom" constant="40" id="MoV-Oq-8x9"/>
<constraint firstItem="wZX-j4-jE1" firstAttribute="centerX" secondItem="8bC-Xf-vdC" secondAttribute="centerX" id="ORQ-qT-7X6"/>
<constraint firstItem="wZX-j4-jE1" firstAttribute="top" secondItem="CsI-1t-Xgb" secondAttribute="bottom" constant="40" id="RxP-dl-ZHj"/>
<constraint firstItem="3nm-I6-mIX" firstAttribute="centerX" secondItem="8bC-Xf-vdC" secondAttribute="centerX" id="Su9-fF-Yja"/>
<constraint firstItem="qr0-vF-EOL" firstAttribute="centerY" secondItem="8bC-Xf-vdC" secondAttribute="centerY" id="YPA-dq-Ps0"/>
<constraint firstItem="qr0-vF-EOL" firstAttribute="centerX" secondItem="8bC-Xf-vdC" secondAttribute="centerX" id="e5f-pW-Rje"/>
Expand Down
Original file line number Diff line number Diff line change
@@ -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
}
}
}
Original file line number Diff line number Diff line change
@@ -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"}

}
Original file line number Diff line number Diff line change
Expand Up @@ -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<Any>?

@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:
Expand All @@ -50,6 +52,8 @@ import UserNotifications
self.repeats = repeats
self.date = date
self.userInfo = userInfo
self.media = media
self.mediaUrl = mediaUrl
super.init()
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,12 @@ import SwiftDate
}
}

@available(iOS 10.0, *)
@objc public func setupCategories(_ categories: Set<UNNotificationCategory>) {
let center = UNUserNotificationCenter.current()
center.setNotificationCategories(categories)
}

@available(iOS 10.0, *)
@objc public func getNotificationSettings(completion: ((_ value: UNAuthorizationStatus) -> Void)? = nil) {
let center = UNUserNotificationCenter.current()
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand All @@ -145,8 +169,6 @@ import SwiftDate
content.attachments = attachementFiles as! [UNNotificationAttachment]
}

//content.categoryIdentifier = "CPJReminderCategory"

return content
}

Expand Down Expand Up @@ -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")
Expand Down Expand Up @@ -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])
}

}
Loading

0 comments on commit c14344d

Please sign in to comment.