I’m struggling to send a communication notification (with the sender’s avatar and his name). I’m making a client application for a third-party site where sending Push notifications is not implemented. As a result, I use local notification sending. I found very little information on the Internet about these notifications, but the code I found doesn’t work for me.
The question is, can communication notifications be sent only through Push notifications? If so, this issue can be considered closed. If not, why do I have a regular notification sent, although I turned on the necessary Capabilities and added Message Intent to Info.plist.
To clarify, I don’t have a backend server. I’m just asking if notifications with an avatar and contact name (communication notifications) work only for Push notifications? I can’t send a push, only a local notification. BUT I can’t style the local notification under the message style with the contact name and avatar.
func didReceive(
request: UNNotificationRequest,
withContentHandler contentHandler: @escaping (UNNotificationContent
) -> Void) {
let handle = INPersonHandle(value: "unique-user-id)", type: .unknown)
let sender = INPerson(personHandle: handle,
nameComponents: nil,
displayName: "Name",
image: nil, // here you can provide UIImage for user avatar
contactIdentifier: nil,
customIdentifier: "unique-user-id")
let intent = INSendMessageIntent(
recipients: nil,
outgoingMessageType: .outgoingMessageText,
content: "content of message",
speakableGroupName: nil,
conversationIdentifier: "unique-user-id-conv",
serviceName: nil,
sender: sender,
attachments: nil
)
let interaction = INInteraction(intent: intent, response: nil)
interaction.direction = .incoming
interaction.donate(completion: nil)
let content = request.content
do {
let updatedContent = try content.updating(from: intent)
let mutableBestAttemptContent = (updatedContent.mutableCopy() as? UNMutableNotificationContent)!
mutableBestAttemptContent.threadIdentifier = "Thread-identifier" // use this field for grouping notifications
mutableBestAttemptContent.userInfo = request.content.userInfo
contentHandler(mutableBestAttemptContent)
} catch {
// Handle errors that may occur while updating content.
}
}