Skip to content

Commit 6dc7378

Browse files
author
jiankui.shao
committed
fix push connection error
1 parent e8c51b7 commit 6dc7378

File tree

8 files changed

+363
-291
lines changed

8 files changed

+363
-291
lines changed

SmartPush.xcodeproj/project.pbxproj

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
B89258D91E63C831005E9F27 /* Sec.m in Sources */ = {isa = PBXBuildFile; fileRef = B89258D81E63C831005E9F27 /* Sec.m */; };
2121
B8F2D8FF1E5D1D320029D63C /* SecManager.m in Sources */ = {isa = PBXBuildFile; fileRef = B8F2D8FE1E5D1D320029D63C /* SecManager.m */; };
2222
B8F2D9021E5D2B600029D63C /* DragPopUpButton.m in Sources */ = {isa = PBXBuildFile; fileRef = B8F2D9011E5D2B600029D63C /* DragPopUpButton.m */; };
23+
F5C8364126D77CA70006B5BC /* NetworkManager.m in Sources */ = {isa = PBXBuildFile; fileRef = F5C8363F26D77CA70006B5BC /* NetworkManager.m */; };
2324
/* End PBXBuildFile section */
2425

2526
/* Begin PBXFileReference section */
@@ -46,6 +47,8 @@
4647
B8F2D8FE1E5D1D320029D63C /* SecManager.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = SecManager.m; sourceTree = "<group>"; };
4748
B8F2D9001E5D2B600029D63C /* DragPopUpButton.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = DragPopUpButton.h; sourceTree = "<group>"; };
4849
B8F2D9011E5D2B600029D63C /* DragPopUpButton.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = DragPopUpButton.m; sourceTree = "<group>"; };
50+
F5C8363F26D77CA70006B5BC /* NetworkManager.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; name = NetworkManager.m; path = ../../../../Downloads/NetworkManager.m; sourceTree = "<group>"; };
51+
F5C8364026D77CA70006B5BC /* NetworkManager.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = NetworkManager.h; path = ../../../../Downloads/NetworkManager.h; sourceTree = "<group>"; };
4952
/* End PBXFileReference section */
5053

5154
/* Begin PBXFrameworksBuildPhase section */
@@ -149,6 +152,8 @@
149152
B8F2D8FC1E5D1CF90029D63C /* Manager */ = {
150153
isa = PBXGroup;
151154
children = (
155+
F5C8364026D77CA70006B5BC /* NetworkManager.h */,
156+
F5C8363F26D77CA70006B5BC /* NetworkManager.m */,
152157
B8F2D8FD1E5D1D320029D63C /* SecManager.h */,
153158
B8F2D8FE1E5D1D320029D63C /* SecManager.m */,
154159
);
@@ -194,6 +199,7 @@
194199
developmentRegion = English;
195200
hasScannedForEncodings = 0;
196201
knownRegions = (
202+
English,
197203
en,
198204
Base,
199205
);
@@ -232,6 +238,7 @@
232238
A276B6D01AB51A6F009EF649 /* main.m in Sources */,
233239
A276B6F41AB51B01009EF649 /* TextFieldDrag.m in Sources */,
234240
B8F2D8FF1E5D1D320029D63C /* SecManager.m in Sources */,
241+
F5C8364126D77CA70006B5BC /* NetworkManager.m in Sources */,
235242
A276B6CE1AB51A6F009EF649 /* AppDelegate.m in Sources */,
236243
);
237244
runOnlyForDeploymentPostprocessing = 0;
@@ -334,6 +341,7 @@
334341
INFOPLIST_FILE = SmartPush/Info.plist;
335342
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/../Frameworks";
336343
MACOSX_DEPLOYMENT_TARGET = 10.10;
344+
PRODUCT_BUNDLE_IDENTIFIER = org.skyfox.SmartPush2;
337345
PRODUCT_NAME = "$(TARGET_NAME)";
338346
};
339347
name = Debug;
@@ -346,6 +354,7 @@
346354
INFOPLIST_FILE = SmartPush/Info.plist;
347355
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/../Frameworks";
348356
MACOSX_DEPLOYMENT_TARGET = 10.10;
357+
PRODUCT_BUNDLE_IDENTIFIER = org.skyfox.SmartPush2;
349358
PRODUCT_NAME = "$(TARGET_NAME)";
350359
};
351360
name = Release;

SmartPush/Base.lproj/Main.storyboard

Lines changed: 115 additions & 45 deletions
Large diffs are not rendered by default.

SmartPush/Info.plist

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,21 +2,14 @@
22
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
33
<plist version="1.0">
44
<dict>
5-
<key>NSAppTransportSecurity</key>
6-
<dict>
7-
<key>NSAllowsArbitraryLoads</key>
8-
<true/>
9-
</dict>
10-
<key>LSApplicationCategoryType</key>
11-
<string></string>
125
<key>CFBundleDevelopmentRegion</key>
136
<string>en</string>
147
<key>CFBundleExecutable</key>
158
<string>$(EXECUTABLE_NAME)</string>
169
<key>CFBundleIconFile</key>
1710
<string></string>
1811
<key>CFBundleIdentifier</key>
19-
<string>org.skyfox.$(PRODUCT_NAME:rfc1034identifier)</string>
12+
<string>$(PRODUCT_BUNDLE_IDENTIFIER)</string>
2013
<key>CFBundleInfoDictionaryVersion</key>
2114
<string>6.0</string>
2215
<key>CFBundleName</key>
@@ -29,8 +22,15 @@
2922
<string>????</string>
3023
<key>CFBundleVersion</key>
3124
<string>1</string>
25+
<key>LSApplicationCategoryType</key>
26+
<string></string>
3227
<key>LSMinimumSystemVersion</key>
3328
<string>$(MACOSX_DEPLOYMENT_TARGET)</string>
29+
<key>NSAppTransportSecurity</key>
30+
<dict>
31+
<key>NSAllowsArbitraryLoads</key>
32+
<true/>
33+
</dict>
3434
<key>NSHumanReadableCopyright</key>
3535
<string>Copyright © 2015年 www.skyfox.org. All rights reserved.</string>
3636
<key>NSMainStoryboardFile</key>

SmartPush/Manager/NetworkManager.h

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
//
2+
// NetworkManager.h
3+
// SmartPush
4+
//
5+
// Created by shao on 2021/8/24.
6+
// Copyright © 2021 www.skyfox.org. All rights reserved.
7+
//
8+
9+
//#import <Foundation/Foundation.h>
10+
#import <Cocoa/Cocoa.h>
11+
12+
13+
NS_ASSUME_NONNULL_BEGIN
14+
typedef void(^NetworkManagerSuccessBlock)(id responeObject);
15+
typedef void(^NetworkManagerFailBlock)(NSError *error);
16+
17+
@interface NetworkManager : NSObject<NSURLSessionDelegate>
18+
@property (nonatomic, strong, nullable) __attribute__((NSObject)) SecIdentityRef identity;
19+
@property (nonatomic, strong) NSURLSession *session;
20+
@property (nonatomic, copy) NetworkManagerSuccessBlock successBlock;
21+
@property (nonatomic, copy) NetworkManagerFailBlock failBlock;
22+
23+
+ (NetworkManager*)sharedManager;
24+
//https://developer.apple.com/documentation/usernotifications/setting_up_a_remote_notification_server/sending_notification_requests_to_apns?language=objc
25+
- (void)postWithPayload:(NSString *)payload
26+
toToken:(NSString *)token
27+
withTopic:(nullable NSString *)topic
28+
priority:(NSUInteger)priority
29+
collapseID:(NSString *)collapseID
30+
payloadType:(NSUInteger)payloadType
31+
inSandbox:(BOOL)sandbox
32+
exeSuccess:(void(^)(id responseObject))exeSuccess
33+
exeFailed:(void(^)(NSError *error))exeFailed;
34+
-(void)disconnect;
35+
@end
36+
37+
NS_ASSUME_NONNULL_END

SmartPush/Manager/NetworkManager.m

Lines changed: 128 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,128 @@
1+
//
2+
// NetworkManager.m
3+
// SmartPush
4+
//
5+
// Created by shao on 2021/8/24.
6+
// Copyright © 2021 www.skyfox.org. All rights reserved.
7+
//
8+
#define Push_Developer "api.sandbox.push.apple.com"
9+
#define Push_Production "api.push.apple.com"
10+
#import "NetworkManager.h"
11+
static dispatch_once_t _onceToken;
12+
static NetworkManager *_sharedManager = nil;
13+
14+
@implementation NetworkManager
15+
16+
+ (NetworkManager*)sharedManager{
17+
18+
dispatch_once(&_onceToken, ^{
19+
_sharedManager = [[self alloc] init];
20+
21+
});
22+
23+
return _sharedManager;
24+
}
25+
- (instancetype)init
26+
{
27+
self = [super init];
28+
if (self) {
29+
30+
}
31+
return self;
32+
}
33+
- (void)disconnect{
34+
35+
}
36+
#pragma mark - Public
37+
- (void)setIdentity:(SecIdentityRef)identity {
38+
39+
if (_identity != identity) {
40+
if (_identity != NULL) {
41+
CFRelease(_identity);
42+
}
43+
if (identity != NULL) {
44+
_identity = (SecIdentityRef)CFRetain(identity);
45+
46+
// Create a new session
47+
NSURLSessionConfiguration *conf = [NSURLSessionConfiguration defaultSessionConfiguration];
48+
self.session = [NSURLSession sessionWithConfiguration:conf
49+
delegate:self
50+
delegateQueue:[NSOperationQueue mainQueue]];
51+
52+
} else {
53+
_identity = NULL;
54+
}
55+
}
56+
}
57+
58+
- (void)postWithPayload:(NSString *)payload
59+
toToken:(NSString *)token
60+
withTopic:(nullable NSString *)topic
61+
priority:(NSUInteger)priority
62+
collapseID:(NSString *)collapseID
63+
payloadType:(NSUInteger)payloadType
64+
inSandbox:(BOOL)sandbox
65+
exeSuccess:(void(^)(id responseObject))exeSuccess
66+
exeFailed:(void(^)(NSError *error))exeFailed {
67+
68+
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"https://api%@.push.apple.com/3/device/%@", sandbox?@".sandbox":@"", token]]];
69+
request.HTTPMethod = @"POST";
70+
71+
request.HTTPBody = [payload dataUsingEncoding:NSUTF8StringEncoding];
72+
73+
if (topic) {
74+
[request addValue:topic forHTTPHeaderField:@"apns-topic"];
75+
}
76+
77+
if (collapseID.length > 0) {
78+
[request addValue:collapseID forHTTPHeaderField:@"apns-collapse-id"];
79+
}
80+
81+
[request addValue:[NSString stringWithFormat:@"%lu", (unsigned long)priority] forHTTPHeaderField:@"apns-priority"];
82+
83+
[request addValue:@"0" forHTTPHeaderField:@"apns-push-type"];
84+
85+
NSURLSessionDataTask *task = [self.session dataTaskWithRequest:request completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
86+
NSHTTPURLResponse *r = (NSHTTPURLResponse *)response;
87+
88+
if (r == nil && error) {
89+
if (self.failBlock) {
90+
self.failBlock(error);
91+
}
92+
return;
93+
}
94+
95+
if (r.statusCode != 200 && data) {
96+
NSError *error;
97+
NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:data options:0 error:&error];
98+
99+
if (error) {return;}
100+
101+
NSString *reason = dict[@"reason"];
102+
103+
// Not implemented?
104+
// NSString *ID = r.allHeaderFields[@"apns-id"];
105+
if (self.successBlock) {
106+
self.successBlock(dict);
107+
}
108+
109+
}
110+
}];
111+
[task resume];
112+
}
113+
114+
#pragma mark - NSURLSessionDelegate
115+
116+
- (void)URLSession:(NSURLSession *)session task:(nonnull NSURLSessionTask *)task didReceiveChallenge:(nonnull NSURLAuthenticationChallenge *)challenge completionHandler:(nonnull void (^)(NSURLSessionAuthChallengeDisposition, NSURLCredential * _Nullable))completionHandler {
117+
SecCertificateRef certificate;
118+
119+
SecIdentityCopyCertificate(self.identity, &certificate);
120+
121+
NSURLCredential *cred = [[NSURLCredential alloc] initWithIdentity:self.identity
122+
certificates:@[(__bridge_transfer id)certificate]
123+
persistence:NSURLCredentialPersistenceForSession];
124+
125+
completionHandler(NSURLSessionAuthChallengeUseCredential, cred);
126+
}
127+
128+
@end

SmartPush/Model/Sec.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,5 +15,6 @@
1515

1616
@property (nonatomic,strong) NSDate *date;
1717
@property (nonatomic,copy) NSString *expire;
18+
@property (nonatomic,assign) BOOL fromFile;
1819

1920
@end

SmartPush/PushViewController.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,10 +37,14 @@
3737
@property (weak) IBOutlet NSPopUpButton *payLoadPopUpButton;
3838
@property (weak) IBOutlet NSTextField *tokenTextField;
3939
@property (weak) IBOutlet DragPopUpButton *cerPopUpButton;
40+
@property (weak) IBOutlet NSSegmentedControl *prioritySegmentedControl;
41+
@property (weak) IBOutlet NSPopUpButton *payloadTypeButton;
4042

4143
- (IBAction)connect:(id)sender;
4244
- (IBAction)push:(id)sender;
4345
- (IBAction)modeSwitch:(id)sender;
4446
- (IBAction)payLoadButtonTouched:(id)sender;
47+
- (IBAction)playLoadTypeTouched:(id)sender;
48+
- (IBAction)prioritySwitch:(id)sender;
4549

4650
@end

0 commit comments

Comments
 (0)