美文网首页
ios - App 杀死或退到后台语音播报收到的推送内容&Not

ios - App 杀死或退到后台语音播报收到的推送内容&Not

作者: Superman168 | 来源:发表于2019-04-16 16:52 被阅读0次

    前言

    之前开发中遇到的一个需求,APP 收到推送消息,然后语音播报一下内容,类似支付宝和微信的收付款语音提醒,没说后台必须也可以播放,但有一点是:收到多少推送消息,就播多少条!!!这个在之前的文章中也简单总结了一下:

    ios 语音合成小结及遇到的一些问题

    但是当时也尝试了不同的方法,让APP 在后台或者杀死依然能够播放收到的推送消息,这就是苹果的 NotificationServiceExtension 推送扩展,不多仅支持 iOS 10.0 以后

    'UNNotificationServiceExtension' is only available on iOS 10.0 or newer

    NotificationServiceExtension 创建

    首先,先开启相关权限:

    推送权限 后台权限
    1. 在 file ->New ->Target


      image.png
    2. 或者在 Target 下点击加号新建


      image.png

    选择 NotificationServiceExtension 选项新建 NotificationService

    image.png image.png

    这样就创建好了,NotificationService.m文件内的代码,这就是iOS10的推送扩展:

    
    #import "NotificationService.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 {
        self.contentHandler = contentHandler;
        self.bestAttemptContent = [request.content mutableCopy];
        
        // Modify the notification content here...
        self.bestAttemptContent.title = [NSString stringWithFormat:@"%@ [modified]", self.bestAttemptContent.title];
        
        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);
    }
    
    @end
    

    到这一步推送扩展基本已经可以了。然后先运行一下扩展,然后再选择运行一下项目,这时你就可以尽情的测试了,将App 杀死,推送测试,So easy 是吗?

    这只是简单使用,还可以做其他的一些操作,- (void)didReceiveNotificationRequest:(UNNotificationRequest *)request withContentHandler:(void (^)(UNNotificationContent * _Nonnull))contentHandler { 这个方法就是用来处理通知内容重写的方法,附件什么的,可以尽情的为所欲为!

    原先直接从APNs推送到用户手机的消息,中间添加了ServiceExtension处理的一个步骤(当然你也可以不处理),通过这个ServiceExtension,我们可以把即将给用户展示的通知内容,做各种自定义的处理,最终,给用户呈现一个更为丰富的通知,当然,如果网络不好,或者附件过大,可能在给定的时间内,我们没能将通知重新编辑,那么,则会显示发过来的原版通知内容。

    这是我当时简单的处理:

    /** 修改收到的推送消息 */
    - (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:@"%@ [这是标题]", self.bestAttemptContent.title];
        
        NSString *voiceContent = [NSString stringWithFormat:@"%@",[self.bestAttemptContent.userInfo objectForKey:@"voice"]];
        NSString *volume = [NSString stringWithFormat:@"%@",[self.bestAttemptContent.userInfo objectForKey:@"volume"]];
        NSString *speed = [NSString stringWithFormat:@"%@",[self.bestAttemptContent.userInfo objectForKey:@"speed"]];
    
        // 原生的语音播放
        //忽略静音按钮
        AVAudioSession *session =[AVAudioSession sharedInstance];
        [session setCategory:AVAudioSessionCategoryPlayback error:nil];
        // 系统播放
        self.aVSpeechSynthesizer = [[AVSpeechSynthesizer alloc] init];
        //需要转换的文本
        AVSpeechUtterance * aVSpeechUtterance = [[AVSpeechUtterance alloc] initWithString:voiceContent];
        //设置语速快慢
        //    aVSpeechUtterance.rate = AVSpeechUtteranceMinimumSpeechRate;
        float speedd = [speed floatValue]/100;
        aVSpeechUtterance.rate = speedd;
        //设置语言类别(不能识别,返回nil)
        AVSpeechSynthesisVoice *voiceType = [AVSpeechSynthesisVoice voiceWithLanguage:@"zh-CN"];
        aVSpeechUtterance.voice = voiceType;
        //设置音量大小
        aVSpeechUtterance.volume = [volume floatValue]/100.f;
        //语音合成器会生成音频
        [self.aVSpeechSynthesizer speakUtterance:aVSpeechUtterance];
    
         self.contentHandler(self.bestAttemptContent);
    }
    
    • 注意点:
    1. 推送内容,在aps里一定要有"mutable -content"这个字段,和 alert 字段,其它可以添加自定义的字段。
      推送内容大概如下:
    {
        "aps": {
            "alert": "",
            "badge": 1,
            "sound": "default",
            "mutable-content": "1",
            "url": "http://"
            "volume":50,
            "speed":50
        }
    }
    
    1. 如果调试这个通知扩展类,无论怎么打断点都毫无反应?
      期初我以为这个通知扩展类是不能调试的,后来才在网上发现说要选择正确的 Target 才可以的,不过目前没有试,遇到可以测试一下。
    image.png

    另外附上极光相关的文档:

    极光Notification Service Extension 相关接口

    相关文章

      网友评论

          本文标题:ios - App 杀死或退到后台语音播报收到的推送内容&Not

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