iOSiOS开发锦集iOS开发攻城狮的集散地

iOS- 实现APP前台、后台、甚至杀死进程下收到通知后进行语音

2018-07-27  本文已影响1021人  Guomingjian

前言 :公司项目-鲜特汇收银台APP,需要实现客户扫二维码付款后,后台推送通知,APP客户端收到通知之后语音播放:“鲜特汇到账xxx元”的功能。

PS:本项目使用的是极光推送。详细讲解过程:

1、极光SDK集成

极光推送SDK下载 ,下载好iOS版SDK直接将Lib文件夹拖入工程中,添加依赖库:libresolv.tbd。有疑问查看 官方集成文档

9D00027F-0454-432B-BACD-FBBA25C481D9.png

AppDelegate.m
1.1、配置 SDK

//
//  AppDelegate.m
//  JPushTest
//
//  Created by 郭明健 on 2018/7/26.
//  Copyright © 2018年 GuoMingJian. All rights reserved.
//

#import "AppDelegate.h"
#import "JPUSHService.h"
#import <UserNotifications/UserNotifications.h>

//#define appkey @"xxxxx"

@interface AppDelegate ()<JPUSHRegisterDelegate, UNUserNotificationCenterDelegate>

@end

@implementation AppDelegate

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {

    //配置极光推送,appkey定义为宏,填自己极光应用对应的appkey
    [JPUSHService setupWithOption:launchOptions
                           appKey:appkey
                          channel:nil
                 apsForProduction:NO
            advertisingIdentifier:nil];
    [JPUSHService setLogOFF];

    //注册APNs
    JPUSHRegisterEntity * entity = [[JPUSHRegisterEntity alloc] init];
    entity.types = JPAuthorizationOptionAlert|JPAuthorizationOptionBadge|JPAuthorizationOptionSound;
    [JPUSHService registerForRemoteNotificationConfig:entity delegate:self];

    return YES;
}

- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken
{
    NSString *deviceTokenStr = [self getDeviceToken:deviceToken];
    NSLog(@"deviceToken:%@", deviceTokenStr);
    [JPUSHService registerDeviceToken:deviceToken];
}

#pragma mark - private
- (NSString *)getDeviceToken:(NSData *)deviceToken
{
    NSMutableString *deviceTokenStr = [NSMutableString string];
    const char *bytes = deviceToken.bytes;
    int iCount = (int)deviceToken.length;
    for (int i = 0; i < iCount; i++)
    {
        [deviceTokenStr appendFormat:@"%02x", bytes[i]&0x000000FF];
    }
    return deviceTokenStr;
}

1.2、推送通知回调方法

#pragma mark - 远程通知
/**
 APNs 新增 "content-available":1, (静默推送)
 */
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler
{
    [JPUSHService handleRemoteNotification:userInfo];
    completionHandler(UIBackgroundFetchResultNewData);
}

#pragma mark - JPUSHRegisterDelegate
- (void)jpushNotificationCenter:(UNUserNotificationCenter *)center willPresentNotification:(UNNotification *)notification withCompletionHandler:(void (^)(NSInteger options))completionHandler
{
    //本地通知
    completionHandler(UNNotificationPresentationOptionBadge|UNNotificationPresentationOptionSound|UNNotificationPresentationOptionAlert);
}

- (void)jpushNotificationCenter:(UNUserNotificationCenter *)center didReceiveNotificationResponse:(UNNotificationResponse *)response withCompletionHandler:(void(^)())completionHandler
{
    //点击通知回调
    completionHandler();
}

#pragma mark -
- (void)applicationDidBecomeActive:(UIApplication *)application
{
    [[UIApplication sharedApplication] setApplicationIconBadgeNumber:0];
}

- (void)applicationDidEnterBackground:(UIApplication *)application {
    NSLog(@"App进入后台");
}

@end

2、到这里极光推送已经集成好了,可以通过极光控制台模拟推送通知了,当然你也可以使用 Easy APNs Provider工具来发送通知。

E315C9EC-F239-491C-89C1-A9C94283917F.png 6DD4BD06-9DDD-4D90-892F-BA3482CB5E92.png

3、入坑历程:

1、APP处于前台状态,推送什么的都没问题。一旦,APP退到后台,这时候推送通知过来,根本没有方法监听得到通知来了这个动作,就拿不到推送得内容。
为了解决这个问题,我开始想到的是静默推送。
静默推送设置:

5E0FC4ED-7BAF-43C8-A474-6CE6F18A3B67.png

APNs添加key-value -> "content-available":1,
例子:

{
  "aps" : {
    "content-available" : 1,
    "alert" : {
      "title" : "通知",
      "body" : "鲜特汇到账45元"
    },
    "badge" : 0,
    "sound" : "default"
  }
}

设置好之后,运行APP,当APP退到后台,此时推送通知过来会在已下方法监听到:

- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler
{
}

当APP退到后台,推送一条通知,在上面代理方法打个断点,处理好通知内容,然后执行语音播报代码:

//文字--语音播报
- (void)playMsg:(NSString *)msg
{
    //NSLog(@"语音播报:%@", msg);
    dispatch_async(dispatch_get_main_queue(), ^{
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.6 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            //后台播报
            [[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback error:nil];
            [[AVAudioSession sharedInstance] setActive:YES error:nil];
            //
            AVSpeechSynthesizer *avSpeech = [[AVSpeechSynthesizer alloc] init];
            AVSpeechUtterance *avSpeechterance = [AVSpeechUtterance speechUtteranceWithString:msg];
            AVSpeechSynthesisVoice *voiceType = [AVSpeechSynthesisVoice voiceWithLanguage:@"zh-CN"];
            avSpeechterance.voice = voiceType;
            avSpeechterance.volume = 1;
            avSpeechterance.rate = 0.5;
            avSpeechterance.pitchMultiplier = 1.1;
            [avSpeech speakUtterance:avSpeechterance];
        });
    });
}

此时,APP退到后台也能语音播报,如果你以为这样大功告成那你就错了,这样非常容易被系统中断,导致语音根本没声音。报以下错:

[TTS] Failure starting audio queue alp!
[TTS] _BeginSpeaking: couldn't begin playback

围绕这个报错我可算是尝尽办法,什么后台任务,runloop常驻线程(怀疑APP挂起导致),开后台语音等等。然而并没有什么卵用!!!

最终是通过Notification Service解决的,大家了解一下:

Editor->Add Target

F19F088D-3D2E-4BE2-95E5-FD910BA22486.png

给通知扩展取个名字(随你喜好取),bundle id就是你项目id.扩展名就可以了。扩展的版本兼容到10以上,毕竟通知扩展是iOS10推出的。

551B75A4-C26D-4893-9650-904D02E0FA4F.png 3C41C397-8D9B-4FC8-95F5-333A935A83B0.png

注意APNs必须添加key-value -> "mutable-content":1, 才能监听到通知。完成到这里基本就弄好了。语音播报用系统的感觉声音怪怪的,所有用了百度的语音合成SDK。

8917379F-C14A-40CC-91E0-8C95455BA829.png
//
//  NotificationService.m
//  PushExtend
//
//  Created by 郭明健 on 2018/7/26.
//  Copyright © 2018年 GuoMingJian. All rights reserved.
//

#import "NotificationService.h"
#import "CommonMethod.h"
#import <AVFoundation/AVFoundation.h>
#import "BDSSpeechSynthesizer.h"

//百度语音TTS:https://ai.baidu.com/docs#/TTS-iOS-SDK/4d8edeab
NSString* API_KEY = @"xxxxxxxx";
NSString* SECRET_KEY = @"xxxxxxxx";

#define kTime 0.6

@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 configureSDK];
    NSMutableDictionary *userInfo = [NSMutableDictionary dictionaryWithDictionary:request.content.userInfo];
    [self dealWithUserInfo:userInfo];
    //=========================//
    
    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);
}

#pragma mark - private

- (void)dealWithUserInfo:(NSDictionary *)userInfo
{
    /*
     根据后台 API 保存语音播报设置,
获取 Userdefault 决定是播报文字还是音频文件。
APP 跟扩展 Service 想公用 UserDefault必须用到 APP Group ID
     */
    BOOL isPlayAudio = [CommonMethod getIsPlayAudio];
    if (isPlayAudio)
    {
        dispatch_async(dispatch_get_main_queue(), ^{
            dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(kTime * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                //播放音频文件
                NSURL *mediaURL = [[NSBundle mainBundle] URLForResource:@"test" withExtension:@"caf"];
                //后台继续播放
                [[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback error:nil];
                [[AVAudioSession sharedInstance] setActive:YES error:nil];
                //播放
                AVAudioPlayer *audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:mediaURL error:nil];
                [audioPlayer play];
            });
        });
    }
    else
    {
        NSString *msg = @"";
        id content = userInfo[@"aps"][@"alert"];
        if ([content isKindOfClass:[NSDictionary class]])
        {
            msg = content[@"body"];
        }
        dispatch_async(dispatch_get_main_queue(), ^{
            dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(kTime * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                //百度语音TTS
                [[BDSSpeechSynthesizer sharedInstance] speakSentence:msg withError:nil];
            });
        });
    }
}

#pragma mark - 百度语音TTS

- (void)configureSDK
{
    [self configureOnlineTTS];
}

- (void)configureOnlineTTS
{
    [[BDSSpeechSynthesizer sharedInstance] setApiKey:API_KEY withSecretKey:SECRET_KEY];
    [[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback error:nil];
}

@end

写到这里基本OK了,剩下的就是调试,调试步骤:

1、run宿主APP
2、run通知扩展Target
3、APP退到后台
4、推送通知

希望能帮到大家,Q群:286380794,同时欢迎大家留言交流或者纠正我的错误-_-^^

上一篇下一篇

猜你喜欢

热点阅读