I've been trying to update my (local & push) notifications to Communication notifications.
When a user receives a communication event from one of their friend, I want the displayed notification to include the friend's profile picture. Just like the new iMessage app does.
After watching the dedicated WWDC2021 session, I added a Siri Intent to my SwiftUI app:
// I correctly added the StartCallIntent to both my app's Info.plist and the Siri Intent extension.
<array>
<string>INStartCallIntent</string>
</array>
and tried to update my notification flow with the following:
let image = INImage(imageData: friend.profilePicture.cellImage.pngData()!)
let intentFriend = INPerson(
personHandle: INPersonHandle(value: friend.phoneNumber, type: .phoneNumber),
nameComponents: friend.nameComponents,
displayName: friend.localName,
image: image,
contactIdentifier: nil,
customIdentifier: nil,
isMe: false,
suggestionType: .instantMessageAddress
)
let incomingCommunicationIntent = INStartCallIntent(
callRecordFilter: nil,
callRecordToCallBack: nil,
audioRoute: .unknown,
destinationType: .normal,
contacts: [intentFriend],
callCapability: .audioCall
)
let interaction = INInteraction(intent: incomingCommunicationIntent, response: nil)
interaction.direction = .incoming
interaction.donate(completion: nil)
do {
let result = try notification.updating(from: incomingCommunicationIntent)
return result
} catch let error {
print("-- \(error)")
// TODO: Handle error here
}
return notification
The returned UNNotificationContent
is then handled to be displayed either as a push notification or a local one.
While the above code doesn't crash and seems to /work/, the notification doesn't look any different.
Looking with a debugger, the _UNNotificationCommunicationContext
is initialized, but:
_displayName
is nil_recipients
is empty (as expected)_contentURL
is nil_attachments
is emptysender
is set to a UNNotificationContact
on which
handle
& displayName
are correctly set_handleType
seems correctly set tooIn the app's logs, I can see:
[Intents] -[INCache cacheableObjectForIdentifier:] Unable to find cacheable object with identifier EDB29166-E46A-CF23-AB27-8B61F763A039 in cache.
[Intents] -[INCache cacheableObjectForIdentifier:] Unable to find cacheable object with identifier intents-remote-image-proxy:?proxyIdentifier=EDB29166-E46A-CF23-AB27-8B61F763A039.png&storageServiceIdentifier=com.apple.Intents.INImageServiceConnection in cache.
What am I missing to correctly display the friend's profile picture?
Thank you
First, make sure your notifications settings are correct for the apps, and that the "mute notifications" option is not set for either app: Use notifications on your iPhone, iPad, and iPod touch - Apple Support. If you still need help after that, don't hesitate to respond back with more details on what is happening.
Communication notifications are unique in their ability to break through the scheduled notification summary by default, and can also break through a Focus. Enable the Communication Notifications capability in your app and include the activity types that your app supports in its NSUserActivityTypes array.
If you are not getting notifications after the iOS 15 update, Focus might be the culprit. And before we do some damage control, let’s disable Focus and see if the notifications start pouring in or not. From your lock screen, tap the Focus icon. Tap the currently active Focus to turn it off.
Under the " Allowed Notifications " section, tap on " People " and select the contacts from which you want to get notifications. 5. When done, go back and tap on " Apps " and select the apps from which you want to get notifications. Now, you'll be able to see notifications from selected contacts and apps while in Focus mode.
Note: To view the notification setting for all apps at once, you can go to Settings → Notifications → scroll to the Notification Style section. Tap any app to alter the setting.
You can also invoke Control Center or go to Settings → Focus and disable the Focus Mode from there. Once the Focus is turned off, ask your friends or family to message or mail you to check whether you received the notification or not. If notifications are still not working, skip to no. 5. And if they are arriving, you can either
Unfortunately Apple did not do a great job at documenting communication notifications, their WWDC video leaves a ton of very important details out, but there are a few things:
In your Notification Service Extension's Info.plist
you need to add this:
NSExtension
-> NSExtensionAttributes
(dict) -> IntentsSupported
(array) -> INSendMessageIntent
or INStartCallIntent
.
Make sure you enabled the "Communication Notifications" capability on your main app target.
Finally, your code looks right but you need to add this (note: I have only tested this for INSendMessageIntent
)
// Swift
incomingCommunicationIntent.setImage(image, forParameterNamed: \.sender)
// Objective-C
[incomingCommunicationIntent setImage:image forParameterNamed:"sender"];
For those who's confused like I was, the INSendMessageIntent
key has to be added under NSUserActivityTypes
in the plist file in the main target, like this:
<key>NSUserActivityTypes</key>
<array>
<string>INSendMessageIntent</string>
</array>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With