Skip to content

Instantly share code, notes, and snippets.

@janodev
janodev / createNotification.swift
Last active August 16, 2016 12:46
Create a notification with an image attachment
let content = UNMutableNotificationContent()
content.title = "Will it rain?"
content.body = "It never rains in (Southern) California!"
content.sound = UNNotificationSound.default()
let url = URL(string: "https://67.media.tumblr.com/ab04c028a5244377a0ab96e73915e584/tumblr_nfn3ztLjxk1tq4of6o1_400.gif")
let attachment = try! UNNotificationAttachment(identifier: "image", url: url!, options: nil)
content.attachments = [attachment]
@janodev
janodev / scheduleNotification.swift
Created August 16, 2016 12:47
Schedule a notification
let trigger = UNTimeIntervalNotificationTrigger(timeInterval: 5, repeats: false)
let request = UNNotificationRequest(identifier: "local_notification", content: content, trigger: trigger)
UNUserNotificationCenter.current().add(request)
@janodev
janodev / customNotification.json
Created August 16, 2016 12:48
A custom notification payload
{
"aps": {
"alert": {...}
"mutable-content": 1
}
"my-attachment": "https://foo.bar/baz.jpg",
"category": "DylanOrDermotCategory"
}
@janodev
janodev / setCategoryLocal.swift
Created August 16, 2016 12:50
Set the category of a local notification
override func didReceive(_ request: UNNotificationRequest, withContentHandler contentHandler:(UNNotificationContent) -> Void)
{
content = (request.content.mutableCopy() as? UNMutableNotificationContent)
content?.categoryIdentifier = "DylanOrDermotCategory"
// ...
}
@janodev
janodev / createAttachment.swift
Created August 16, 2016 12:50
Create a notification attachment
import UserNotifications
class ServiceExtension: UNNotificationServiceExtension
{
struct Attachments {
static let image = (identifier:"image", jsonElement:"imageURL")
}
var contentHandler: ((UNNotificationContent) -> Void)?
var content: UNMutableNotificationContent?
@janodev
janodev / contentExtension.plist
Created August 16, 2016 12:51
plist of a Notification Content Extension
NSExtension Dictionary
NSExtensionAttributes Dictionary
UNNotificationExtensionDefaultContent Number 0.5
UNNotificationExtensionDefaultContentHidden Bool YES
UNNotificationExtensionCategory String event-invite
NSExtensionMainStoryboard String MainInterface
NSExtensionPointIdentifier String com.apple.usernotifications.content-extensio
@janodev
janodev / layoutContent.swift
Created August 16, 2016 12:53
Layout the content
override func viewDidLoad()
{
super.viewDidLoad()
let size = view.bounds.size
preferrredContentSize = CGSize(width: size.width, height: size.width / 2)
}
@janodev
janodev / recoverAttachment.swift
Created August 16, 2016 12:53
Recover a notification attachment
import UIKit
import UserNotifications
import UserNotificationsUI
struct Attachments {
static let image = (identifier:"image", jsonElement:"imageURL")
}
class ContentViewController: UIViewController, UNNotificationContentExtension
{
@janodev
janodev / handleAction.swift
Created August 16, 2016 12:55
Handle the action
class NotificationViewController: UIViewController, UINotificationContentExtension
{
func didReceive(_ response: UNNotificationResponse,
completionHandler done: (UNNotificationContentExtensionResponseOption) -> Void)
{
server.postEventResponse(response.actionIdentifier)
{
if response.actionIdentifier == "accept"
{
eventResponse.text = "Going!"
@janodev
janodev / handleTextResponse.swift
Created August 16, 2016 12:56
Handle a text response
class NotificationViewController: UIViewController, UINotificationContentExtension
{
override func canBecomeFirstResponder() -> Bool {
return true
}
override var inputAccessoryView: UIView {
get { return inputView }
}