用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,方便测试。