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

来源:互联网 发布:增删算法统宗拍卖 编辑:程序博客网 时间:2024/05/16 01:17
用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 = [LHSessionManagermanager];
    base64Encoded = [
NSStringstringWithFormat:@"Basic %@", base64Encoded];
   
    lhmanager.
requestSerializer= [AFJSONRequestSerializerserializer];//申明请求的数据是json类型
   //返回参数无需设置,AFN默认就是json
//    lhmanager.responseSerializer = [AFJSONResponseSerializer serializer];//申明返回的结果是json类型
    [lhmanager.requestSerializersetValue:@"application/json"forHTTPHeaderField:@"Content-Type"];
   
   
//获取用户token
    [lhmanager.
requestSerializersetValue:@"/v3/push HTTP/1.1"forHTTPHeaderField:@"POST"];
    [lhmanager.
requestSerializersetValue:base64EncodedforHTTPHeaderField:@"Authorization"];
   
   
   
NSMutableDictionary *androidMsg = [[NSMutableDictionaryalloc]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 = [[NSMutableDictionaryalloc]init];
    [iosMsg
setValue:@"IOS端请注意,推送来了!"                   forKey:@"alert"];
    [iosMsg
setValue:@"default"                       forKey:@"sound"];
    [iosMsg
setValue:@"+1"                             forKey:@"badge"];
    [iosMsg
setValue:@{@"newsid":@"321"}             forKey:@"extras"];
   
   
NSMutableDictionary *notificationParams = [[NSMutableDictionaryalloc]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 = [[NSMutableDictionaryalloc]init];
    [optionsParams
setValue:@"60"                     forKey:@"time_to_live"];
    [optionsParams
setValue:@"false"                   forKey:@"apns_production"];
   
   
NSDictionary *dic = [NSDictionarydictionaryWithObjectsAndKeys:
                        
@[@"android",@"ios"],@"platform",
                        
@"all",@"audience",
                         notificationParams,
@"notification",
                        
//                         messageParams, @"message",
                        
//                         smsMsgParams, @"sms_message",
                         optionsParams,
@"options",
                        
nil];
   
    [lhmanager
POST:@"https://api.jpush.cn/v3/push"parameters:dicprogress:^(NSProgress*_Nonnull uploadProgress) {
       
    }
success:^(NSURLSessionDataTask*_Nonnull task, id _NullableresponseObject) {
      
NSLog(@"推送成功\n\nresponseObject = %@", responseObject);

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


写了个Demo,方便测试。



源码下载地址:http://download.csdn.net/detail/luohancc/9568727

1 0
原创粉丝点击