看到群里有同学在咨询,推送通知如何自定义左侧的icon 部位,因为iOS 10之后有推出,通知扩展,所以大家知道可以通过
Notification Extension
可以给通知添加媒体资源即图片、音视频等。但是我们也知道,通过通知扩展添加的图片是展示在右侧的,如下图:
那么如何修改左侧的icon,实现苹果短信的效果呢?带着疑问我们向下看。
查找苹果
官方文档
及
WWDC 2021
发现,iOS 15之后,苹果推出了一个叫
通讯通知
的功能。可以看看苹果官方给的定义。
Apple has added the ability to distinguish your app’s notifications as communication notifications. These notifications will now feature the image or avatar of the contact they were sent from and can integrate with SiriKit so that Siri can intelligently provide shortcuts and suggestions for communication actions based on common contacts. For example, when users are setting allowed contacts for a Focus mode or are placing a call from your app. Siri will intelligently suggest contacts based on the intent data donated by your application.
Apple 添加了将应用程序的通知区分为通信通知的功能。这些通知现在将包含发送它们的联系人的图像或头像,并且可以与 SiriKit 集成,以便 Siri 可以智能地根据常用联系人为通信操作提供快捷方式和建议。例如,当用户为 Focus 模式设置允许的联系人或从您的应用程序拨打电话时。Siri 将根据您的应用程序提供的意图数据智能地建议联系人。
就是说,苹果提供了将推送通知区分为通信通知的功能,用通信通知可以显示用户的头像等内容。
图例:
看完定义与效果,我们发现这正是我们需要的功能,那么具体如何实现呢?
To use communication notifications, apps will need to add the Communication Notifications capability to their App in Xcode and update the content of their notification in the app’s Notification Service Extension with an intent object that implements the new UNNotificationContentProviding protocol.
要使用通讯通知,APP 需要在 Xcode 中将通讯通知功能添加到其 APP,并在应用程序通知服务扩展中实现
UNNotificationContentProviding
协议。
<key>NSUserActivityTypes</key>
<array>
<string>INStartCallIntent</string>
<string>INSendMessageIntent</string>
</array>
#import <Intents/Intents.h>
#import <UserNotifications/UserNotifications.h>
NSPersonNameComponents *nameComponents = [[NSPersonNameComponents alloc] init];
nameComponents.nickname = message.fromUsername;
INPerson *messageSender = [[INPerson alloc]initWithPersonHandle:[[INPersonHandle alloc]initWithValue:nil type:INPersonHandleTypeUnknown] nameComponents:nameComponents displayName:message.fromName image:avatarImage contactIdentifier:nil customIdentifier:message.fromUsername isMe:NO suggestionType:(INPersonSuggestionTypeNone)];
INSendMessageIntent *intent = [[INSendMessageIntent alloc] initWithRecipients:@[messageSender] outgoingMessageType:(INOutgoingMessageTypeOutgoingMessageText) content:contentAttribut.string speakableGroupName:[[INSpeakableString alloc]initWithSpokenPhrase:@""] conversationIdentifier:message.msgId serviceName:nil sender:messageSender attachments:nil];
[intent setImage:avatarImage forParameterNamed:@"speakableGroupName"];
INInteraction *interaction = [[INInteraction alloc]initWithIntent:intent response:nil];
interaction.direction = INInteractionDirectionIncoming;
[interaction donateInteractionWithCompletion:nil];
完整实现代码如下:
UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter];
UNMutableNotificationContent *content = [[UNMutableNotificationContent alloc] init];
content.title = message.fromName;
content.body = contentAttribut.string;
content.sound = [UNNotificationSound defaultSound];
content.badge = @([UIApplication sharedApplication].applicationIconBadgeNumber + 1);
content.userInfo = @{
@"url":@"xxxxxxx",
if (@available(iOS 15.0, *)) {
if (message.fromUsername && message.fromAvatar && message.fromName && message.msgId)
NSURL *imageURL = [NSURL URLWithString:message.fromAvatar];
__block INImage *avatarImage = nil;
[[SDWebImageDownloader sharedDownloader] downloadImageWithURL:imageURL completed:^(UIImage * _Nullable image, NSData * _Nullable data, NSError * _Nullable error, BOOL finished) {
if (image) {
avatarImage = [INImage imageWithImageData:data];
NSPersonNameComponents *nameComponents = [[NSPersonNameComponents alloc] init];
nameComponents.nickname = message.fromUsername;
INPerson *messageSender = [[INPerson alloc]initWithPersonHandle:[[INPersonHandle alloc]initWithValue:nil type:INPersonHandleTypeUnknown] nameComponents:nameComponents displayName:message.fromName image:avatarImage contactIdentifier:nil customIdentifier:message.fromUsername isMe:NO suggestionType:(INPersonSuggestionTypeNone)];
INSendMessageIntent *intent = [[INSendMessageIntent alloc] initWithRecipients:@[messageSender] outgoingMessageType:(INOutgoingMessageTypeOutgoingMessageText) content:contentAttribut.string speakableGroupName:[[INSpeakableString alloc]initWithSpokenPhrase:@""] conversationIdentifier:message.msgId serviceName:nil sender:messageSender attachments:nil];
[intent setImage:avatarImage forParameterNamed:@"speakableGroupName"];
INInteraction *interaction = [[INInteraction alloc]initWithIntent:intent response:nil];
interaction.direction = INInteractionDirectionIncoming;
[interaction donateInteractionWithCompletion:nil];
UNNotificationRequest *request = [UNNotificationRequest requestWithIdentifier:message.msgId content:[content contentByUpdatingWithProvider:intent error:nil] trigger:nil];
[center addNotificationRequest:request withCompletionHandler:^(NSError *_Nullable error) {
NSLog(@"成功添加推送");
}];
}];
}else{
UNNotificationRequest *request = [UNNotificationRequest requestWithIdentifier:message.msgId content:content trigger:nil];
[center addNotificationRequest:request withCompletionHandler:^(NSError *_Nullable error) {
NSLog(@"成功添加推送");
}];
}else{
UNNotificationRequest *request = [UNNotificationRequest requestWithIdentifier:message.msgId content:content trigger:nil];
[center addNotificationRequest:request withCompletionHandler:^(NSError *_Nullable error) {
NSLog(@"成功添加推送");
}];
iOS10 之后的通知具有扩展功能,可以在系统收到通知、展示通知时做一些事情。
<key>NSUserActivityTypes</key>
<array>
<string>INStartCallIntent</string>
<string>INSendMessageIntent</string>
</array>
- (void)didReceiveNotificationRequest:(UNNotificationRequest *)request withContentHandler:(void (^)(UNNotificationContent * _Nonnull))contentHandler {
核心代码如下:
- (void)didReceiveNotificationRequest:(UNNotificationRequest *)request withContentHandler:(void (^)(UNNotificationContent * _Nonnull))contentHandler {
self.bestAttemptContent = [request.content mutableCopy];
if (@available(iOS 15.0, *)) {
NSString *fromUsername = self.bestAttemptContent.userInfo[@"xxx"];
NSString *fromAvatar = self.bestAttemptContent.userInfo[@"xxx"];
NSString *fromNickName = self.bestAttemptContent.userInfo[@"xxx"];
NSString *messageId = self.bestAttemptContent.userInfo[@"xxx"];
if (fromUsername && fromAvatar && fromNickName && messageId)
NSURL *imageURL = [NSURL URLWithString:fromAvatar];
__block INImage *avatarImage = nil;
[[SDWebImageDownloader sharedDownloader] downloadImageWithURL:imageURL completed:^(UIImage * _Nullable image, NSData * _Nullable data, NSError * _Nullable error, BOOL finished) {
if (image) {
avatarImage = [INImage imageWithImageData:data];
NSPersonNameComponents *nameComponents = [[NSPersonNameComponents alloc] init];
nameComponents.nickname = fromUsername;
INPerson *messageSender = [[INPerson alloc]initWithPersonHandle:[[INPersonHandle alloc]initWithValue:nil type:INPersonHandleTypeUnknown] nameComponents:nameComponents displayName:fromNickName image:avatarImage contactIdentifier:nil customIdentifier:fromUsername isMe:NO suggestionType:(INPersonSuggestionTypeNone)];
INSendMessageIntent *intent = [[INSendMessageIntent alloc] initWithRecipients:@[messageSender] outgoingMessageType:(INOutgoingMessageTypeOutgoingMessageText) content:self.bestAttemptContent.body speakableGroupName:[[INSpeakableString alloc]initWithSpokenPhrase:@""] conversationIdentifier:messageId serviceName:nil sender:messageSender attachments:nil];
[intent setImage:avatarImage forParameterNamed:@"speakableGroupName"];
INInteraction *interaction = [[INInteraction alloc]initWithIntent:intent response:nil];
interaction.direction = INInteractionDirectionIncoming;
[interaction donateInteractionWithCompletion:nil];
self.bestAttemptContent = [[request.content contentByUpdatingWithProvider:intent error:nil] mutableCopy];
}];
}else{
contentHandler(self.bestAttemptContent);
}else{
contentHandler(self.bestAttemptContent);
至此,通讯通知功能完成,可以测试了。我们实现后的效果如下图:
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/ad3a22c4a2e309ad417590487eefca8b.jpeg)
自iOS10 更新以来,Apple 表示这是 iOS 有史以来最大的升级(our biggest release yet),更加智能开放的Siri、强化应用对3DTouch支持、 HomeKit 、电话拦截及全新设计的通知等等。
iOS 10 中将之前繁杂的推送通知统一成UserNotifications.framework 来集中管理和使用通知功能,还增加一些实用的功能——撤回单条通知、...
WWDC 2021 苹果在 iOS 15 系统中对通知做了很多改变, 让通知更加个性化.
这里只有讨论通信通知 Communication Notifications, 苹果自带的很多应用, 以及第三方App 飞书, 都使用了这个通知功能。
通信通知 Communication Notifications 简介
iOS 15系统后, Apple 添加了通信通知的功能。这些通知将包含发送它们的联系人的头像,并且可以与 SiriKit 集成,以便 Siri 可以智能地根据常用联系人提供通信操作的快捷方式和建议
1>Observer参数:是哪个对象要监听
2>selector参数:监听的对象收到通知后执行哪个方法
3>name参数:监听通知的名称,明确监听哪个类型名称的通知,可以自定义通知名称。(如果不写,则会监听发出通知对象的所有通
iOS开发-NotificationServiceExtension实现实时音视频呼叫通知响铃与震动在之前的开发中,遇到了实时音视频呼叫通知,当App未打开或者App在后台时候,需要通知到用户,用户点击通知栏后是否接入实时音视频的视频或者音频通话。在iOS需要为工程新建Target:NotificationServiceExtension。
Intent的概念
Siri做完语音识别和语义分析之后,将结构化语音分析结果打包成一个某个领域(Domain)的意图(Intent),然后交给支持这个意图(Intent)的第三方应用(比如微信),第三方应用被启动,从传入的Intent中获取相应的信息,完成操作。
例如,上图演示中提到了的例句 “I need to send a message to Nancy via WeChat sayin
苹果信息推送服务(Apple Push Notification Service),是由苹果官方提供的消息推送服务。推送形式包括顶部消息条、声音以及badge number()有了APNS,应用程序可在任意状态接收到与程序有关的消息(包括运行状态not running,foreground以及background),由于在大多数情况下,iOS中最多只有一个应用能处于active状态,所以,APNS
新项目要用到推送,然后语音播报推送里的内容。比如支付宝的推送功能“支付宝到账100元”,这种的。
目前做这个的方法第一个想到的就是Notification Service Extension了,Notification Service Extension是ios10推出的新功能,所以只能在ios10及其以上的手机上有用。
Notification Service Extension的作用就...
注意:1.Intent的命名,如果命名为A,编译之后,系统自动生成AIntent.h文件,import AIntent.h即可使用。
2.Paramters:你想要Intent传递的数据,自定义即可。
3.ShortCut Ty...