Skip to content

Instantly share code, notes, and snippets.

@thedevme
Last active October 18, 2017 16:18
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save thedevme/20f664bbf679fe557209c57d5e34216f to your computer and use it in GitHub Desktop.
Save thedevme/20f664bbf679fe557209c57d5e34216f to your computer and use it in GitHub Desktop.
Slide Code
// Basic Notification Setup
class AppDelegate: UIResponder, UIApplicationDelegate {
var window: UIWindow?
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
registerForNotificationsWithApplication(application)
return true
}
func registerForNotificationsWithApplication(_ application: UIApplication) {
let notificationCenter = UNUserNotificationCenter.current()
notificationCenter.delegate = self
notificationCenter.requestAuthorization(options: [.alert, .sound, .badge]) { _, error in
if let error = error {
fatalError("failed to get auth \(error)")
}
}
// Hidden Content
let articleCategory = UNNotificationCategory(identifier: "article-category",
actions: [],
intentIdentifiers: [],
hiddenPreviewsBodyPlaceholder:
NSLocalizedString("ARTICLE_CATEGORY", comment: "article placeholder"), options: [])
let scoreCategory = UNNotificationCategory(identifier: "score-category",
actions: [],
intentIdentifiers: [],
hiddenPreviewsBodyPlaceholder:
NSLocalizedString("SCORE_CATEGORY", comment: "score placeholder"), options: [])
notificationCenter.setNotificationCategories([scoreCategory, articleCategory])
application.registerForRemoteNotifications()
}
func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
var token = ""
for i in 0..<deviceToken.count {
token = token + String(format: "%02.2hhx", arguments: [deviceToken[i]])
}
print("token \(token)")
}
}
extension AppDelegate: UNUserNotificationCenterDelegate {
func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {
print(notification)
completionHandler([.alert, .sound])
}
func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) {
completionHandler()
}
func application(_ application: UIApplication, didFailToRegisterForRemoteNotificationsWithError error: Error) {
print("Push notifications registration failed with \(error)")
}
}
{
"aps":
{
"alert":"New Draft Pick",
"mutable-content": 1,
"category": "draft-category"
},
"title": "Drafted ",
"round": "6",
"pick": "199",
"first_name": "Tom",
"last_name": "Brady",
"position": "QB",
"height": "6’6",
"weight": "214",
"grade": "6.7",
"draft-summary": "Summary goes here"
}
class NotificationService: UNNotificationServiceExtension {
var contentHandler: ((UNNotificationContent) -> Void)?
var bestAttemptContent: UNMutableNotificationContent?
var currentDownloadTask: URLSessionDownloadTask?
override func didReceive(_ request: UNNotificationRequest, withContentHandler contentHandler: @escaping (UNNotificationContent) -> Void) {
self.contentHandler = contentHandler
bestAttemptContent = (request.content.mutableCopy() as? UNMutableNotificationContent)
if let mutableContent = self.bestAttemptContent,
let urlString = mutableContent.userInfo["url"] as? String,
let url = URL(string: urlString),
let body = mutableContent.userInfo["body"] as? String,
let subtitle = mutableContent.userInfo["subtitle"] as? String,
let title = mutableContent.userInfo["title"] as? String
{
mutableContent.title = title
mutableContent.body = body
currentDownloadTask = URLSession.shared.downloadTask(with: url, completionHandler: { fileURL, _, error in
if let error = error {
NSLog("download task failed with \(error)")
if let mediaType = mutableContent.userInfo["type"] as? String {
mutableContent.title = NSString.localizedUserNotificationString(forKey: "FALLBACK_TITLE", arguments: [mediaType])
}
}
else {
if let fileURL = fileURL,
let fileType = NotificationService.fileType(fileExtension: url.pathExtension),
let attachment = try? UNNotificationAttachment(identifier: "pushAttachment", url: fileURL, options: [UNNotificationAttachmentOptionsTypeHintKey: fileType]) {
mutableContent.attachments = [attachment]
mutableContent.title = title
mutableContent.subtitle = subtitle
mutableContent.body = body
mutableContent.categoryIdentifier = "test"
}
}
contentHandler(mutableContent)
})
currentDownloadTask?.resume()
}
}
override func serviceExtensionTimeWillExpire() {
if let contentHandler = contentHandler, let bestAttemptContent = bestAttemptContent {
contentHandler(bestAttemptContent)
}
}
class func fileType(fileExtension: String) -> CFString? {
return UTTypeCreatePreferredIdentifierForTag(kUTTagClassFilenameExtension, fileExtension as CFString, nil)?.takeRetainedValue()
}
}
@thedevme
Copy link
Author

thedevme commented Sep 1, 2017

Hidden Notifications Plist

screen shot 2017-09-01 at 9 28 31 am

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment