用OC写的极光推送服务端代码

用OC写的极光推送服务端代码                                                         

极光推送 - 服务端代码API:http://docs.jpush.io/server/rest_api_v3_push/#_5

OC这样写

- (
void )testPush {
   
    NSString *keys = @"0cd0edeb61e54d93459f6513:1c84a3fae6c9f8375c341998g" ;   //me
//    NSString *keys = @"bc0cca8452b6e2082c5e793c:9525b4edae9c87cd536e07ef"; //qk
   
NSData *nsdata = [keys dataUsingEncoding : NSUTF8StringEncoding ];
   
NSString *base64Encoded = [nsdata base64EncodedStringWithOptions : 0 ];
   
LHSessionManager *lhmanager = [ LHSessionManager manager ];
    base64Encoded = [
NSString stringWithFormat : @"Basic %@" , base64Encoded];
   
    lhmanager.
requestSerializer = [ AFJSONRequestSerializer serializer ]; // 申明请求的数据是 json 类型
    // 返回参数无需设置, AFN 默认就是 json
//    lhmanager.responseSerializer = [AFJSONResponseSerializer serializer];// 申明返回的结果是 json 类型
    [lhmanager. requestSerializer setValue : @"application/json" forHTTPHeaderField : @"Content-Type" ];
   
   
// 获取用户 token
    [lhmanager.
requestSerializer setValue : @"/v3/push HTTP/1.1" forHTTPHeaderField : @"POST" ];
    [lhmanager.
requestSerializer setValue :base64Encoded forHTTPHeaderField : @"Authorization" ];
   
   
   
NSMutableDictionary *androidMsg = [[ NSMutableDictionary alloc ] init ];
    [androidMsg
setValue : @"Android 端请注意,推送来了! "                 forKey : @"alert" ];
    [androidMsg
setValue : @"Send to Android"             forKey : @"title" ];
    [androidMsg
setValue : @"1"                           forKey : @"builder_id" ];
    [androidMsg
setValue : @{ @"newsid" : @"321" }           forKey : @"extras" ];
   
   
   
NSMutableDictionary *iosMsg = [[ NSMutableDictionary alloc ] init ];
    [iosMsg
setValue : @"IOS 端请注意,推送来了! "                     forKey : @"alert" ];
    [iosMsg
setValue : @"default"                         forKey : @"sound" ];
    [iosMsg
setValue : @"+1"                               forKey : @"badge" ];
    [iosMsg
setValue : @{ @"newsid" : @"321" }               forKey : @"extras" ];
   
   
NSMutableDictionary *notificationParams = [[ NSMutableDictionary alloc ] init ];
    [notificationParams
setValue :androidMsg             forKey : @"android" ];
    [notificationParams
setValue :iosMsg                 forKey : @"ios" ];
   
   
   
//    NSMutableDictionary *messageParams = [[NSMutableDictionary alloc] init];
   
//    [messageParams setValue:@" 注意,推送来了 "                 forKey:@"msg_content"];
   
//    [messageParams setValue:@"text"                     forKey:@"content_type"];
   
//    [messageParams setValue:@"msg"                      forKey:@"title"];
   
//    [messageParams setValue:@{@"key": @"value"}         forKey:@"extras"];
   
//
   
//    NSMutableDictionary *smsMsgParams = [[NSMutableDictionary alloc] init];
   
//    [smsMsgParams setValue:@"sms msg content"           forKey:@"content"];
   
//    [smsMsgParams setValue:@"3600"                      forKey:@"delay_time"];
   
   
NSMutableDictionary *optionsParams = [[ NSMutableDictionary alloc ] init ];
    [optionsParams
setValue : @"60"                       forKey : @"time_to_live" ];
    [optionsParams
setValue : @"false"                     forKey : @"apns_production" ];
   
   
NSDictionary *dic = [ NSDictionary dictionaryWithObjectsAndKeys :
                        
@[ @"android" , @"ios" ] , @"platform" ,
                        
@"all" , @"audience" ,
                         notificationParams,
@"notification" ,
                        
//                         messageParams, @"message",
                        
//                         smsMsgParams, @"sms_message",
                         optionsParams,
@"options" ,
                        
nil ];
   
    [lhmanager
POST : @"https://api.jpush.cn/v3/push" parameters :dic progress :^( NSProgress * _Nonnull uploadProgress) {
       
    }
success :^( NSURLSessionDataTask * _Nonnull task, id   _Nullable responseObject) {
      
NSLog ( @" 推送成功 \n\nresponseObject = %@" , responseObject);

    }
failure :^( NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
       
NSLog ( @" 失败 %@" , error);
    }];
   
}


写了个Demo,方便测试。




  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
当然可以,以下是 Objective-C 版本的代码实现: 1. 在 info.plist 文件中添加蓝牙权限: - NSBluetoothPeripheralUsageDescription:描述了应用程序需要使用蓝牙的原因。 - NSMicrophoneUsageDescription:描述了应用程序需要使用麦克风的原因。 2. 创建一个 AVAudioSession 实例并设置它的类别为 AVAudioSessionCategoryRecord: ``` AVAudioSession *audioSession = [AVAudioSession sharedInstance]; NSError *error; BOOL success = [audioSession setCategory:AVAudioSessionCategoryRecord error:&error]; if (!success) { NSLog(@"Failed to set audio session category: %@", error.localizedDescription); } ``` 3. 设置 AVAudioSession 实例的输入源和输出源: ``` NSArray<AVAudioSessionPortDescription *> *bluetoothInputs = [audioSession.availableInputs filteredArrayUsingPredicate:[NSPredicate predicateWithBlock:^BOOL(AVAudioSessionPortDescription * _Nullable input, NSDictionary<NSString *,id> * _Nullable bindings) { return input.portType == AVAudioSessionPortBluetoothHFP || input.portType == AVAudioSessionPortBluetoothA2DP; }]]; if (bluetoothInputs.count > 0) { NSError *error; success = [audioSession setPreferredInput:bluetoothInputs.firstObject error:&error]; if (!success) { NSLog(@"Failed to set preferred input: %@", error.localizedDescription); } } AVAudioSessionPortDescription *currentOutput = audioSession.currentRoute.outputs.firstObject; success = [audioSession setPreferredOutput:currentOutput error:&error]; if (!success) { NSLog(@"Failed to set preferred output: %@", error.localizedDescription); } ``` 4. 初始化 AVAudioRecorder 对象并开始录音: ``` NSURL *audioURL = // Specify the URL where you want to save the recorded audio. NSDictionary *audioSettings = @{AVFormatIDKey: @(kAudioFormatMPEG4AAC), AVSampleRateKey: @(44100), AVNumberOfChannelsKey: @(2)}; NSError *error; AVAudioRecorder *audioRecorder = [[AVAudioRecorder alloc] initWithURL:audioURL settings:audioSettings error:&error]; if (!audioRecorder) { NSLog(@"Failed to initialize audio recorder: %@", error.localizedDescription); } else { [audioRecorder prepareToRecord]; [audioRecorder record]; } ``` 这样就可以从蓝牙设备的麦克风录制音频了。希望对您有所帮助!

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值