iOS UNNotificationContent和UNNotificationService

iOS 10.0后推出的推送擴(kuò)展,主要分為兩個部分
1、通知服務(wù)擴(kuò)展(UNNotificationServiceExtension),功能是在收到推送后允許開發(fā)者對推送內(nèi)容做修改,比如添加附件,展示圖片,播放音頻,加載網(wǎng)絡(luò)數(shù)據(jù)等
2、通知內(nèi)容擴(kuò)展(UNNotificationContentExtension),功能是在展示通知欄的時候,可以自定義通知展示界面

UNNotificationServiceExtension 通知服務(wù)擴(kuò)展
在創(chuàng)建這個擴(kuò)展時會生成info.plist NotificationService.m/.h PushExtension.entitlements三個文件

在NotificationService.m文件中,有兩個回調(diào)方法:

// 系統(tǒng)接到通知后,有最多30秒在這里重寫通知內(nèi)容(如下載附件并更新通知)
- (void)didReceiveNotificationRequest:(UNNotificationRequest *)request withContentHandler:(void (^)(UNNotificationContent *contentToDeliver))contentHandler;
// 處理過程超時,則收到的通知直接展示出來
- (void)serviceExtensionTimeWillExpire;

如上所看,具體的修改內(nèi)容就是在第一個方法中,下面會提供基礎(chǔ)的操作代碼,可根據(jù)自己的項(xiàng)目做調(diào)整

注意?。。。。?!
加載并處理附件時間上限為30秒,否則,通知按系統(tǒng)默認(rèn)形式彈出;
UNNotificationAttachment的url接收的是本地文件的url;
服務(wù)端在處理推送內(nèi)容時,最好加上媒體類型字段;
aps字符串中的mutable-content字段需要設(shè)置為1;表示可修改,否則不走擴(kuò)展
在對NotificationService進(jìn)行debug時,需要在Xcode頂欄選擇編譯運(yùn)行的target為NotificationService,否則無法進(jìn)行實(shí)時debug。

UNNotificationAttachment:attachment支持
1)音頻5M(kUTTypeWaveformAudio/kUTTypeMP3/kUTTypeMPEG4Audio/kUTTypeAudioInterchangeFileFormat)
2)圖片10M(kUTTypeJPEG/kUTTypeGIF/kUTTypePNG)
3)視頻50M(kUTTypeMPEG/kUTTypeMPEG2Video/kUTTypeMPEG4/kUTTypeAVIMovie)

消息內(nèi)容格式:

 {"aps":{"alert":{"title":"Title...","subtitle":"Subtitle...","body":"Body..."},"sound":"default","badge": 1,"mutable-content": 1,"category": "QiShareCategoryIdentifier",},"msgid":"123","media":{"type":"image","url":"[www.fotor.com/images2/fea…](https://link.juejin.im/?target=https%3A%2F%2Fwww.fotor.com%2Fimages2%2Ffeatures%2Fphoto_effects%2Fe_bw.jpg)"}}

UNNotificationContentExtension 通知內(nèi)容擴(kuò)展

通知內(nèi)容擴(kuò)展界面NotificationViewController的結(jié)構(gòu)如下:
設(shè)置actions: 從NotificationViewController直接繼承于ViewController,因此可以在這個類中重寫相關(guān)方法,來修改界面的相關(guān)布局及樣式。在這個界面展開之前,用戶可以通過UNNotificationAction與相應(yīng)推送通知交互,但是用戶和這個通知內(nèi)容擴(kuò)展界面無法直接交互。
設(shè)置category: 推送通知內(nèi)容中的category字段,與UNNotificationContentExtension的info.plist中UNNotificationExtensionCategory字段的值要匹配,系統(tǒng)才能找到自定義的UI。

在Info.plist添加交互標(biāo)識


標(biāo)識樣式展示

自定義視圖的大小可以通過設(shè)置NotificationViewController的preferredContentSize大小來控制,
但是用戶體驗(yàn)稍顯突兀,可以通過設(shè)置info.plist中的UNNotificationExtensionInitialContentSizeRatio屬性的值來優(yōu)化;
contentExtension中的info.plist中NSExtension下NSExtensionAttributes字段下可以配置以下屬性的值,
UNNotificationExtensionCategory:表示自定義內(nèi)容假面可以識別的category,可以為數(shù)組,也即可以為這個content綁定多個通知;
UNNotificationExtensionInitialContentSizeRatio:默認(rèn)的UI界面的寬高比;
UNNotificationExtensionDefaultContentHidden:是否顯示系統(tǒng)默認(rèn)的標(biāo)題欄和內(nèi)容,可選參數(shù);UNNotificationExtensionOverridesDefaultTitle:是否讓系統(tǒng)采用消息的標(biāo)題作為通知的標(biāo)題,可選參數(shù)。
處理通知內(nèi)容擴(kuò)展的過程中關(guān)于identifier的設(shè)置共有五處(UNNotificationAction、UNNotificationCategory、bestAttemptContent、contentExtension中的info.plist中,aps字符串中),請區(qū)別不同identifier的作用。
兩個擴(kuò)展聯(lián)合使用,在XCode中選擇當(dāng)前target,才能打斷點(diǎn)看到相應(yīng)log信息。

消息格式內(nèi)容

 { "aps":{ "alert":"Testing...(0)","badge":1,"sound":"default","category":"QiShareCategoryIdentifier"}}

在NotificationService.m中設(shè)置category的值如下:
self.bestAttemptContent.categoryIdentifier = @"QiShareCategoryIdentifier";

info.plist中關(guān)于category的配置如下:
UNNotificationContentExtension協(xié)議:NotificationViewController 中生成時默認(rèn)實(shí)現(xiàn)了。

NotificationService代碼

#import "NotificationService.h"
#import <AVFoundation/AVFoundation.h>
 
@interface NotificationService ()
 
@property (nonatomic, strong) void (^contentHandler)(UNNotificationContent *contentToDeliver);
@property (nonatomic, strong) UNMutableNotificationContent *bestAttemptContent;
 
@end
 
@implementation NotificationService
 
- (void)didReceiveNotificationRequest:(UNNotificationRequest *)request withContentHandler:(void (^)(UNNotificationContent * _Nonnull))contentHandler {
//獲取回調(diào)block ,這個回調(diào)必須實(shí)現(xiàn),在修改數(shù)據(jù)后,這個回調(diào)會把修改的數(shù)據(jù)回調(diào)給系統(tǒng)做展示    
self.contentHandler = contentHandler;
    self.bestAttemptContent = [request.content mutableCopy];
//通知的標(biāo)題
    self.bestAttemptContent.title = [NSString stringWithFormat:@"%@ [ServiceExtension modified]", self.bestAttemptContent.title];
    
    // 設(shè)置UNNotificationAction 類似于微信的回復(fù),頭條的查看等按紐,按鈕僅可修改位置、顏色、大小等,添加這些按鈕,必須在info.plist 文件中添加對于的標(biāo)識!下面附添加標(biāo)識的圖片

    UNNotificationAction * actionA  =[UNNotificationAction actionWithIdentifier:@"ActionA" title:@"A_Required" options:UNNotificationActionOptionAuthenticationRequired];
    UNNotificationAction * actionB = [UNNotificationAction actionWithIdentifier:@"ActionB" title:@"B_Destructive" options:UNNotificationActionOptionDestructive];
    UNNotificationAction * actionC = [UNNotificationAction actionWithIdentifier:@"ActionC" title:@"C_Foreground" options:UNNotificationActionOptionForeground];
    UNTextInputNotificationAction * actionD = [UNTextInputNotificationAction actionWithIdentifier:@"ActionD"
                                                                                            title:@"D_InputDestructive"
                                                                                          options:UNNotificationActionOptionDestructive
                                                                             textInputButtonTitle:@"Send"
                                                                             textInputPlaceholder:@"input some words here ..."];
    NSArray *actionArr = [[NSArray alloc] initWithObjects:actionA, actionB, actionC, actionD, nil];
    NSArray *identifierArr = [[NSArray alloc] initWithObjects:@"ActionA", @"ActionB", @"ActionC", @"ActionD", nil];
    UNNotificationCategory * notficationCategory = [UNNotificationCategory categoryWithIdentifier:@"QiShareCategoryIdentifier"
                                                                                          actions:actionArr
                                                                                intentIdentifiers:identifierArr
                                                                                          options:UNNotificationCategoryOptionCustomDismissAction];
    [[UNUserNotificationCenter currentNotificationCenter] setNotificationCategories:[NSSet setWithObject:notficationCategory]];
    
    // 設(shè)置categoryIdentifier 這個就是在info.plist 中添加的標(biāo)識,必須一一對應(yīng)
    self.bestAttemptContent.categoryIdentifier = @"QiShareCategoryIdentifier";
    
    // 加載網(wǎng)絡(luò)請求 比如網(wǎng)絡(luò)圖片等
    NSDictionary *userInfo =  self.bestAttemptContent.userInfo;
    NSString *mediaUrl = userInfo[@"media"][@"url"];
    NSString *mediaType = userInfo[@"media"][@"type"];
    if (!mediaUrl.length) {
        self.contentHandler(self.bestAttemptContent);
    } else {
        [self loadAttachmentForUrlString:mediaUrl withType:mediaType completionHandle:^(UNNotificationAttachment *attach) {
            
            if (attach) {
                self.bestAttemptContent.attachments = [NSArray arrayWithObject:attach];
            }
            //將數(shù)據(jù)回調(diào)給系統(tǒng)展示通知
            self.contentHandler(self.bestAttemptContent);
        }];
    }
}
 //加載網(wǎng)絡(luò)圖片
- (void)loadAttachmentForUrlString:(NSString *)urlStr withType:(NSString *)type completionHandle:(void(^)(UNNotificationAttachment *attach))completionHandler
{
    __block UNNotificationAttachment *attachment = nil;
    NSURL *attachmentURL = [NSURL URLWithString:urlStr];
    NSString *fileExt = [self getfileExtWithMediaType:type];
    
    NSURLSession *session = [NSURLSession sessionWithConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration]];
    [[session downloadTaskWithURL:attachmentURL completionHandler:^(NSURL *temporaryFileLocation, NSURLResponse *response, NSError *error) {
        if (error) {
            NSLog(@"加載多媒體失敗 %@", error.localizedDescription);
        } else {
            NSFileManager *fileManager = [NSFileManager defaultManager];
            NSURL *localURL = [NSURL fileURLWithPath:[temporaryFileLocation.path stringByAppendingString:fileExt]];
            [fileManager moveItemAtURL:temporaryFileLocation toURL:localURL error:&error];
            
            // 自定義推送UI需要
            NSMutableDictionary * dict = [self.bestAttemptContent.userInfo mutableCopy];
            [dict setObject:[NSData dataWithContentsOfURL:localURL] forKey:@"image"];
            self.bestAttemptContent.userInfo = dict;
            
            NSError *attachmentError = nil;
            attachment = [UNNotificationAttachment attachmentWithIdentifier:@"QiShareCategoryIdentifier" URL:localURL options:nil error:&attachmentError];
            if (attachmentError) {
                NSLog(@"%@", attachmentError.localizedDescription);
            }
        }
        completionHandler(attachment);
    }] resume];
}
 
- (NSString *)getfileExtWithMediaType:(NSString *)mediaType {
    NSString *fileExt = mediaType;
    if ([mediaType isEqualToString:@"image"]) {
        fileExt = @"jpg";
    }
    if ([mediaType isEqualToString:@"video"]) {
        fileExt = @"mp4";
    }
    if ([mediaType isEqualToString:@"audio"]) {
        fileExt = @"mp3";
    }
    return [@"." stringByAppendingString:fileExt];
}
 
- (void)serviceExtensionTimeWillExpire {
    // Called just before the extension will be terminated by the system.
    // Use this as an opportunity to deliver your "best attempt" at modified content, otherwise the original push payload will be used.
    self.contentHandler(self.bestAttemptContent);
}
 
@end

NotificationView 代碼

#import "NotificationViewController.h"
#import <UserNotifications/UserNotifications.h>
#import <UserNotificationsUI/UserNotificationsUI.h>
 
#define Margin      15
 
@interface NotificationViewController () <UNNotificationContentExtension>
 
@property (nonatomic, strong) UILabel *label;
@property (nonatomic, strong) UILabel *subLabel;
@property (nonatomic, strong) UIImageView *imageView;
 
@property (nonatomic, strong) UILabel *hintLabel;
 
@end
 
@implementation NotificationViewController
 
- (void)viewDidLoad {
 
    [super viewDidLoad];
    
    CGPoint origin = self.view.frame.origin;
    CGSize size = self.view.frame.size;
    
    self.label = [[UILabel alloc] initWithFrame:CGRectMake(Margin, Margin, size.width-Margin*2, 30)];
    self.label.autoresizingMask = UIViewAutoresizingFlexibleWidth;
    [self.view addSubview:self.label];
    
    self.subLabel = [[UILabel alloc] initWithFrame:CGRectMake(Margin, CGRectGetMaxY(self.label.frame)+10, size.width-Margin*2, 30)];
    self.subLabel.autoresizingMask = UIViewAutoresizingFlexibleWidth;
    [self.view addSubview:self.subLabel];
    
    self.imageView = [[UIImageView alloc] initWithFrame:CGRectMake(Margin, CGRectGetMaxY(self.subLabel.frame)+10, 100, 100)];
    [self.view addSubview:self.imageView];
    
    self.hintLabel = [[UILabel alloc] initWithFrame:CGRectMake(Margin, CGRectGetMaxY(self.imageView.frame)+10, size.width-Margin*2, 20)];
    [self.hintLabel setText:@"我是hintLabel"];
    [self.hintLabel setFont:[UIFont systemFontOfSize:14]];
    [self.hintLabel setTextAlignment:NSTextAlignmentLeft];
    [self.view addSubview:self.hintLabel];
    self.view.frame = CGRectMake(origin.x, origin.y, size.width, CGRectGetMaxY(self.imageView.frame)+Margin);
 
    // 設(shè)置控件邊框顏色
    [self.label.layer setBorderColor:[UIColor redColor].CGColor];
    [self.label.layer setBorderWidth:1.0];
    [self.subLabel.layer setBorderColor:[UIColor greenColor].CGColor];
    [self.subLabel.layer setBorderWidth:1.0];
    [self.imageView.layer setBorderWidth:2.0];
    [self.imageView.layer setBorderColor:[UIColor blueColor].CGColor];
    [self.view.layer setBorderWidth:2.0];
    [self.view.layer setBorderColor:[UIColor cyanColor].CGColor];
}
 
- (void)didReceiveNotification:(UNNotification *)notification {
    
    self.label.text = notification.request.content.title;
    self.subLabel.text = [NSString stringWithFormat:@"%@ [ContentExtension modified]", notification.request.content.subtitle];
    
    NSData *data = notification.request.content.userInfo[@"image"];
    UIImage *image = [UIImage imageWithData:data];
    [self.imageView setImage:image];
}
 
- (void)didReceiveNotificationResponse:(UNNotificationResponse *)response completionHandler:(void (^)(UNNotificationContentExtensionResponseOption))completion {
    
    [self.hintLabel setText:[NSString stringWithFormat:@"觸發(fā)了%@", response.actionIdentifier]];
    if ([response.actionIdentifier isEqualToString:@"ActionA"]) {
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            completion(UNNotificationContentExtensionResponseOptionDismiss);
        });
    } else if ([response.actionIdentifier isEqualToString:@"ActionB"]) {
 
    } else if ([response.actionIdentifier isEqualToString:@"ActionC"]) {
 
    }  else if ([response.actionIdentifier isEqualToString:@"ActionD"]) {
 
    } else {
        completion(UNNotificationContentExtensionResponseOptionDismiss);
    }
    completion(UNNotificationContentExtensionResponseOptionDoNotDismiss);
}
 
@end

更新,10.0-12.0 后臺推送語音播報可以使用系統(tǒng)自帶的文字合成語音播報AVSpeechSynthesisVoice或者內(nèi)置幾段語音進(jìn)行合成后再進(jìn)行播放
分別例子:
系統(tǒng)合成

av= [[AVSpeechSynthesizer alloc]init];
av.delegate=self;//掛上代理
AVSpeechSynthesisVoice*voice = [AVSpeechSynthesisVoicevoiceWithLanguage:@"zh-CN"];//設(shè)置發(fā)音,這是中文普通話
AVSpeechUtterance*utterance = [[AVSpeechUtterance   alloc]initWithString:@"需要播報的文字"];//需要轉(zhuǎn)換的文字
utterance.rate=0.6;// 設(shè)置語速,范圍0-1,注意0最慢,1最快;
utterance.voice= voice;
[avspeakUtterance:utterance];//開始

內(nèi)置語音

- (void)audioMergeClick{
//1.獲取本地音頻素材
    NSString *audioPath1 = [[NSBundle mainBundle]pathForResource:@"一" ofType:@"mp3"];
    NSString *audioPath2 = [[NSBundle mainBundle]pathForResource:@"元" ofType:@"mp3"];
    AVURLAsset *audioAsset1 = [AVURLAsset assetWithURL:[NSURL fileURLWithPath:audioPath1]];
    AVURLAsset *audioAsset2 = [AVURLAsset assetWithURL:[NSURL fileURLWithPath:audioPath2]];
//2.創(chuàng)建兩個音頻軌道,并獲取兩個音頻素材的軌道
    AVMutableComposition *composition = [AVMutableComposition composition];
    //音頻軌道
    AVMutableCompositionTrack *audioTrack1 = [composition addMutableTrackWithMediaType:AVMediaTypeAudio preferredTrackID:0];
    AVMutableCompositionTrack *audioTrack2 = [composition addMutableTrackWithMediaType:AVMediaTypeAudio preferredTrackID:0];
    //獲取音頻素材軌道
    AVAssetTrack *audioAssetTrack1 = [[audioAsset1 tracksWithMediaType:AVMediaTypeAudio] firstObject];
    AVAssetTrack *audioAssetTrack2 = [[audioAsset2 tracksWithMediaType:AVMediaTypeAudio]firstObject];
//3.將兩段音頻插入音軌文件,進(jìn)行合并
    //音頻合并- 插入音軌文件
    // `startTime`參數(shù)要設(shè)置為第一段音頻的時長,即`audioAsset1.duration`, 表示將第二段音頻插入到第一段音頻的尾部。

    [audioTrack1 insertTimeRange:CMTimeRangeMake(kCMTimeZero, audioAsset1.duration) ofTrack:audioAssetTrack1 atTime:kCMTimeZero error:nil];
    [audioTrack2 insertTimeRange:CMTimeRangeMake(kCMTimeZero, audioAsset2.duration) ofTrack:audioAssetTrack2 atTime:audioAsset1.duration error:nil];
//4. 導(dǎo)出合并后的音頻文件
    //`presetName`要和之后的`session.outputFileType`相對應(yīng)
    //音頻文件目前只找到支持m4a 類型的
    AVAssetExportSession *session = [[AVAssetExportSession alloc]initWithAsset:composition presetName:AVAssetExportPresetAppleM4A];
    
    NSString *outPutFilePath = [[self.filePath stringByDeletingLastPathComponent] stringByAppendingPathComponent:@"xindong.m4a"];
    
    if ([[NSFileManager defaultManager] fileExistsAtPath:outPutFilePath]) {
        [[NSFileManager defaultManager] removeItemAtPath:outPutFilePath error:nil];
    }
    // 查看當(dāng)前session支持的fileType類型
    NSLog(@"---%@",[session supportedFileTypes]);
    session.outputURL = [NSURL fileURLWithPath:self.filePath];
    session.outputFileType = AVFileTypeAppleM4A; //與上述的`present`相對應(yīng)
    session.shouldOptimizeForNetworkUse = YES;   //優(yōu)化網(wǎng)絡(luò)
    [session exportAsynchronouslyWithCompletionHandler:^{
        if (session.status == AVAssetExportSessionStatusCompleted) {
            NSLog(@"合并成功----%@", outPutFilePath);
            _audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:outPutFilePath] error:nil];
            [_audioPlayer play];
        } else {
            // 其他情況, 具體請看這里`AVAssetExportSessionStatus`.
        }
    }];
    
}
- (NSString *)filePath {
    if (!_filePath) {
        _filePath = [NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES) firstObject];
        NSString *folderName = [_filePath stringByAppendingPathComponent:@"MergeAudio"];
        BOOL isCreateSuccess = [kFileManager createDirectoryAtPath:folderName withIntermediateDirectories:YES attributes:nil error:nil];
        if (isCreateSuccess) _filePath = [folderName stringByAppendingPathComponent:@"xindong.m4a"];
    }
    return _filePath;
}

官方12.1之后,在這個推送擴(kuò)展里面AVAudioPlayer失效
可以播放工程主目錄和Library/Sounds,還可以播放AppGroup中Library/Sounds的音頻 ,我們可以在后臺合成,然后下載到AppGroup后修改sound字段進(jìn)行播放,首先打開我們項(xiàng)目的AppGroup,在主項(xiàng)目和推送擴(kuò)展里都要勾選AppGroup
之后接到通知,解析出下載鏈接,下載完在本地修改sound字段,交由系統(tǒng)播報

- (void)didReceiveNotificationRequest:(UNNotificationRequest *)request withContentHandler:(void (^)(UNNotificationContent * _Nonnull))contentHandler {
    self.contentHandler = contentHandler;
    self.bestAttemptContent = [request.content mutableCopy];
    
    // Modify the notification content here...
    self.bestAttemptContent.title = [NSString stringWithFormat:@"%@ [modified]", self.bestAttemptContent.title];
   
    // 這個info 內(nèi)容就是通知信息攜帶的數(shù)據(jù),后面我們?nèi)≌Z音播報的文案,通知欄的title,以及通知內(nèi)容都是從這個info字段中獲取
    NSDictionary *info = self.bestAttemptContent.userInfo;
    NSString * urlStr = [info objectForKey:@"soundUrl"];
    [self loadWavWithUrl:urlStr];
    
//    self.contentHandler(self.bestAttemptContent);
}
-(void)loadWavWithUrl:(NSString *)urlStr{
    NSLog(@"開始下載");
    NSURL *url = [NSURL URLWithString:urlStr];
       //默認(rèn)的congig
    NSURLSessionConfiguration *config = [NSURLSessionConfiguration defaultSessionConfiguration];
    
    //session
    NSURLSession *session = [NSURLSession sessionWithConfiguration:config delegate:self delegateQueue:[NSOperationQueue mainQueue]];
    self.task = [session downloadTaskWithURL:url completionHandler:^(NSURL * _Nullable location, NSURLResponse * _Nullable response, NSError * _Nullable error) {
        if (!error) {
            NSLog(@"下載完成");
            NSString * name = [NSString stringWithFormat:@"%u.wav",arc4random()%50000 ];
             //獲取保存文件的路徑
             NSString *path = self.filePath;
             //將url對應(yīng)的文件copy到指定的路徑

             NSFileManager *fileManager = [NSFileManager defaultManager];
             if(![fileManager fileExistsAtPath:path]){
                 [fileManager createDirectoryAtPath:path withIntermediateDirectories:YES attributes:nil error:nil];
             }
             NSString * soundStr = [NSString stringWithFormat:@"%@",name];

             NSString *savePath = [path stringByAppendingPathComponent:[NSString stringWithFormat:@"%@",soundStr]];
             if ([fileManager fileExistsAtPath:savePath]) {
                 [fileManager removeItemAtPath:savePath error:nil];
                }
             NSURL *saveURL = [NSURL fileURLWithPath:savePath];
            
             NSError * saveError;
             // 文件移動到cache路徑中
             [[NSFileManager defaultManager] moveItemAtURL:location toURL:saveURL error:&saveError];
             if (!saveError)
             {
                 AVURLAsset *audioAsset=[AVURLAsset URLAssetWithURL:saveURL options:nil];
                 self.bestAttemptContent.sound = soundStr;
                 self.contentHandler(self.bestAttemptContent);
             }

        }else{
            
            NSLog(@"失敗");
        }
         
    }];
    
    //啟動下載任務(wù)
    [_task resume];
}
- (NSString *)filePath {
    if (_filePath) {
        return _filePath;
    }
    NSURL *groupURL = [[NSFileManager defaultManager] containerURLForSecurityApplicationGroupIdentifier:@"group.com.jiutianyunzhu.BPMall"];
    NSString *groupPath = [groupURL path];

     _filePath = [groupPath stringByAppendingPathComponent:@"Library/Sounds"];
    NSFileManager *fileManager = [NSFileManager defaultManager];
    if (![fileManager fileExistsAtPath:_filePath]) {
        [fileManager createDirectoryAtPath:_filePath withIntermediateDirectories:NO attributes:nil error:nil];
    }
    return _filePath;
}

當(dāng)音頻下載處理完成后記得調(diào)用self.contentHandler(self.bestAttemptContent);
只有當(dāng)調(diào)用self.contentHandler(self.bestAttemptContent);之后,才會彈出頂部橫幅,并開始播報,橫幅消失時音頻會停止,實(shí)測橫幅時長大概6s!所以音頻需要處理控制在6s之內(nèi)!

注意!?。?!
1.網(wǎng)上大都說支持三種格式 aiff、caf以及wav,但實(shí)測也支持MP3格式
2.處理完成后一定要記得調(diào)用 self.contentHandler(self.bestAttemptContent);,否則不會出現(xiàn)通知橫幅
3.下載失敗最好準(zhǔn)備一段默認(rèn)語音播報
4.多條推送同時到達(dá)問題,可以寫個隊(duì)列,調(diào)用self.contentHandler(self.bestAttemptContent);后,主動去阻塞線程一定的時長(音頻時長),播放完成后記得刪除掉!

本文借鑒

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容