iOS推送通知添加自定义头像
苹果提供了将推送通知区分为通信通知的功能,用通信通知可以显示用户的头像等内容。
需要通讯通知 Communication Notifications 来实现
要使用通讯通知,APP 需要在 Xcode 中将通讯通知功能添加到其 APP,并在应用程序通知服务扩展中实现 UNNotificationContentProviding 协议。
1、 首先将以下键值添加到APP Info.plist 文件中
<key>NSUserActivityTypes</key><array><string>INStartCallIntent</string><string>INSendMessageIntent</string></array>
2、在Xcode - Signing & Capabilities 中添加 Communication Notifications 功能
本地通知 - 实现通讯通知
#import<Intents/Intents.h>#import<UserNotifications/UserNotifications.h>
2、通过使用 INPerson 和 INSendMessageIntent 创建对话信息将其添加到APP的推送通知消息中。
完整实现代码如下:
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(iOS15.0,*)){//实现私信消息内容展示if(message.fromUsername&&message.fromAvatar&&message.fromName&&message.msgId){//需要先将图片下载下来,我们这里使用的SDWebImageDownloader下载图片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(@"成功添加推送");}];}
1、首先添加通知扩展(Notification Service Extension)
iOS10 之后的通知具有扩展功能,可以在系统收到通知、展示通知时做一些事情。
2、将以下键值对添加到通知扩展中
<key>NSUserActivityTypes</key><array><string>INStartCallIntent</string><string>INSendMessageIntent</string></array>
3、在通知扩展下 NotificationService 中的以下方法中,实现通讯通知,具体实现方式与本地推送大同小异。
-(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(iOS15.0,*)){//实现消息内容展示// 发送者名称NSString*fromUsername=self.bestAttemptContent.userInfo[@"xxx"];// 发送者头像url地址NSString*fromAvatar=self.bestAttemptContent.userInfo[@"xxx"];// 发送者昵称NSString*fromNickName=self.bestAttemptContent.userInfo[@"xxx"];// 消息 idNSString*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);}}