Forked from Dexwell/LocalCommunicationNotification.swift
Created
November 15, 2022 02:33
-
-
Save gatherheart/87c28d606326de8b660087991ab96080 to your computer and use it in GitHub Desktop.
iOS 15 Local Communication Notification
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var content = UNMutableNotificationContent() | |
content.title = "Title" | |
content.subtitle = "Subtitle" | |
content.body = "Text" | |
content.sound = nil | |
content.categoryIdentifier = "categoryName" | |
var personNameComponents = PersonNameComponents() | |
personNameComponents.nickname = "Sender Name" | |
let avatar = INImage(imageData: UIImage(named: "Avatar")!.pngData()!) | |
let senderPerson = INPerson( | |
personHandle: INPersonHandle(value: "1233211234", type: .unknown), | |
nameComponents: personNameComponents, | |
displayName: "Sender Name", | |
image: avatar, | |
contactIdentifier: nil, | |
customIdentifier: nil, | |
isMe: false, | |
suggestionType: .none | |
) | |
let mePerson = INPerson( | |
personHandle: INPersonHandle(value: "1233211234", type: .unknown), | |
nameComponents: nil, | |
displayName: nil, | |
image: nil, | |
contactIdentifier: nil, | |
customIdentifier: nil, | |
isMe: true, | |
suggestionType: .none | |
) | |
let intent = INSendMessageIntent( | |
recipients: [mePerson], | |
outgoingMessageType: .outgoingMessageText, | |
content: "Test", | |
speakableGroupName: INSpeakableString(spokenPhrase: "Sender Name"), | |
conversationIdentifier: "sampleConversationIdentifier", | |
serviceName: nil, | |
sender: senderPerson, | |
attachments: nil | |
) | |
intent.setImage(avatar, forParameterNamed: \.sender) | |
let interaction = INInteraction(intent: intent, response: nil) | |
interaction.direction = .incoming | |
interaction.donate(completion: nil) | |
do { | |
content = try content.updating(from: intent) as! UNMutableNotificationContent | |
} catch { | |
// Handle error | |
} | |
// Show 3 seconds from now | |
let trigger = UNTimeIntervalNotificationTrigger(timeInterval: 3, repeats: false) | |
// Choose a random identifier | |
let request = UNNotificationRequest(identifier: UUID().uuidString, content: content, trigger: trigger) | |
// Add notification request | |
UNUserNotificationCenter.current().add(request) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment