Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
iOS 15 Local Communication Notification
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)
@Dexwell

This comment has been minimized.

Copy link
Owner Author

@Dexwell Dexwell commented Aug 10, 2021

To get this to work, also make sure to do the following:

  • In your Notification Service Extension's Info.plist, add this: NSExtensionNSExtensionAttributes (dictionary) → IntentsSupported (array) → INSendMessageIntent (string)
  • Enable the Communication Notifications capability on your main app target
  • In Xcode, run your Notification Service Extension target and select your app to launch it with
@wilg

This comment has been minimized.

Copy link

@wilg wilg commented Sep 23, 2021

Do you know how to get this working with group notifications? iMessage seems to show the group icon instead of the sender icon in this case.

I cannot get it to simultaneously show both the group name and an icon for it. If I don't provide the group as an INPerson in recipients then speakableGroupName will not appear. But the INImage on the group's INPerson is never displayed, nor is the sender's image when I do this.

@Dexwell

This comment has been minimized.

Copy link
Owner Author

@Dexwell Dexwell commented Sep 23, 2021

@wilg No idea as I haven't worked on group notifications yet, but please share if you do find out!

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