Skip to content

Commit

Permalink
fix push connection error
Browse files Browse the repository at this point in the history
  • Loading branch information
jiankui.shao committed Aug 30, 2021
1 parent e8c51b7 commit 6dc7378
Show file tree
Hide file tree
Showing 8 changed files with 363 additions and 291 deletions.
9 changes: 9 additions & 0 deletions SmartPush.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
B89258D91E63C831005E9F27 /* Sec.m in Sources */ = {isa = PBXBuildFile; fileRef = B89258D81E63C831005E9F27 /* Sec.m */; };
B8F2D8FF1E5D1D320029D63C /* SecManager.m in Sources */ = {isa = PBXBuildFile; fileRef = B8F2D8FE1E5D1D320029D63C /* SecManager.m */; };
B8F2D9021E5D2B600029D63C /* DragPopUpButton.m in Sources */ = {isa = PBXBuildFile; fileRef = B8F2D9011E5D2B600029D63C /* DragPopUpButton.m */; };
F5C8364126D77CA70006B5BC /* NetworkManager.m in Sources */ = {isa = PBXBuildFile; fileRef = F5C8363F26D77CA70006B5BC /* NetworkManager.m */; };
/* End PBXBuildFile section */

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

/* Begin PBXFrameworksBuildPhase section */
Expand Down Expand Up @@ -149,6 +152,8 @@
B8F2D8FC1E5D1CF90029D63C /* Manager */ = {
isa = PBXGroup;
children = (
F5C8364026D77CA70006B5BC /* NetworkManager.h */,
F5C8363F26D77CA70006B5BC /* NetworkManager.m */,
B8F2D8FD1E5D1D320029D63C /* SecManager.h */,
B8F2D8FE1E5D1D320029D63C /* SecManager.m */,
);
Expand Down Expand Up @@ -194,6 +199,7 @@
developmentRegion = English;
hasScannedForEncodings = 0;
knownRegions = (
English,
en,
Base,
);
Expand Down Expand Up @@ -232,6 +238,7 @@
A276B6D01AB51A6F009EF649 /* main.m in Sources */,
A276B6F41AB51B01009EF649 /* TextFieldDrag.m in Sources */,
B8F2D8FF1E5D1D320029D63C /* SecManager.m in Sources */,
F5C8364126D77CA70006B5BC /* NetworkManager.m in Sources */,
A276B6CE1AB51A6F009EF649 /* AppDelegate.m in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
Expand Down Expand Up @@ -334,6 +341,7 @@
INFOPLIST_FILE = SmartPush/Info.plist;
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/../Frameworks";
MACOSX_DEPLOYMENT_TARGET = 10.10;
PRODUCT_BUNDLE_IDENTIFIER = org.skyfox.SmartPush2;
PRODUCT_NAME = "$(TARGET_NAME)";
};
name = Debug;
Expand All @@ -346,6 +354,7 @@
INFOPLIST_FILE = SmartPush/Info.plist;
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/../Frameworks";
MACOSX_DEPLOYMENT_TARGET = 10.10;
PRODUCT_BUNDLE_IDENTIFIER = org.skyfox.SmartPush2;
PRODUCT_NAME = "$(TARGET_NAME)";
};
name = Release;
Expand Down
160 changes: 115 additions & 45 deletions SmartPush/Base.lproj/Main.storyboard

Large diffs are not rendered by default.

16 changes: 8 additions & 8 deletions SmartPush/Info.plist
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,14 @@
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>NSAppTransportSecurity</key>
<dict>
<key>NSAllowsArbitraryLoads</key>
<true/>
</dict>
<key>LSApplicationCategoryType</key>
<string></string>
<key>CFBundleDevelopmentRegion</key>
<string>en</string>
<key>CFBundleExecutable</key>
<string>$(EXECUTABLE_NAME)</string>
<key>CFBundleIconFile</key>
<string></string>
<key>CFBundleIdentifier</key>
<string>org.skyfox.$(PRODUCT_NAME:rfc1034identifier)</string>
<string>$(PRODUCT_BUNDLE_IDENTIFIER)</string>
<key>CFBundleInfoDictionaryVersion</key>
<string>6.0</string>
<key>CFBundleName</key>
Expand All @@ -29,8 +22,15 @@
<string>????</string>
<key>CFBundleVersion</key>
<string>1</string>
<key>LSApplicationCategoryType</key>
<string></string>
<key>LSMinimumSystemVersion</key>
<string>$(MACOSX_DEPLOYMENT_TARGET)</string>
<key>NSAppTransportSecurity</key>
<dict>
<key>NSAllowsArbitraryLoads</key>
<true/>
</dict>
<key>NSHumanReadableCopyright</key>
<string>Copyright © 2015年 www.skyfox.org. All rights reserved.</string>
<key>NSMainStoryboardFile</key>
Expand Down
37 changes: 37 additions & 0 deletions SmartPush/Manager/NetworkManager.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
//
// NetworkManager.h
// SmartPush
//
// Created by shao on 2021/8/24.
// Copyright © 2021 www.skyfox.org. All rights reserved.
//

//#import <Foundation/Foundation.h>
#import <Cocoa/Cocoa.h>


NS_ASSUME_NONNULL_BEGIN
typedef void(^NetworkManagerSuccessBlock)(id responeObject);
typedef void(^NetworkManagerFailBlock)(NSError *error);

@interface NetworkManager : NSObject<NSURLSessionDelegate>
@property (nonatomic, strong, nullable) __attribute__((NSObject)) SecIdentityRef identity;
@property (nonatomic, strong) NSURLSession *session;
@property (nonatomic, copy) NetworkManagerSuccessBlock successBlock;
@property (nonatomic, copy) NetworkManagerFailBlock failBlock;

+ (NetworkManager*)sharedManager;
//https://developer.apple.com/documentation/usernotifications/setting_up_a_remote_notification_server/sending_notification_requests_to_apns?language=objc
- (void)postWithPayload:(NSString *)payload
toToken:(NSString *)token
withTopic:(nullable NSString *)topic
priority:(NSUInteger)priority
collapseID:(NSString *)collapseID
payloadType:(NSUInteger)payloadType
inSandbox:(BOOL)sandbox
exeSuccess:(void(^)(id responseObject))exeSuccess
exeFailed:(void(^)(NSError *error))exeFailed;
-(void)disconnect;
@end

NS_ASSUME_NONNULL_END
128 changes: 128 additions & 0 deletions SmartPush/Manager/NetworkManager.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
//
// NetworkManager.m
// SmartPush
//
// Created by shao on 2021/8/24.
// Copyright © 2021 www.skyfox.org. All rights reserved.
//
#define Push_Developer "api.sandbox.push.apple.com"
#define Push_Production "api.push.apple.com"
#import "NetworkManager.h"
static dispatch_once_t _onceToken;
static NetworkManager *_sharedManager = nil;

@implementation NetworkManager

+ (NetworkManager*)sharedManager{

dispatch_once(&_onceToken, ^{
_sharedManager = [[self alloc] init];

});

return _sharedManager;
}
- (instancetype)init
{
self = [super init];
if (self) {

}
return self;
}
- (void)disconnect{

}
#pragma mark - Public
- (void)setIdentity:(SecIdentityRef)identity {

if (_identity != identity) {
if (_identity != NULL) {
CFRelease(_identity);
}
if (identity != NULL) {
_identity = (SecIdentityRef)CFRetain(identity);

// Create a new session
NSURLSessionConfiguration *conf = [NSURLSessionConfiguration defaultSessionConfiguration];
self.session = [NSURLSession sessionWithConfiguration:conf
delegate:self
delegateQueue:[NSOperationQueue mainQueue]];

} else {
_identity = NULL;
}
}
}

- (void)postWithPayload:(NSString *)payload
toToken:(NSString *)token
withTopic:(nullable NSString *)topic
priority:(NSUInteger)priority
collapseID:(NSString *)collapseID
payloadType:(NSUInteger)payloadType
inSandbox:(BOOL)sandbox
exeSuccess:(void(^)(id responseObject))exeSuccess
exeFailed:(void(^)(NSError *error))exeFailed {

NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"https://api%@.push.apple.com/3/device/%@", sandbox?@".sandbox":@"", token]]];
request.HTTPMethod = @"POST";

request.HTTPBody = [payload dataUsingEncoding:NSUTF8StringEncoding];

if (topic) {
[request addValue:topic forHTTPHeaderField:@"apns-topic"];
}

if (collapseID.length > 0) {
[request addValue:collapseID forHTTPHeaderField:@"apns-collapse-id"];
}

[request addValue:[NSString stringWithFormat:@"%lu", (unsigned long)priority] forHTTPHeaderField:@"apns-priority"];

[request addValue:@"0" forHTTPHeaderField:@"apns-push-type"];

NSURLSessionDataTask *task = [self.session dataTaskWithRequest:request completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
NSHTTPURLResponse *r = (NSHTTPURLResponse *)response;

if (r == nil && error) {
if (self.failBlock) {
self.failBlock(error);
}
return;
}

if (r.statusCode != 200 && data) {
NSError *error;
NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:data options:0 error:&error];

if (error) {return;}

NSString *reason = dict[@"reason"];

// Not implemented?
// NSString *ID = r.allHeaderFields[@"apns-id"];
if (self.successBlock) {
self.successBlock(dict);
}

}
}];
[task resume];
}

#pragma mark - NSURLSessionDelegate

- (void)URLSession:(NSURLSession *)session task:(nonnull NSURLSessionTask *)task didReceiveChallenge:(nonnull NSURLAuthenticationChallenge *)challenge completionHandler:(nonnull void (^)(NSURLSessionAuthChallengeDisposition, NSURLCredential * _Nullable))completionHandler {
SecCertificateRef certificate;

SecIdentityCopyCertificate(self.identity, &certificate);

NSURLCredential *cred = [[NSURLCredential alloc] initWithIdentity:self.identity
certificates:@[(__bridge_transfer id)certificate]
persistence:NSURLCredentialPersistenceForSession];

completionHandler(NSURLSessionAuthChallengeUseCredential, cred);
}

@end
1 change: 1 addition & 0 deletions SmartPush/Model/Sec.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,6 @@

@property (nonatomic,strong) NSDate *date;
@property (nonatomic,copy) NSString *expire;
@property (nonatomic,assign) BOOL fromFile;

@end
4 changes: 4 additions & 0 deletions SmartPush/PushViewController.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,14 @@
@property (weak) IBOutlet NSPopUpButton *payLoadPopUpButton;
@property (weak) IBOutlet NSTextField *tokenTextField;
@property (weak) IBOutlet DragPopUpButton *cerPopUpButton;
@property (weak) IBOutlet NSSegmentedControl *prioritySegmentedControl;
@property (weak) IBOutlet NSPopUpButton *payloadTypeButton;

- (IBAction)connect:(id)sender;
- (IBAction)push:(id)sender;
- (IBAction)modeSwitch:(id)sender;
- (IBAction)payLoadButtonTouched:(id)sender;
- (IBAction)playLoadTypeTouched:(id)sender;
- (IBAction)prioritySwitch:(id)sender;

@end
Loading

0 comments on commit 6dc7378

Please sign in to comment.