Skip to content

Commit 076fd62

Browse files
authored
Merge pull request #72 from JanC/develop
Show an error when the connection to apns fails
2 parents 509c1cc + aa0980d commit 076fd62

File tree

3 files changed

+23
-6
lines changed

3 files changed

+23
-6
lines changed

Knuff/APNSViewController.m

+15-6
Original file line numberDiff line numberDiff line change
@@ -658,12 +658,21 @@ - (void)popoverDidClose:(NSNotification *)notification {
658658
#pragma mark - SBAPNSDelegate
659659

660660
- (void)APNS:(SBAPNS *)APNS didRecieveStatus:(NSInteger)statusCode reason:(NSString *)reason forID:(NSString *)ID {
661-
NSAlert *alert = [NSAlert new];
662-
[alert addButtonWithTitle:@"OK"];
663-
alert.messageText = @"Error delivering notification";
664-
alert.informativeText = [NSString stringWithFormat:@"%ld: %@", (long)statusCode, reason];
665-
666-
[alert beginSheetModalForWindow:self.document.windowForSheet completionHandler:nil];
661+
662+
[self showError:[NSString stringWithFormat:@"%ld: %@", (long)statusCode, reason]];
663+
}
664+
665+
- (void)APNS:(nonnull SBAPNS *)APNS didFailWithError:(nonnull NSError *)error {
666+
[self showError:[NSString stringWithFormat:@"Connection failed: %@", error]];
667+
}
668+
669+
- (void)showError:(NSString *)errorMessage {
670+
NSAlert *alert = [NSAlert new];
671+
[alert addButtonWithTitle:@"OK"];
672+
alert.messageText = @"Error delivering notification";
673+
alert.informativeText = errorMessage;
674+
675+
[alert beginSheetModalForWindow:self.document.windowForSheet completionHandler:nil];
667676
}
668677

669678
@end

Knuff/SBAPNS.h

+1
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212

1313
@protocol SBAPNSDelegate <NSObject>
1414
- (void)APNS:(nonnull SBAPNS *)APNS didRecieveStatus:(NSInteger)statusCode reason:(nonnull NSString *)reason forID:(nullable NSString *)ID;
15+
- (void)APNS:(nonnull SBAPNS *)APNS didFailWithError:(nonnull NSError *)error;
1516
@end
1617

1718
@interface SBAPNS : NSObject

Knuff/SBAPNS.m

+7
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,13 @@ - (void)pushPayload:(nonnull NSDictionary *)payload
7070
NSURLSessionDataTask *task = [self.session dataTaskWithRequest:request completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
7171
NSHTTPURLResponse *r = (NSHTTPURLResponse *)response;
7272

73+
if (r == nil && error) {
74+
if ([self.delegate respondsToSelector:@selector(APNS:didFailWithError:)]) {
75+
[self.delegate APNS:self didFailWithError:error];
76+
}
77+
return;
78+
}
79+
7380
if (r.statusCode != 200 && data) {
7481
NSError *error;
7582
NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:data options:0 error:&error];

0 commit comments

Comments
 (0)