美文网首页
swift之推送播放

swift之推送播放

作者: 夏至樱花祭 | 来源:发表于2019-04-12 14:15 被阅读0次

去年今日此门中,人面桃花相映红。
人面不知何处去,桃花依旧笑春风。

最近公司项目有一个需求,收到推送之后,不论app是前端运行还是没有被启动,都要播报一段音频或者提示语音。网上找了一圈资料之后,发现使用Notification Servivice Extension可以实现该功能。(推送功能请自行完成)具体操作如下:


image.png image.png

然后找到创建的服务(苹果创建这个服务是oc代码,并不需要添加到桥接文件中);实现其中的功能,此处的代码在运行的时候是不能打断点调试的;

#import "NotificationService.h"
#import <AVFoundation/AVFoundation.h>
#import <AudioToolbox/AudioToolbox.h>
typedef void(^PlayVoiceBlock)(void);

@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 {
    self.contentHandler = contentHandler;
    self.bestAttemptContent = [request.content mutableCopy];
    
    UNNotificationContent *content = request.content;
//    NSDictionary *userInfo = content.userInfo;
    
    // Modify the notification content here...
    self.bestAttemptContent.title = [NSString stringWithFormat:@"%@", self.bestAttemptContent.title];
    
//  将文字转为语音播放
//[self playVoiceWithAVSpeechSynthesisVoiceWithContent:content.body];

    if ([content.title isEqualToString:@"xx通知"]) {
        [self alertVoiceWithFinshBlock:@"fire" block:^{
            self.contentHandler(self.bestAttemptContent);
        }];
    }else if ([content.title isEqualToString:@"xx通知"]) {

        [self alertVoiceWithFinshBlock:@"smoke" block:^{
            self.contentHandler(self.bestAttemptContent);
        }];
    }else{
        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.
    self.contentHandler(self.bestAttemptContent);
}
- (void)alertVoiceWithFinshBlock:(NSString *)musicName block:(PlayVoiceBlock )block{
    
    [self playAudioWithNotifiType:musicName];
    
}

//播放音效
- (void)playAudioWithNotifiType:(NSString *)musicName{
    SystemSoundID  soundID = 0;
    NSString       * audioPath = [[NSBundle mainBundle]pathForResource:musicName ofType:@"wav"];
    NSURL          * filePath = [NSURL fileURLWithPath:audioPath];
    AudioServicesCreateSystemSoundID(CFBridgingRetain(filePath), &soundID);
    AudioServicesPlayAlertSound(soundID);
}

- (void)playVoiceWithAVSpeechSynthesisVoiceWithContent:(NSString *)content
{
    if (content.length == 0) {
        return;
    }
    // 创建嗓音,指定嗓音不存在则返回nil
    AVSpeechSynthesisVoice *voice = [AVSpeechSynthesisVoice voiceWithLanguage:@"zh-CN"];
    
    // 创建语音合成器
    AVSpeechSynthesizer *synthesizer = [[AVSpeechSynthesizer alloc] init];
    
    // 实例化发声的对象
    AVSpeechUtterance *utterance = [AVSpeechUtterance speechUtteranceWithString:content];
    utterance.voice = voice;
    utterance.rate = 0.5; // 语速
    // 朗读的内容
    [synthesizer speakUtterance:utterance];
}
@end

做推送测试的时候,调试比较麻烦,网上找到一个比较好用的测试工具,推荐给需要的伙伴。
https://github.com/jamesczy/iOSPhsh

相关文章

网友评论

      本文标题:swift之推送播放

      本文链接:https://www.haomeiwen.com/subject/ospqwqtx.html