-
Notifications
You must be signed in to change notification settings - Fork 8
/
NotificationService.swift
91 lines (71 loc) · 3.16 KB
/
NotificationService.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
//
// NotificationService.swift
// NotificationService
//
// Created by 马权 on 6/23/16.
// Copyright © 2016 马权. All rights reserved.
//
/*
Service push data struct:
{
"aps" : {
"alert" : {
"title" : "title",
"body" : "Your message Here"
},
"mutable-content" : "1",
"category" : "Cheer" // if needn't use custom extensionContent, delete it.
},
"imageAbsoluteString" : "http://ww1.sinaimg.cn/large/65312d9agw1f59leskkcij20cs0csmym.jpg" // custom info
}
*/
import UserNotifications
class NotificationService: UNNotificationServiceExtension {
var contentHandler: ((UNNotificationContent) -> Void)?
var bestAttemptContent: UNMutableNotificationContent?
override func didReceive(_ request: UNNotificationRequest,
withContentHandler contentHandler:(UNNotificationContent) -> Void) {
self.contentHandler = contentHandler
bestAttemptContent = (request.content.mutableCopy() as? UNMutableNotificationContent)
// get app group fmURL. If you reset group, remember reset GroupIdentifier
let fmURL: URL? = FileManager.default().containerURLForSecurityApplicationGroupIdentifier("group.emm")
LOG_WARNING
print("If you reset app group or provisioning Profile, remember reset GroupIdentifier")
var attachement: UNNotificationAttachment? = nil
// use semaphore convert async download to sync
let semaphore = DispatchSemaphore(value: 0)
URLSession.downloadImage(atURL: URL.resource(type: .AttachmentRemote)) { (data, error) in
var url: URL? = nil
do {
// have to set file extension which support by UNNotificationAttachment, png, jpeg, gif...
url = try fmURL?.appendingPathComponent("customAttachmentPic").appendingPathExtension(".png")
}
catch {
// out put error by notification
if let bestAttemptContent = self.bestAttemptContent {
bestAttemptContent.title = "customAttachmentPic"
bestAttemptContent.body = String(error)
contentHandler(bestAttemptContent)
return
}
}
// write to app group file url
try! data?.write(to: url!)
// create UNNotificationAttachment by app group file url
attachement = try! UNNotificationAttachment(identifier: "attachment", url: url!, options: nil)
semaphore.signal()
}
semaphore.wait()
// success set attachments
if let bestAttemptContent = bestAttemptContent {
bestAttemptContent.title = "\(bestAttemptContent.title) [modified]"
bestAttemptContent.attachments = [attachement!]
contentHandler(bestAttemptContent)
}
}
override func serviceExtensionTimeWillExpire() {
if let contentHandler = contentHandler, let bestAttemptContent = bestAttemptContent {
contentHandler(bestAttemptContent)
}
}
}