去年今日此门中,人面桃花相映红。
人面不知何处去,桃花依旧笑春风。
最近公司项目有一个需求,收到推送之后,不论app是前端运行还是没有被启动,都要播报一段音频或者提示语音。网上找了一圈资料之后,发现使用Notification Servivice Extension可以实现该功能。(推送功能请自行完成)具体操作如下:
![](https://img.haomeiwen.com/i2828079/ac41d3444655a0df.png)
![](https://img.haomeiwen.com/i2828079/98d94ba210f61db7.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
网友评论