diff --git a/iOS_SDK/OneSignalDevApp/OneSignalDevApp/AppDelegate.h b/iOS_SDK/OneSignalDevApp/OneSignalDevApp/AppDelegate.h index c6e8fe29f..7e651874c 100644 --- a/iOS_SDK/OneSignalDevApp/OneSignalDevApp/AppDelegate.h +++ b/iOS_SDK/OneSignalDevApp/OneSignalDevApp/AppDelegate.h @@ -31,7 +31,7 @@ #import #import -@interface AppDelegate : UIResponder +@interface AppDelegate : UIResponder @property (strong, nonatomic) UIWindow *window; diff --git a/iOS_SDK/OneSignalDevApp/OneSignalDevApp/AppDelegate.m b/iOS_SDK/OneSignalDevApp/OneSignalDevApp/AppDelegate.m index eb53ab9ea..da5664ba5 100644 --- a/iOS_SDK/OneSignalDevApp/OneSignalDevApp/AppDelegate.m +++ b/iOS_SDK/OneSignalDevApp/OneSignalDevApp/AppDelegate.m @@ -57,16 +57,6 @@ - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:( _notificationDelegate = [OneSignalNotificationCenterDelegate new]; - id openNotificationHandler = ^(OSNotificationOpenedResult *result) { - // TODO: opened handler Not triggered - NSLog(@"OSNotificationOpenedResult: %@", result.action); - #pragma clang diagnostic push - #pragma clang diagnostic ignored "-Wdeprecated" - UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Notifiation Opened In App Delegate" message:@"Notification Opened In App Delegate" delegate:self cancelButtonTitle:@"Delete" otherButtonTitles:@"Cancel", nil]; - [alert show]; - #pragma clang diagnostic pop - }; - // OneSignal Init with app id and lauch options [OneSignal setLaunchURLsInApp:YES]; [OneSignal setProvidesNotificationSettingsView:NO]; @@ -75,8 +65,7 @@ - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:( [OneSignal.InAppMessages paused:true]; [OneSignal.Notifications addForegroundLifecycleListener:self]; - [OneSignal.Notifications setNotificationOpenedHandler:openNotificationHandler]; - + [OneSignal.Notifications addClickListener:self]; [OneSignal.User.pushSubscription addObserver:self]; NSLog(@"OneSignal Demo App push subscription observer added"); @@ -115,6 +104,10 @@ - (void)onPushSubscriptionDidChangeWithState:(OSPushSubscriptionChangedState *)s mainController.subscriptionSegmentedControl.selectedSegmentIndex = (NSInteger) state.current.optedIn; } +- (void)onClickNotification:(OSNotificationClickEvent * _Nonnull)event { + NSLog(@"Dev App onClickNotification with event %@", [event jsonRepresentation]); +} + #pragma mark OSInAppMessageDelegate - (void)onClickInAppMessage:(OSInAppMessageClickEvent * _Nonnull)event { diff --git a/iOS_SDK/OneSignalDevApp/OneSignalDevAppClip/AppDelegate.m b/iOS_SDK/OneSignalDevApp/OneSignalDevAppClip/AppDelegate.m index f9e24ad62..7e11a86c5 100644 --- a/iOS_SDK/OneSignalDevApp/OneSignalDevAppClip/AppDelegate.m +++ b/iOS_SDK/OneSignalDevApp/OneSignalDevAppClip/AppDelegate.m @@ -54,10 +54,6 @@ - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:( [OneSignal.Debug setLogLevel:ONE_S_LL_VERBOSE]; [OneSignal.Debug setAlertLevel:ONE_S_LL_NONE]; _notificationDelegate = [OneSignalNotificationCenterDelegate new]; - - id openNotificationHandler = ^(OSNotificationOpenedResult *result) { - NSLog(@"OSNotificationOpenedResult: %@", result.action); - }; // OneSignal Init with app id and lauch options [OneSignal setLaunchURLsInApp:YES]; @@ -74,8 +70,6 @@ - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:( [OneSignal.InAppMessages paused:false]; - [OneSignal.Notifications setNotificationOpenedHandler:openNotificationHandler]; - NSLog(@"UNUserNotificationCenter.delegate: %@", UNUserNotificationCenter.currentNotificationCenter.delegate); return YES; diff --git a/iOS_SDK/OneSignalSDK/OneSignalCore/Source/OSNotificationClasses.h b/iOS_SDK/OneSignalSDK/OneSignalCore/Source/OSNotificationClasses.h index 85dcb7cdb..dd4a8ce18 100644 --- a/iOS_SDK/OneSignalSDK/OneSignalCore/Source/OSNotificationClasses.h +++ b/iOS_SDK/OneSignalSDK/OneSignalCore/Source/OSNotificationClasses.h @@ -36,20 +36,17 @@ typedef NS_ENUM(NSUInteger, OSNotificationActionType) { OSNotificationActionTypeActionTaken }; -@interface OSNotificationAction : NSObject - -/* The type of the notification action */ -@property(readonly)OSNotificationActionType type; - +@interface OSNotificationClickResult : NSObject /* The ID associated with the button tapped. NULL when the actionType is NotificationTapped */ @property(readonly, nullable)NSString* actionId; +@property(readonly, nullable)NSString* url; @end -@interface OSNotificationOpenedResult : NSObject +@interface OSNotificationClickEvent : NSObject @property(readonly, nonnull)OSNotification* notification; -@property(readonly, nonnull)OSNotificationAction *action; +@property(readonly, nonnull)OSNotificationClickResult *result; /* Convert object into an NSString that can be convertible into a custom Dictionary / JSON Object */ - (NSString* _Nonnull)stringify; diff --git a/iOS_SDK/OneSignalSDK/OneSignalCore/Source/OneSignalTrackFirebaseAnalytics.h b/iOS_SDK/OneSignalSDK/OneSignalCore/Source/OneSignalTrackFirebaseAnalytics.h index b40a02eb5..948541aaf 100644 --- a/iOS_SDK/OneSignalSDK/OneSignalCore/Source/OneSignalTrackFirebaseAnalytics.h +++ b/iOS_SDK/OneSignalSDK/OneSignalCore/Source/OneSignalTrackFirebaseAnalytics.h @@ -33,7 +33,7 @@ +(void)init; +(void)updateFromDownloadParams:(NSDictionary*)params; -+(void)trackOpenEvent:(OSNotificationOpenedResult*)results; ++(void)trackOpenEvent:(OSNotificationClickEvent*)event; +(void)trackReceivedEvent:(OSNotification*)notification; +(void)trackInfluenceOpenEvent; @end diff --git a/iOS_SDK/OneSignalSDK/OneSignalCore/Source/OneSignalTrackFirebaseAnalytics.m b/iOS_SDK/OneSignalSDK/OneSignalCore/Source/OneSignalTrackFirebaseAnalytics.m index 6c1416c87..94acbb8ae 100644 --- a/iOS_SDK/OneSignalSDK/OneSignalCore/Source/OneSignalTrackFirebaseAnalytics.m +++ b/iOS_SDK/OneSignalSDK/OneSignalCore/Source/OneSignalTrackFirebaseAnalytics.m @@ -87,7 +87,7 @@ + (NSString*)getCampaignNameFromNotification:(OSNotification *)notification { return [notification.title substringToIndex:titleLength]; } -+ (void)trackOpenEvent:(OSNotificationOpenedResult*)results { ++ (void)trackOpenEvent:(OSNotificationClickEvent*)event { if (!trackingEnabled) return; @@ -97,8 +97,8 @@ + (void)trackOpenEvent:(OSNotificationOpenedResult*)results { parameters:@{ @"source": @"OneSignal", @"medium": @"notification", - @"notification_id": results.notification.notificationId, - @"campaign": [self getCampaignNameFromNotification:results.notification] + @"notification_id": event.notification.notificationId, + @"campaign": [self getCampaignNameFromNotification:event.notification] }]; } diff --git a/iOS_SDK/OneSignalSDK/OneSignalNotifications/OSNotificationsManager.h b/iOS_SDK/OneSignalSDK/OneSignalNotifications/OSNotificationsManager.h index 63a031a92..6c99c2f66 100644 --- a/iOS_SDK/OneSignalSDK/OneSignalNotifications/OSNotificationsManager.h +++ b/iOS_SDK/OneSignalSDK/OneSignalNotifications/OSNotificationsManager.h @@ -32,7 +32,10 @@ #import #import -typedef void (^OSNotificationOpenedBlock)(OSNotificationOpenedResult * _Nonnull result); +@protocol OSNotificationClickListener +- (void)onClickNotification:(OSNotificationClickEvent *_Nonnull)event +NS_SWIFT_NAME(onClick(event:)); +@end @interface OSNotificationWillDisplayEvent : NSObject @@ -54,8 +57,8 @@ typedef void (^OSNotificationOpenedBlock)(OSNotificationOpenedResult * _Nonnull + (OSNotificationPermission)permissionNative NS_REFINED_FOR_SWIFT; + (void)addForegroundLifecycleListener:(NSObject *_Nullable)listener; + (void)removeForegroundLifecycleListener:(NSObject *_Nullable)listener; - -+ (void)setNotificationOpenedHandler:(OSNotificationOpenedBlock _Nullable)block; ++ (void)addClickListener:(NSObject*_Nonnull)listener NS_REFINED_FOR_SWIFT; ++ (void)removeClickListener:(NSObject*_Nonnull)listener NS_REFINED_FOR_SWIFT; + (void)requestPermission:(OSUserResponseBlock _Nullable )block; + (void)requestPermission:(OSUserResponseBlock _Nullable )block fallbackToSettings:(BOOL)fallback; + (void)registerForProvisionalAuthorization:(OSUserResponseBlock _Nullable )block NS_REFINED_FOR_SWIFT; @@ -111,8 +114,7 @@ typedef void (^OSNotificationOpenedBlock)(OSNotificationOpenedResult * _Nonnull + (void)setPushSubscriptionId:(NSString *_Nullable)pushSubscriptionId; + (void)handleWillShowInForegroundForNotification:(OSNotification *_Nonnull)notification completion:(OSNotificationDisplayResponse _Nonnull)completion; -+ (void)handleNotificationAction:(OSNotificationActionType)actionType actionID:(NSString* _Nonnull)actionID; - ++ (void)handleNotificationActionWithUrl:(NSString* _Nullable)url actionID:(NSString* _Nonnull)actionID; + (BOOL)clearBadgeCount:(BOOL)fromNotifOpened; + (BOOL)receiveRemoteNotification:(UIApplication* _Nonnull)application UserInfo:(NSDictionary* _Nonnull)userInfo completionHandler:(void (^_Nonnull)(UIBackgroundFetchResult))completionHandler; diff --git a/iOS_SDK/OneSignalSDK/OneSignalNotifications/OSNotificationsManager.m b/iOS_SDK/OneSignalSDK/OneSignalNotifications/OSNotificationsManager.m index 003b2fdb9..c06748df1 100644 --- a/iOS_SDK/OneSignalSDK/OneSignalNotifications/OSNotificationsManager.m +++ b/iOS_SDK/OneSignalSDK/OneSignalNotifications/OSNotificationsManager.m @@ -35,14 +35,14 @@ of this software and associated documentation files (the "Software"), to deal #import "UNUserNotificationCenter+OneSignalNotifications.h" #import "UIApplicationDelegate+OneSignalNotifications.h" -@implementation OSNotificationOpenedResult -@synthesize notification = _notification, action = _action; +@implementation OSNotificationClickEvent +@synthesize notification = _notification, result = _result; -- (id)initWithNotification:(OSNotification*)notification action:(OSNotificationAction*)action { +- (id)initWithNotification:(OSNotification*)notification result:(OSNotificationClickResult*)result { self = [super init]; if(self) { _notification = notification; - _action = action; + _result = result; } return self; } @@ -63,25 +63,24 @@ - (NSDictionary *_Nonnull)jsonRepresentation { error:&jsonError]; NSMutableDictionary* obj = [NSMutableDictionary new]; - NSMutableDictionary* action = [NSMutableDictionary new]; - [action setObject:self.action.actionId forKeyedSubscript:@"actionID"]; - [obj setObject:action forKeyedSubscript:@"action"]; + NSMutableDictionary* result = [NSMutableDictionary new]; + [result setObject:self.result.actionId forKeyedSubscript:@"actionID"]; + [result setObject:self.result.url forKeyedSubscript:@"url"]; + [obj setObject:result forKeyedSubscript:@"result"]; [obj setObject:notifDict forKeyedSubscript:@"notification"]; - if(self.action.type) - [obj[@"action"] setObject:@(self.action.type) forKeyedSubscript: @"type"]; - + return obj; } @end -@implementation OSNotificationAction -@synthesize type = _type, actionId = _actionId; +@implementation OSNotificationClickResult +@synthesize url = _url, actionId = _actionId; --(id)initWithActionType:(OSNotificationActionType)type :(NSString*)actionID { +-(id)initWithUrl:(NSString*)url :(NSString*)actionID { self = [super init]; if(self) { - _type = type; + _url = url; _actionId = actionID; } return self; @@ -149,8 +148,14 @@ + (void)setLifecycleListener:(id)lifecycleListe static int mSubscriptionStatus = -1; -static NSMutableArray *_unprocessedOpenedNotifis; -static OSNotificationOpenedBlock _notificationOpenedHandler; +static NSMutableArray *_unprocessedClickEvents; +static NSMutableArray *> *_clickListeners; ++ (NSMutableArray *>*)clickListeners { + if (!_clickListeners) + _clickListeners = [NSMutableArray new]; + return _clickListeners; +} + static NSDictionary* _lastMessageReceived; static NSString *_lastMessageID = @""; static NSString *_lastMessageIdFromAction; @@ -255,7 +260,7 @@ + (void)resetLocals { _lastMessageReceived = nil; _lastMessageIdFromAction = nil; _lastMessageID = @""; - _unprocessedOpenedNotifis = nil; + _unprocessedClickEvents = nil; } + (void)setLastPermissionState:(OSPermissionStateInternal *)lastPermissionState { @@ -693,7 +698,7 @@ + (void)handleNotificationOpened:(NSDictionary*)messageDict // [[OSSessionManager sharedSessionManager] onDirectInfluenceFromNotificationOpen:_appEntryState withNotificationId:messageId]; // } - [self handleNotificationAction:actionType actionID:actionID]; + [self handleNotificationActionWithUrl:notification.launchURL actionID:actionID]; } + (void)submitNotificationOpened:(NSString*)messageId { @@ -793,28 +798,35 @@ + (BOOL)handleIAMPreview:(OSNotification *)notification { return NO; } -+ (void)handleNotificationAction:(OSNotificationActionType)actionType actionID:(NSString*)actionID { ++ (void)handleNotificationActionWithUrl:(NSString*)url actionID:(NSString*)actionID { if (![OneSignalCoreHelper isOneSignalPayload:_lastMessageReceived]) return; - OSNotificationAction *action = [[OSNotificationAction alloc] initWithActionType:actionType :actionID]; + OSNotificationClickResult *result = [[OSNotificationClickResult alloc] initWithUrl:url :actionID]; OSNotification *notification = [OSNotification parseWithApns:_lastMessageReceived]; - OSNotificationOpenedResult *result = [[OSNotificationOpenedResult alloc] initWithNotification:notification action:action]; + OSNotificationClickEvent *event = [[OSNotificationClickEvent alloc] initWithNotification:notification result:result]; // Prevent duplicate calls to same action if ([notification.notificationId isEqualToString:_lastMessageIdFromAction]) return; _lastMessageIdFromAction = notification.notificationId; - [OneSignalTrackFirebaseAnalytics trackOpenEvent:result]; - - if (!_notificationOpenedHandler) { - [self addUnprocessedOpenedNotifi:result]; + [OneSignalTrackFirebaseAnalytics trackOpenEvent:event]; + + if (self.clickListeners.count == 0) { + [self addUnprocessedClickEvent:event]; return; } - _notificationOpenedHandler(result); + [self fireClickListenersForEvent:event]; } ++ (void)fireClickListenersForEvent:(OSNotificationClickEvent*)event { + for (NSObject *listener in self.clickListeners) { + if ([listener respondsToSelector:@selector(onClickNotification:)]) { + [listener onClickNotification:event]; + } + } +} + (void)lastMessageReceived:(NSDictionary*)message { _lastMessageReceived = message; @@ -828,11 +840,15 @@ + (BOOL)shouldSuppressURL { return shouldSuppress ?: false; } -+ (void)setNotificationOpenedHandler:(OSNotificationOpenedBlock)block { - _notificationOpenedHandler = block; - [OneSignalLog onesignalLog:ONE_S_LL_VERBOSE message:@"Notification opened handler set successfully"]; - [self fireNotificationOpenedHandlerForUnprocessedEvents]; - ++ (void)addClickListener:(NSObject*)listener { + [self.clickListeners addObject:listener]; + [OneSignalLog onesignalLog:ONE_S_LL_VERBOSE message:@"Notification click listener added successfully"]; + [self fireClickListenersForUnprocessedEvents]; +} + ++ (void)removeClickListener:(NSObject*)listener { + [self.clickListeners removeObject:listener]; + [OneSignalLog onesignalLog:ONE_S_LL_VERBOSE message:@"Notification click listener removed successfully"]; } + (void)addForegroundLifecycleListener:(NSObject *_Nullable)listener { @@ -845,24 +861,24 @@ + (void)removeForegroundLifecycleListener:(NSObject*)getUnprocessedOpenedNotifis { - if (!_unprocessedOpenedNotifis) - _unprocessedOpenedNotifis = [NSMutableArray new]; - return _unprocessedOpenedNotifis; ++ (NSMutableArray*)getUnprocessedClickEvents { + if (!_unprocessedClickEvents) + _unprocessedClickEvents = [NSMutableArray new]; + return _unprocessedClickEvents; } -+ (void)addUnprocessedOpenedNotifi:(OSNotificationOpenedResult*)result { - [[self getUnprocessedOpenedNotifis] addObject:result]; ++ (void)addUnprocessedClickEvent:(OSNotificationClickEvent*)event { + [[self getUnprocessedClickEvents] addObject:event]; } -+ (void)fireNotificationOpenedHandlerForUnprocessedEvents { - if (!_notificationOpenedHandler) ++ (void)fireClickListenersForUnprocessedEvents { + if (self.clickListeners.count == 0) { return; - - for (OSNotificationOpenedResult* notification in [self getUnprocessedOpenedNotifis]) { - _notificationOpenedHandler(notification); } - _unprocessedOpenedNotifis = [NSMutableArray new]; + for (OSNotificationClickEvent* event in [self getUnprocessedClickEvents]) { + [self fireClickListenersForEvent:event]; + } + _unprocessedClickEvents = [NSMutableArray new]; } + (BOOL)receiveRemoteNotification:(UIApplication*)application UserInfo:(NSDictionary*)userInfo completionHandler:(void (^)(UIBackgroundFetchResult))completionHandler { diff --git a/iOS_SDK/OneSignalSDK/Source/OneSignalSwiftInterface.swift b/iOS_SDK/OneSignalSDK/Source/OneSignalSwiftInterface.swift index f9f64e878..7dfaf6981 100644 --- a/iOS_SDK/OneSignalSDK/Source/OneSignalSwiftInterface.swift +++ b/iOS_SDK/OneSignalSDK/Source/OneSignalSwiftInterface.swift @@ -124,6 +124,14 @@ public extension OSNotifications { static func removePermissionObserver(_ observer: OSNotificationPermissionObserver) { return __remove(observer) } + + static func addClickListener(_ listener: OSNotificationClickListener) { + return __add(listener) + } + + static func removeClickListener(_ listener: OSNotificationClickListener) { + return __remove(listener) + } } public extension OSLocation {