Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Extension framework #427

Merged
merged 4 commits into from
Oct 4, 2018
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
#import <OneSignal/OneSignal.h>

#import <UIKit/UIKit.h>

#import "NotificationService.h"

#import <OneSignalExtension/OneSignalExtension.h>
#import <FirebaseAnalytics/FIRApp.h>
#import <FirebaseAnalytics/FIRAnalytics.h>

Expand Down Expand Up @@ -62,7 +59,7 @@ - (void)didReceiveNotificationRequest:(UNNotificationRequest *)request withConte

NSLog(@"START!!!!!! request.content.userInfo: %@", request.content.userInfo);

[OneSignal didReceiveNotificationExtensionRequest:self.receivedRequest withMutableNotificationContent:self.bestAttemptContent];
[OneSignalExtension didReceiveNotificationExtensionRequest:self.receivedRequest withMutableNotificationContent:self.bestAttemptContent];
// DEBUGGING: Uncomment the 2 lines below and comment out the one above to ensure this extension is excuting
// Note, this extension only runs when mutable-content is set
// Setting an attachment or action buttons automatically adds this
Expand Down Expand Up @@ -90,7 +87,7 @@ - (void)serviceExtensionTimeWillExpire {
// Called just before the extension will be terminated by the system.
// Use this as an opportunity to deliver your "best attempt" at modified content, otherwise the original push payload will be used.

[OneSignal serviceExtensionTimeWillExpireRequest:self.receivedRequest withMutableNotificationContent:self.bestAttemptContent];
[OneSignalExtension serviceExtensionTimeWillExpireRequest:self.receivedRequest withMutableNotificationContent:self.bestAttemptContent];

self.contentHandler(self.bestAttemptContent);
}
Expand Down
274 changes: 252 additions & 22 deletions iOS_SDK/OneSignalSDK/OneSignal.xcodeproj/project.pbxproj

Large diffs are not rendered by default.

22 changes: 22 additions & 0 deletions iOS_SDK/OneSignalSDK/OneSignalExtension/Info.plist
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>CFBundleDevelopmentRegion</key>
<string>$(DEVELOPMENT_LANGUAGE)</string>
<key>CFBundleExecutable</key>
<string>$(EXECUTABLE_NAME)</string>
<key>CFBundleIdentifier</key>
<string>$(PRODUCT_BUNDLE_IDENTIFIER)</string>
<key>CFBundleInfoDictionaryVersion</key>
<string>6.0</string>
<key>CFBundleName</key>
<string>$(PRODUCT_NAME)</string>
<key>CFBundlePackageType</key>
<string>FMWK</string>
<key>CFBundleShortVersionString</key>
<string>1.0</string>
<key>CFBundleVersion</key>
<string>$(CURRENT_PROJECT_VERSION)</string>
</dict>
</plist>
29 changes: 29 additions & 0 deletions iOS_SDK/OneSignalSDK/OneSignalExtension/NSURLSession+OneSignal.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
//
// NSURLSession+OneSignal.h
// OneSignal
//
// Created by Brad Hesse on 9/26/18.
// Copyright © 2018 Hiptic. All rights reserved.
//

#import <Foundation/Foundation.h>

NS_ASSUME_NONNULL_BEGIN

@interface DirectDownloadDelegate : NSObject <NSURLSessionDataDelegate> {
NSError* error;
NSURLResponse* response;
BOOL done;
NSFileHandle* outputHandle;
}
@property (readonly, getter=isDone) BOOL done;
@property (readonly) NSError* error;
@property (readonly) NSURLResponse* response;

@end

NS_ASSUME_NONNULL_END

@interface NSURLSession (DirectDownload)
+ (NSString *)downloadItemAtURL:(NSURL *)url toFile:(NSString *)localPath error:(NSError **)error;
@end
77 changes: 77 additions & 0 deletions iOS_SDK/OneSignalSDK/OneSignalExtension/NSURLSession+OneSignal.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
//
// NSURLSession+OneSignal.m
// OneSignal
//
// Created by Brad Hesse on 9/26/18.
// Copyright © 2018 Hiptic. All rights reserved.
//

#import "NSURLSession+OneSignal.h"

@implementation DirectDownloadDelegate
@synthesize error, response, done;

-(void)URLSession:(NSURLSession *)session dataTask:(NSURLSessionDataTask *)dataTask didReceiveData:(NSData *)data {
[outputHandle writeData:data];
}

-(void)URLSession:(NSURLSession *)session dataTask:(NSURLSessionDataTask *)dataTask didReceiveResponse:(NSURLResponse *)aResponse completionHandler:(void (^)(NSURLSessionResponseDisposition))completionHandler {
response = aResponse;
completionHandler(NSURLSessionResponseAllow);
}

-(void)URLSession:(NSURLSession *)session didBecomeInvalidWithError:(NSError *)anError {
error = anError;
done = YES;

[outputHandle closeFile];
}

-(void)URLSession:(NSURLSession *)session task:(NSURLSessionTask *)task didCompleteWithError:(NSError *)anError {
done = YES;
error = anError;
[outputHandle closeFile];
}

- (id)initWithFilePath:(NSString*)path {
if (self = [super init]) {
if ([[NSFileManager defaultManager] fileExistsAtPath:path])
[[NSFileManager defaultManager] removeItemAtPath:path error:nil];

[[NSFileManager defaultManager] createFileAtPath:path contents:nil attributes:nil];
outputHandle = [NSFileHandle fileHandleForWritingAtPath:path];
}
return self;
}
@end

@implementation NSURLSession (DirectDownload)

+ (NSString * _Nullable)downloadItemAtURL:(NSURL * _Nonnull)url toFile:(NSString * _Nonnull)localPath error:(NSError **)error {
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:url];

DirectDownloadDelegate *delegate = [[DirectDownloadDelegate alloc] initWithFilePath:localPath];

NSURLSession *session = [NSURLSession sessionWithConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration] delegate:delegate delegateQueue:nil];

NSURLSessionDataTask *task = [session dataTaskWithRequest:request];

[task resume];

[session finishTasksAndInvalidate];

while (![delegate isDone]) {
[[NSRunLoop currentRunLoop] runUntilDate:[NSDate dateWithTimeIntervalSinceNow:1.0]];
}

NSError *downloadError = [delegate error];
if (downloadError != nil) {
if (error)
*error = downloadError;
return nil;
}

return delegate.response.MIMEType;
}

@end
24 changes: 24 additions & 0 deletions iOS_SDK/OneSignalSDK/OneSignalExtension/OneSignalExtension.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
//
// OneSignalExtension.h
// OneSignalExtension
//
// Created by Brad Hesse on 9/26/18.
// Copyright © 2018 Hiptic. All rights reserved.
//

#import <UIKit/UIKit.h>
#import <UserNotifications/UserNotifications.h>

//! Project version number for OneSignalExtension.
FOUNDATION_EXPORT double OneSignalExtensionVersionNumber;

//! Project version string for OneSignalExtension.
FOUNDATION_EXPORT const unsigned char OneSignalExtensionVersionString[];

// In this header, you should import all the public headers of your framework using statements like #import <OneSignalExtension/PublicHeader.h>


@interface OneSignalExtension : NSObject
+ (UNMutableNotificationContent*)didReceiveNotificationExtensionRequest:(UNNotificationRequest*)request withMutableNotificationContent:(UNMutableNotificationContent*)replacementContent;
+ (UNMutableNotificationContent*)serviceExtensionTimeWillExpireRequest:(UNNotificationRequest*)request withMutableNotificationContent:(UNMutableNotificationContent*)replacementContent;
@end
38 changes: 38 additions & 0 deletions iOS_SDK/OneSignalSDK/OneSignalExtension/OneSignalExtension.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
//
// OneSignalExtension.m
// OneSignalExtension
//
// Created by Brad Hesse on 9/26/18.
// Copyright © 2018 Hiptic. All rights reserved.
//

#import "OneSignalExtension.h"
#import "OneSignalNotificationServiceExtensionHandler.h"
#import "NSDictionary+OneSignal.h"

@implementation OneSignalExtension

// Called from the app's Notification Service Extension
+ (UNMutableNotificationContent*)didReceiveNotificationExtensionRequest:(UNNotificationRequest*)request withMutableNotificationContent:(UNMutableNotificationContent*)replacementContent {

if (!request.content.userInfo.isOneSignalPayload)
return replacementContent;

return [OneSignalNotificationServiceExtensionHandler
didReceiveNotificationExtensionRequest:request
withMutableNotificationContent:replacementContent];
}


// Called from the app's Notification Service Extension
+ (UNMutableNotificationContent*)serviceExtensionTimeWillExpireRequest:(UNNotificationRequest*)request withMutableNotificationContent:(UNMutableNotificationContent*)replacementContent {

if (!request.content.userInfo.isOneSignalPayload)
return replacementContent;

return [OneSignalNotificationServiceExtensionHandler
serviceExtensionTimeWillExpireRequest:request
withMutableNotificationContent:replacementContent];
}

@end
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,9 @@

#import "OneSignalNotificationServiceExtensionHandler.h"
#import "OneSignalExtensionBadgeHandler.h"
#import "OneSignalHelper.h"
#import "OneSignalTrackFirebaseAnalytics.h"
#import "OSNotificationPayload+Internal.h"
#import "OneSignalAttachmentsController.h"

@implementation OneSignalNotificationServiceExtensionHandler

Expand All @@ -38,7 +38,7 @@ +(UNMutableNotificationContent*)didReceiveNotificationExtensionRequest:(UNNotifi
if (!replacementContent)
replacementContent = [request.content mutableCopy];

let payload = [OSNotificationPayload parseWithApns:request.content.userInfo];
OSNotificationPayload *payload = [OSNotificationPayload parseWithApns:request.content.userInfo];

//handle badge count
[OneSignalExtensionBadgeHandler handleBadgeCountWithNotificationRequest:request withNotificationPayload:payload withMutableNotificationContent:replacementContent];
Expand All @@ -52,7 +52,7 @@ +(UNMutableNotificationContent*)didReceiveNotificationExtensionRequest:(UNNotifi
withMutableNotificationContent:replacementContent];

// Media Attachments
[OneSignalHelper addAttachments:payload toNotificationContent:replacementContent];
[OneSignalAttachmentsController addAttachments:payload toNotificationContent:replacementContent];

return replacementContent;
}
Expand All @@ -62,7 +62,7 @@ +(UNMutableNotificationContent*)serviceExtensionTimeWillExpireRequest:(UNNotific
if (!replacementContent)
replacementContent = [request.content mutableCopy];

let payload = [OSNotificationPayload parseWithApns:request.content.userInfo];
OSNotificationPayload *payload = [OSNotificationPayload parseWithApns:request.content.userInfo];

[self addActionButtonsToExtentionRequest:request
withPayload:payload
Expand All @@ -78,7 +78,7 @@ +(void)addActionButtonsToExtentionRequest:(UNNotificationRequest*)request
if (request.content.categoryIdentifier && ![request.content.categoryIdentifier isEqualToString:@""])
return;

[OneSignalHelper addActionButtons:payload toNotificationContent:replacementContent];
[OneSignalAttachmentsController addActionButtons:payload toNotificationContent:replacementContent];
}

@end
17 changes: 17 additions & 0 deletions iOS_SDK/OneSignalSDK/Source/NSDictionary+OneSignal.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
//
// NSDictionary+OneSignal.h
// OneSignal
//
// Created by Brad Hesse on 9/26/18.
// Copyright © 2018 Hiptic. All rights reserved.
//

#import <Foundation/Foundation.h>

NS_ASSUME_NONNULL_BEGIN

@interface NSDictionary (OneSignal)
- (BOOL)isOneSignalPayload;
@end

NS_ASSUME_NONNULL_END
18 changes: 18 additions & 0 deletions iOS_SDK/OneSignalSDK/Source/NSDictionary+OneSignal.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
//
// NSDictionary+OneSignal.m
// OneSignal
//
// Created by Brad Hesse on 9/26/18.
// Copyright © 2018 Hiptic. All rights reserved.
//

#import "NSDictionary+OneSignal.h"

@implementation NSDictionary (OneSignal)

// Prevent the OSNotification blocks from firing if we receive a Non-OneSignal remote push
- (BOOL)isOneSignalPayload {
return self[@"custom"][@"i"] || self[@"os_data"][@"i"];
}

@end
3 changes: 3 additions & 0 deletions iOS_SDK/OneSignalSDK/Source/NSString+OneSignal.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@
- (NSString*)one_getSemanticVersion;
- (NSString *)fileExtensionForMimeType;
- (NSString *)supportedFileExtension;
- (NSString *)stringByRemovingWhitespace;

+(NSString*)randomStringWithLength:(int)length;

@end
#endif
15 changes: 15 additions & 0 deletions iOS_SDK/OneSignalSDK/Source/NSString+OneSignal.m
Original file line number Diff line number Diff line change
Expand Up @@ -59,4 +59,19 @@ - (NSString *)fileExtensionForMimeType {
return MIME_MAP[self];
}

- (NSString *)stringByRemovingWhitespace {
return [self stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
}

+(NSString*)randomStringWithLength:(int)length {
NSString *letters = @"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
NSMutableString *randomString = [[NSMutableString alloc] initWithCapacity:length];
for(int i = 0; i < length; i++) {
uint32_t ln = (uint32_t)letters.length;
uint32_t rand = arc4random_uniform(ln);
[randomString appendFormat:@"%C", [letters characterAtIndex:rand]];
}
return randomString;
}

@end
2 changes: 2 additions & 0 deletions iOS_SDK/OneSignalSDK/Source/NSURL+OneSignal.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,6 @@

- (NSString *)valueFromQueryParameter:(NSString *)parameter;

- (BOOL)isWWWScheme;

@end
5 changes: 5 additions & 0 deletions iOS_SDK/OneSignalSDK/Source/NSURL+OneSignal.m
Original file line number Diff line number Diff line change
Expand Up @@ -37,4 +37,9 @@ - (NSString *)valueFromQueryParameter:(NSString *)parameter {

return nil;
}

- (BOOL)isWWWScheme {
NSString* urlScheme = [self.scheme lowercaseString];
return [urlScheme isEqualToString:@"http"] || [urlScheme isEqualToString:@"https"];
}
@end
2 changes: 0 additions & 2 deletions iOS_SDK/OneSignalSDK/Source/OSNotificationPayload.m
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,6 @@

#import "OSNotificationPayload+Internal.h"

#import "OneSignal.h"

@implementation OSNotificationPayload

+(instancetype)parseWithApns:(NSDictionary*)message {
Expand Down
Loading