Swift. Notification Service Extension实现语音播报

总的来说,实现收款到账语音合成播报,真的是走了很长的路,踩过很多的坑。避免更多的人重复踩坑,我便记录走过哪些路,掉过哪些坑...

一. 先来说第一种,不推荐再用。因为苹果粑粑不会给你审核通过的

1.推送就不说了,我们使用的是极光推送
2.语音合成使用的是 AVSpeechSynthesizer
  • 为了app退到后台进程不被杀掉,target -> capabilities中开启了background modes中的audio选项。
    WechatIMG2.jpeg
  • 在语音合成前需要添加下面代码。
    在applicationWillResignActive函数里
//  先注册响应后台控制
UIApplication.shared.beginReceivingRemoteControlEvents()

// 语音合成前需要添加

// 设置并激活音频会话类别
do {
    try AVAudioSession.sharedInstance().setCategory(AVAudioSessionCategoryPlayback, with: .duckOthers)
} catch {
    print(error.localizedDescription)
}
do {
    try AVAudioSession.sharedInstance().setActive(true)
} catch {
    print(error.localizedDescription)
}

// 语音合成在推送回调里调用的

// 调用API
SpeechManager.shared.speechWeather(with: alert as! String)
// 实现主要代码
// 自定义语音播报方法
// 此处只例播报一个String的情况
    func speechWeather(with weather: String) {
        if let _ = speechUtterance {
            synthesizer.stopSpeaking(at: .immediate)
        }
        
        //播放声音
        let path = Bundle.main.path(forResource: "sound", ofType: "wav")
        let pathURL = NSURL(fileURLWithPath: path!)
        do {
            audioPlayer = try AVAudioPlayer(contentsOf: pathURL as URL)
        } catch {
            audioPlayer = nil
        }
        audioPlayer?.delegate = self
        audioPlayer?.prepareToPlay()
        audioPlayer?.play()
        
        speechUtterance = AVSpeechUtterance(string: weather)
        //语音类型
        speechUtterance?.rate = 0.5
        synthesizer.speak(speechUtterance!)
    }
3. 遇到的问题。

1.看似完成了app前台和后台的语音播报,但是app退到后台手动杀掉进程就gg了。

2.还有审核时,由于开启了background modes中的audio选项。拒绝的原因是audio应用的场景不对,
审核被拒.png

二. 最重要的是Notification Service Extension(拓展推送服务)。

1. 在项目中创建notificationservice, 选择 file -> new -> target -> Notification Service Extension
选择NotificationService.jpeg
2. 点击next。
  • product name: 拓展项目服务的名称。
  • bundle identifier: 是原app项目的bundle identifier+拓展项目服务的名称。(原app的bundle identifier为com.test,拓展项目名称为.myService,则拓展项目的bundle identifier为com.test.myService)
    拓展工程.jpeg

创建完成在scheme中会多一个myService

WechatIMG7.jpeg

3.相关配置。
  • 1 首先后端要给你个推送测试内容,内容模板如下:
    要记住在aps里一定要有"mutable -content"这个字段,alert 这个用字符串就可以,不用字典。当然字典也行,后面可以获取里面字符串也行
    后端配置.png
  • 2 在service工程里info.plist中添加App Transport Security Settings并且添加 Allow Arbitrary Loads为YES。


    配置.jpeg
4.主要代码如下。
//
//  NotificationService.swift
//  MyService
//
//  Created by JunQiang on 2018/5/24.
//  Copyright © 2018年 多飞. All rights reserved.
//

import UserNotifications
import AVFoundation

class NotificationService: UNNotificationServiceExtension {

    var contentHandler: ((UNNotificationContent) -> Void)?
    var bestAttemptContent: UNMutableNotificationContent?
    var audioPlayer: AVAudioPlayer?

    override func didReceive(_ request: UNNotificationRequest, withContentHandler contentHandler: @escaping (UNNotificationContent) -> Void) {
        self.contentHandler = contentHandler
        bestAttemptContent = (request.content.mutableCopy() as? UNMutableNotificationContent)
        
        if let bestAttemptContent = bestAttemptContent {
            // Modify the notification content here...
//            bestAttemptContent.title = "收款到账"
            do {
                try AVAudioSession.sharedInstance().setCategory(AVAudioSessionCategoryPlayback, with: .duckOthers)
            } catch {
                print(error.localizedDescription)
            }
            do {
                try AVAudioSession.sharedInstance().setActive(true)
            } catch {
                print(error.localizedDescription)
            }
            //播放收金币声音
            bestAttemptContent.sound = UNNotificationSound.init(named: "sound.wav")

            //语音合成
            let aVSpeechSynthesizer = AVSpeechSynthesizer()
            let aps = bestAttemptContent.userInfo["aps"]
            let str = (aps as! NSDictionary)["alert"];

            let aVSpeechUtterance = AVSpeechUtterance(string: str as! String)
            aVSpeechUtterance.rate = AVSpeechUtteranceDefaultSpeechRate
            aVSpeechUtterance.voice = AVSpeechSynthesisVoice.init(language: "zh-CN")
            aVSpeechSynthesizer.speak(aVSpeechUtterance)
            
            contentHandler(bestAttemptContent)
        }
    }
    
    override func 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.

        if let contentHandler = contentHandler, let bestAttemptContent =  bestAttemptContent {
            contentHandler(bestAttemptContent)
        }
    }
}
4. 支持iOS10以上的手机,修改service中的target -> Deployment Target 选择10.0。
image.png

注意:播放收金币的音频时。
一开始考虑的是AVAudioPlayer播放本地音频。后来发现只有第一次收到推送是有播放sound.wav,其他时候是有语音合成未有播放sound.wav。

//播放声音
let path = Bundle.main.path(forResource: "sound", ofType: "wav")
let pathURL = NSURL(fileURLWithPath: path!)
do {
    audioPlayer = try AVAudioPlayer(contentsOf: pathURL as URL)
} catch {
   audioPlayer = nil
}
audioPlayer?.delegate = self
audioPlayer?.prepareToPlay()
audioPlayer?.play()

经过一番搜索,才发现bestAttemptContent本身是可以播放音频的。改完发现可以了

//播放收金币声音
bestAttemptContent.sound = UNNotificationSound.init(named: "sound.wav")

到此结束,总结不好多多指教....谢谢

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 用到的组件 1、通过CocoaPods安装 2、第三方类库安装 3、第三方服务 友盟社会化分享组件 友盟用户反馈 ...
    SunnyLeong阅读 14,699评论 1 180
  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 173,301评论 25 708
  • 今天在家看了部心理学方面的旧片《催眠大师》,想从以下两个方面来探讨着这部电影: 1、 为什么心理医生徐瑞宁不能治疗...
    水波儿阅读 3,337评论 0 4
  • 导 语 我很有家庭责任感,具体表现就是第一时间租房子和黄怡同居。当然,我答应了黄怡的父母:“给我两年,一定能够赚到...
    飘雨桐V阅读 295评论 1 1
  • 阿多尼斯说: "童年就是一座村庄,你永远也走不出它的边际"。打小就一直呆在老家,小小的村庄是我世界的主体,但越长大...
    阿丁h阅读 487评论 2 7