【iOS】实现类似支付宝收钱语音播报功能

需求

1、实现类似支付宝收钱时语音播报

实现思路

1、集成极光推送
2、使用tts将金额播报出来(iOS10至iOS12)
3、收到推送后,处理金额,奖金额分割转换成一个个音频文件
4、将金额以本地推送形式,自定义语音播放出来

实现步骤

1、项目配置

在这里插入图片描述
在这里插入图片描述

在这里插入图片描述

2、集成极光
  • Cocoapods集成极光
    pod 'JCore'
    pod 'JPush'
  • 在AppDelegate里面配置
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    
    // 初始化极光
    JPUSHRegisterEntity * entity = [[JPUSHRegisterEntity alloc] init];
    if (@available(iOS 12.0, *)) {
        entity.types = JPAuthorizationOptionAlert|JPAuthorizationOptionBadge|JPAuthorizationOptionSound|JPAuthorizationOptionProvidesAppNotificationSettings;
    } else {
        // Fallback on earlier versions
        entity.types = JPAuthorizationOptionAlert|JPAuthorizationOptionBadge|JPAuthorizationOptionSound;
    }
    [JPUSHService registerForRemoteNotificationConfig:entity delegate:self];
    
    
    // Required
    // init Push
    // notice: 2.1.5 版本的 SDK 新增的注册方法,改成可上报 IDFA,如果没有使用 IDFA 直接传 nil
    [JPUSHService setupWithOption:launchOptions appKey:@"16185342a0cf0e7ada842c78"
                          channel:@"0"
                 apsForProduction:NO
            advertisingIdentifier:nil];
    
    return YES;
}
  • JPUSHRegisterDelegate处理
#pragma mark - JPUSHRegisterDelegate

- (void)jpushNotificationAuthorization:(JPAuthorizationStatus)status withInfo:(NSDictionary *)info {
    
}

- (void)jpushNotificationCenter:(UNUserNotificationCenter *)center didReceiveNotificationResponse:(UNNotificationResponse *)response withCompletionHandler:(void (^)(void))completionHandler {
    
    NSLog(@"%@", response.notification);
    //完成回调
    completionHandler();
}

- (void)jpushNotificationCenter:(UNUserNotificationCenter *)center willPresentNotification:(UNNotification *)notification withCompletionHandler:(void (^)(NSInteger))completionHandler {
    
    
    NSLog(@"%@", notification);

}

- (void)application:(UIApplication *)application
didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken {
  /// 注册 DeviceToken
  [JPUSHService registerDeviceToken:deviceToken];
}

- (void)jpushNotificationCenter:(UNUserNotificationCenter *)center openSettingsForNotification:(UNNotification *)notification{
    
}
3、NotificationService
  • 导入音频文件(不支持网络文件)
    在这里插入图片描述
  • NotificationService.m
- (void)didReceiveNotificationRequest:(UNNotificationRequest *)request withContentHandler:(void (^)(UNNotificationContent * _Nonnull))contentHandler {
    self.contentHandler = contentHandler;
    self.bestAttemptContent = [request.content mutableCopy];
    
    
    NSLog(@"接到通知 NotificationService");
    
    NSDictionary *info = self.bestAttemptContent.userInfo;
    NSLog(@"info ==> %@", info);
    
    //step1: 推送json解析,获取推送金额
    NSMutableDictionary *dict = [self.bestAttemptContent.userInfo mutableCopy] ;
    BOOL playaudio =  [[dict objectForKey:@"amount"] boolValue] ;
    if(playaudio) {
        
        //step2:先处理金额,得到语音文件的数组,并播放语音(本地推送 -音频)
        NSString *amount = [dict objectForKey:@"amount"] ;//10000
        NSArray *musicArr = [[LCAudioPlayManager sharedInstance] getMusicArrayWithNum:amount];
        __weak __typeof(self) weakSelf = self;
        [[LCAudioPlayManager sharedInstance] pushLocalNotificationToApp:0 withArray:musicArr completed:^{
            // 播放完成后,通知系统
            weakSelf.contentHandler(weakSelf.bestAttemptContent);
        }];
        
    } else {
        //系统通知
        self.contentHandler(self.bestAttemptContent);
    }
}
4、项目运行

在这里插入图片描述
在这里插入图片描述
demo地址:https://download.csdn.net/download/tianzhilan0/15045548
demo地址:https://github.com/tianzhilan0/ZFBShouQian.git

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值