蘋果提供了將推送通知區(qū)分為通信通知的功能是鬼,用通信通知可以顯示用戶的頭像等內(nèi)容肤舞。
需要通訊通知 Communication Notifications 來(lái)實(shí)現(xiàn)
要使用通訊通知,APP 需要在 Xcode 中將通訊通知功能添加到其 APP均蜜,并在應(yīng)用程序通知服務(wù)擴(kuò)展中實(shí)現(xiàn) UNNotificationContentProviding 協(xié)議萨赁。
1、 首先將以下鍵值添加到APP Info.plist 文件中
<key>NSUserActivityTypes</key><array><string>INStartCallIntent</string><string>INSendMessageIntent</string></array>
2兆龙、在Xcode?-?Signing & Capabilities?中添加?Communication Notifications?功能
本地通知 - 實(shí)現(xiàn)通訊通知
#import<Intents/Intents.h>#import<UserNotifications/UserNotifications.h>
2、通過(guò)使用?INPerson 和 INSendMessageIntent 創(chuàng)建對(duì)話信息將其添加到APP的推送通知消息中紫皇。
完整實(shí)現(xiàn)代碼如下:
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,*)){//實(shí)現(xiàn)私信消息內(nèi)容展示if(message.fromUsername&&message.fromAvatar&&message.fromName&&message.msgId){//需要先將圖片下載下來(lái)慰安,我們這里使用的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];}// 消息發(fā)送方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(@"成功添加推送");}];}
遠(yuǎn)程通知 - 實(shí)現(xiàn)通訊通知
1、首先添加通知擴(kuò)展(Notification Service Extension)
iOS10 之后的通知具有擴(kuò)展功能聪铺,可以在系統(tǒng)收到通知化焕、展示通知時(shí)做一些事情。
2铃剔、將以下鍵值對(duì)添加到通知擴(kuò)展中
<key>NSUserActivityTypes</key><array><string>INStartCallIntent</string><string>INSendMessageIntent</string></array>
3撒桨、在通知擴(kuò)展下?NotificationService?中的以下方法中,實(shí)現(xiàn)通訊通知键兜,具體實(shí)現(xiàn)方式與本地推送大同小異凤类。
-(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,*)){//實(shí)現(xiàn)消息內(nèi)容展示// 發(fā)送者名稱NSString*fromUsername=self.bestAttemptContent.userInfo[@"xxx"];// 發(fā)送者頭像url地址NSString*fromAvatar=self.bestAttemptContent.userInfo[@"xxx"];// 發(fā)送者昵稱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];}// 消息發(fā)送方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);}}