NotificationService.swift 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. //
  2. // NotificationService.swift
  3. // BeautifulIndonesiaNotificationService
  4. //
  5. // Created by Akhmad Al Qindi Irsyam on 01/03/23.
  6. //
  7. import UserNotifications
  8. import Intents
  9. import UIKit
  10. class NotificationService: UNNotificationServiceExtension {
  11. var contentHandler: ((UNNotificationContent) -> Void)?
  12. var bestAttemptContent: UNMutableNotificationContent?
  13. override func didReceive(_ request: UNNotificationRequest, withContentHandler contentHandler: @escaping (UNNotificationContent) -> Void) {
  14. self.contentHandler = contentHandler
  15. bestAttemptContent = (request.content.mutableCopy() as? UNMutableNotificationContent)
  16. guard let bestAttemptContent = bestAttemptContent else {
  17. return
  18. }
  19. // Check if the notification is a communication notification
  20. let categoryIdentifier = bestAttemptContent.categoryIdentifier
  21. if categoryIdentifier != "chat" {
  22. contentHandler(bestAttemptContent)
  23. return
  24. }
  25. let avatarDefault = INImage(imageData: UIImage(systemName: "person.circle.fill")!.withTintColor(.gray).pngData()!)
  26. var sender = INPerson(personHandle: INPersonHandle(value: "02cdfdfdac", type: .unknown), nameComponents: nil, displayName: "Meme", image: avatarDefault, contactIdentifier: "02cdfdfdac", customIdentifier: "02cdfdfdac")
  27. // sender = INPerson(personHandle: INPersonHandle(value: "02cdfdfdac", type: .unknown), nameComponents: nil, displayName: "Meme", image: INImage(url: URL(string: "https://i.pinimg.com/474x/69/77/b7/6977b70a129ec184527433bbdf9fe457.jpg")!), contactIdentifier: "02cdfdfdac", customIdentifier: nil)
  28. // let nsDocumentDirectory = FileManager.SearchPathDirectory.documentDirectory
  29. // let nsUserDomainMask = FileManager.SearchPathDomainMask.userDomainMask
  30. // let paths = NSSearchPathForDirectoriesInDomains(nsDocumentDirectory, nsUserDomainMask, true)
  31. // if let dirPath = paths.first {
  32. // let fileURL = URL(fileURLWithPath: dirPath).appendingPathComponent("6977b70a129ec184527433bbdf9fe457.jpg")
  33. // if FileManager.default.fileExists(atPath: fileURL.path) {
  34. // sender = INPerson(personHandle: INPersonHandle(value: "02cdfdfdac", type: .unknown), nameComponents: nil, displayName: "Hansen", image: INImage(url: ), contactIdentifier: "02cdfdfdac", customIdentifier: nil)
  35. // }
  36. // }
  37. let incomingMessageIntent = INSendMessageIntent(recipients: [sender], outgoingMessageType: .outgoingMessageText, content: nil, speakableGroupName: INSpeakableString(spokenPhrase: "Test Group"), conversationIdentifier: "Test Group", serviceName: nil, sender: sender, attachments: nil)
  38. incomingMessageIntent.setImage(avatarDefault, forParameterNamed: \.speakableGroupName)
  39. let interaction = INInteraction(intent: incomingMessageIntent, response: nil)
  40. interaction.direction = .incoming
  41. interaction.donate { error in
  42. if let error = error {
  43. contentHandler(bestAttemptContent)
  44. return
  45. }
  46. let content = UNMutableNotificationContent()
  47. content.subtitle = "testing subTitle"
  48. content.body = "testing body"
  49. content.sound = .default
  50. let myRequest = UNNotificationRequest(identifier: "02cdfdfdac", content: content, trigger: nil)
  51. let myContent = myRequest.content
  52. do {
  53. // Update notification content before displaying the
  54. // communication notification.
  55. if #available(iOSApplicationExtension 15.0, *) {
  56. let updatedContent = try myContent.updating(from: incomingMessageIntent)
  57. contentHandler(updatedContent)
  58. } else {
  59. contentHandler(bestAttemptContent)
  60. }
  61. } catch {
  62. contentHandler(bestAttemptContent)
  63. }
  64. }
  65. }
  66. }