美文网首页
iOS 推送(杀死进程,获取推送消息并播放语音)

iOS 推送(杀死进程,获取推送消息并播放语音)

作者: Joymerry | 来源:发表于2020-09-02 23:35 被阅读0次

    该功能为推送高级功能,主要讲解app杀死进程或者后台情况下收到推送后,播放语音。实际该需求呢,就是类似于支付宝在没有运行的情况下收到推送消息,然后播报收钱的功能,接下来给大家讲解一下具体配置以及代码实现。

    一、首先需要大家了解下iOS 10 UNNotificationServiceExtension 这个类。

    1.选择创建“NotificationServiceExtension”

    第一步,先选择新建 NEW——>Target


    创建Target.png

    第二步,选择UNNotificationServiceExtension扩展类,不是左边的,他们的用途不一样,UNNotificationContentExtensior主要用户自定义推送UI


    UNNotificationServiceExtension.jpg
    第三步,扩展类的名字可以随便起,根据自己的爱好
    VocalPush

    创建完成之后,项目中会多一个VocalPush目录,且Scheme中也同样增加VocalPush


    截屏2020-09-02 20.44.13.png

    二、配置扩展

    1.添加功能支持


    image.png

    2.info.plist中:添加“App Transport Security Settings”字典 ,字典中添加“Allow Arbitrary Loads”设置为YES


    image.png
    3.把扩展里的Deployment Target 改成 10.0之后版本,因为10.0之后才支持推送扩展
    6601599058908_.pic.jpg

    4.基本完成配置工作,要想体验效果,先运行一下扩展,然后再选择运行一下项目。


    6611599059106_.pic.jpg
    5.后端发推送消息的时候,改方式一定要注意,在aps里一定要有"mutable -content"这个字段,值为1。不设置mutable -content = 1,不能实现该方式推送处理。
    {
      "aps" : {
        "content-available" : 1,
        "alert" : {
          "title" : "通知",
          "body" : "XTH到账45元"
        },
        "badge" : 0,
        "sound" : "default"
      }
    }
    

    三、代码实现处理

    1.NotificationService.m文件内的代码,就是iOS10的推送扩展。

    实际使用的是设置self.bestAttemptContent.sound的方式,而现阶段苹果也只能通过设置该属性去实现播放语音的功能。在我的项目中用的是提前录制好的语音文件,而支付宝,是使用文字转语音文件的方式,之后再设置该属性去实现的语音播放,你也可以通过网络下载语音文件之后播放的方式去实现同样的效果。
    网上有很多比较老的文章,笔者在研究此功能的时候也走了不少弯路,比如在扩展类NotificationService中,实现系统语音播报MediaPlayer,AVFoundation去语音转文字播放,或者直接播放语音文件都是不能实现该功能的,但是iOS10.0是可以实现的,根据大量阅读苹果开发文档,苹果起初设计该扩展类的目的是优化推送样式,不是为了语音处理,但是发现大部分开发者通过这种方式处理语音播放问题,所以后来把所有播放语音类的代码,让其在UNNotificationServiceExtension扩展类中的代码都是无效的。但是self.bestAttemptContent仍包含的有sound属性,所以后来大家都通过处理过后生成的语音文件,赋值给self.bestAttemptContent.sound去实现播放语音的效果。

    #import "NotificationService.h"
    @interface NotificationService ()<AVAudioPlayerDelegate,AVSpeechSynthesizerDelegate>
    
    @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];
    
    #pragma mark -
    //    // 重写一些东西
    //     self.bestAttemptContent.title = @"我是标题";
    //     self.bestAttemptContent.subtitle = @"我是子标题";
    //     self.bestAttemptContent.body = @"来自徐不同";
    
        NSDictionary *userInfoDic =  self.bestAttemptContent.userInfo;
        NSLog(@"后台推送消息内容:%@",userInfoDic);
        // 安全预警提醒类型
        if ([userInfoDic[@"pushType"] isEqualToString:@"SAFETY_WARNING"]) {
            // 12.1系统版本之前 可以是用AVAudioSession
            if ([[[UIDevice currentDevice] systemVersion]floatValue] < 12.1) {
                // AVAudioSession是一个单例类
                AVAudioSession *session = [AVAudioSession sharedInstance];
                // AVAudioSessionCategorySoloAmbient是系统默认的category
                [session setCategory:AVAudioSessionCategorySoloAmbient error:nil];
                // 激活AVAudioSession
                [session setActive:YES error:nil];
    
                // 文字转语音播放
                [self playVoiceWithContent:userInfoDic[@"content"]];
            } else {
                if ([userInfoDic[@"sinoiovNoticeType"] isEqualToString:@"SPEEDING"]) {
                    // SPEEDING 超速
                    self.bestAttemptContent.sound = [UNNotificationSound soundNamed:@"overSpeed.m4a"];
                    NSLog(@"超速");
                }else if ([userInfoDic[@"sinoiovNoticeType"] isEqualToString:@"FATIGUE"]) {
                    // FATIGUE 疲劳
                    self.bestAttemptContent.sound = [UNNotificationSound soundNamed:@"fatigueDriving.m4a"];
                    NSLog(@"疲劳");
                }else if ([userInfoDic[@"sinoiovNoticeType"] isEqualToString:@"VEHICLE_OFFLINE"]) {
                    // VEHICLE_OFFLINE 离线
                    self.bestAttemptContent.sound = [UNNotificationSound soundNamed:@"offLine.m4a"];
                    NSLog(@"离线");
                }else if ([userInfoDic[@"sinoiovNoticeType"] isEqualToString:@"PARKING_VIOLATION"]) {
                    // PARKING_VIOLATION 高速公路违规停车
                    self.bestAttemptContent.sound = [UNNotificationSound soundNamed:@"illegalParking.m4a"];
                    NSLog(@"高速公路违规停车");
                }else if ([userInfoDic[@"sinoiovNoticeType"] isEqualToString:@"LOW_SPEED"]) {
                    // LOW_SPEED 高速公路低速行驶
                    self.bestAttemptContent.sound = [UNNotificationSound soundNamed:@"lowSpeed.m4a"];
                    NSLog(@"高速公路低速行驶");
                }
                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);
    }
    @end
    

    需要注意的是:
    1.以上扩展方法中是不能打断点的,打断点会导致崩溃且找不到原因,需要打印数据使用NSLog方法,打断点会崩溃的问题不是个例。
    2.该种方式的推送语音消息只支持6s时间,因为系统的推送弹框的显示时间为6s,超过6s的语音会停止播放,请注意语音时长,因为时间原因,本人没有做深入研究是否可以延长推送弹框的方法。
    以上问题有哪位大神知道原因或者有解决办法,可以联系告知笔者。

    2.以上代码只是实现了语音播报的功能,实际上苹果支持,自定义推送弹框样式,需要使用UNNotificationContentExtensior去实现。但是系统也自带的有默认的图片样式以及后台下载音乐文件,视频文件点击播放的功能。

    相关文章

      网友评论

          本文标题:iOS 推送(杀死进程,获取推送消息并播放语音)

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