推送扩展 Demo

import "NotificationService.h"

import

@interface NotificationService ()

@property (nonatomic, strong) void (^contentHandler)(UNNotificationContent *contentToDeliver);

@property (nonatomic, strong) UNMutableNotificationContent *bestAttemptContent;

/** 语音合成引擎 */
@property (nonatomic, strong) AVSpeechSynthesizer *voiceSpeaker;

/** 弹框是否已经展示 */
@property (nonatomic, assign) BOOL alertIsDisplayed;

@end
PS:这里通过判断文字个数来控制通知栏什么时候显示

/** 文字限制长度 暂定为15字*/
static int contentLengthLimit = 18;

  • (void)didReceiveNotificationRequest:(UNNotificationRequest *)request withContentHandler:(void (^)(UNNotificationContent * _Nonnull))contentHandler {
    self.contentHandler = contentHandler;
    self.bestAttemptContent = [request.content mutableCopy];

    NSError *activeErr = nil;
    NSError *cateroyErr = nil;

    [[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback error:&cateroyErr];
    [[AVAudioSession sharedInstance] setActive:YES error:&activeErr];

    self.alertIsDisplayed = NO;

    // Modify the notification content here...
    self.bestAttemptContent.title = [NSString stringWithFormat:@"%@By涛声", self.bestAttemptContent.title];

    [self speakStringFromServer:self.bestAttemptContent.body];
    }

pragma mark - 处理并播放服务器返回的内容

  • (void)speakStringFromServer:(NSString *)string {

    if (string.length == 0) {

      // 如果字符串长度为0,则直接弹出通知Alert,不执行任何操作 通知栏的隐藏不需要我们来控制,因为5秒左右的生命周期结束后,它会自动隐藏
      self.alertIsDisplayed = YES;
      self.contentHandler(self.bestAttemptContent);
      
      return;
    

    }

    // 如果文字过长的话,会导致文字播放到一半时出现通知的声音,故将通知声音关闭
    self.bestAttemptContent.sound = nil;

    if (string.length <= contentLengthLimit) {

      // 如果文字长度较短的话则直接弹出通知栏并且开启通知声音
      self.bestAttemptContent.sound = [UNNotificationSound defaultSound];
      
      // 如果需要播放的内容长度小于限制长度,5秒时间足以播放完毕,则直接弹出Alert。
      self.alertIsDisplayed = YES;
      self.contentHandler(self.bestAttemptContent);
    

    }

    NSString *needStr = [self getNumberFromString:string];

    [needStr stringByAppendingString:@""];

    NSString *tempStr = [NSString stringWithFormat:@",%@", needStr];

    NSString *finalStr = [string stringByReplacingOccurrencesOfString:needStr withString:tempStr];

    [self speakString:finalStr];

}

  • (void)speakString:(NSString *)string {

    if (self.voiceSpeaker) {

      AVSpeechUtterance *aUtterance = [AVSpeechUtterance speechUtteranceWithString:string];
      
      [aUtterance setVoice:[AVSpeechSynthesisVoice voiceWithLanguage:@"zh-TW"]];
      
      aUtterance.rate = 0.5; //设置语速
      
      aUtterance.volume = 1;  //设置音量(0.0~1.0)默认为1.0
      
      aUtterance.pitchMultiplier = 1;  //设置语调 (0.5-2.0)
      
      [self.voiceSpeaker speakUtterance:aUtterance];
    

    }
    }

pragma mark - 获取字符串中的数字 以便在数值处特意停顿一下

  • (NSString *)getNumberFromString:(NSString *)string {

    NSScanner *scanner = [NSScanner scannerWithString:string];

    [scanner scanUpToCharactersFromSet:[NSCharacterSet decimalDigitCharacterSet] intoString:nil];

    float num ;

    [scanner scanFloat:&num];

    NSString *tempStr = [NSString stringWithFormat:@"%.2f", num];

    NSDecimalNumber *resultNum = [NSDecimalNumber decimalNumberWithString:tempStr];

    NSString *resultStr = [NSString stringWithFormat:@"%@", resultNum];

    return resultStr;
    }

pragma mark - AVSpeechSynthesizerDelegate

  • (void)speechSynthesizer:(AVSpeechSynthesizer *)synthesizer didStartSpeechUtterance:(AVSpeechUtterance *)utterance
    {
    NSLog(@"开始");
    }

  • (void)speechSynthesizer:(AVSpeechSynthesizer *)synthesizer didFinishSpeechUtterance:(AVSpeechUtterance *)utterance
    {
    NSLog(@"结束");
    }

  • (void)speechSynthesizer:(AVSpeechSynthesizer *)synthesizer didPauseSpeechUtterance:(AVSpeechUtterance *)utterance
    {
    NSLog(@"暂停");
    }

  • (void)speechSynthesizer:(AVSpeechSynthesizer *)synthesizer didContinueSpeechUtterance:(AVSpeechUtterance *)utterance
    {
    NSLog(@"继续");
    }

  • (void)speechSynthesizer:(AVSpeechSynthesizer *)synthesizer didCancelSpeechUtterance:(AVSpeechUtterance *)utterance
    {
    NSLog(@"取消");
    self.alertIsDisplayed = YES;
    self.contentHandler(self.bestAttemptContent);
    }

  • (void)speechSynthesizer:(AVSpeechSynthesizer *)synthesizer willSpeakRangeOfSpeechString:(NSRange)characterRange utterance:(AVSpeechUtterance *)utterance {

    if (self.alertIsDisplayed == NO) {

      if (characterRange.location >= utterance.speechString.length / 4) {
          
          // 如果文字长度大于限制,则可能通知栏弹出5秒内无法播放完毕,则暂定为播放到1/4时再弹出状态栏
          self.alertIsDisplayed = YES;
          self.contentHandler(self.bestAttemptContent);
      }
    

    }
    }

  • (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.

    // 当一条通知达到后,如果在30秒内,还没有呼出通知栏,系统就强制调用来呼出通知栏
    self.alertIsDisplayed = YES;
    self.contentHandler(self.bestAttemptContent);
    }

pragma mark - 懒加载

  • (AVSpeechSynthesizer *)voiceSpeaker {

    if (!_voiceSpeaker) {

      _voiceSpeaker = [[AVSpeechSynthesizer alloc] init];
      
      _voiceSpeaker.delegate = self;
    

    }

    return _voiceSpeaker;
    }

你可能感兴趣的:(推送扩展 Demo)