Skip to content

Commit 553d31c

Browse files
authored
Merge pull request #1262 from OneSignal/5.0.0/notification_click_listener_api
[5.0.0] Notification Click Listener - API update
2 parents f5c34df + 95efdd1 commit 553d31c

File tree

9 files changed

+88
-78
lines changed

9 files changed

+88
-78
lines changed

iOS_SDK/OneSignalDevApp/OneSignalDevApp/AppDelegate.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131
#import <UIKit/UIKit.h>
3232
#import <OneSignalFramework/OneSignalFramework.h>
3333

34-
@interface AppDelegate : UIResponder <UIApplicationDelegate, OSNotificationPermissionObserver, OSInAppMessageLifecycleListener, OSPushSubscriptionObserver, OSNotificationLifecycleListener, OSInAppMessageClickListener>
34+
@interface AppDelegate : UIResponder <UIApplicationDelegate, OSNotificationPermissionObserver, OSInAppMessageLifecycleListener, OSPushSubscriptionObserver, OSNotificationLifecycleListener, OSInAppMessageClickListener, OSNotificationClickListener>
3535

3636
@property (strong, nonatomic) UIWindow *window;
3737

iOS_SDK/OneSignalDevApp/OneSignalDevApp/AppDelegate.m

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -57,16 +57,6 @@ - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(
5757

5858
_notificationDelegate = [OneSignalNotificationCenterDelegate new];
5959

60-
id openNotificationHandler = ^(OSNotificationOpenedResult *result) {
61-
// TODO: opened handler Not triggered
62-
NSLog(@"OSNotificationOpenedResult: %@", result.action);
63-
#pragma clang diagnostic push
64-
#pragma clang diagnostic ignored "-Wdeprecated"
65-
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Notifiation Opened In App Delegate" message:@"Notification Opened In App Delegate" delegate:self cancelButtonTitle:@"Delete" otherButtonTitles:@"Cancel", nil];
66-
[alert show];
67-
#pragma clang diagnostic pop
68-
};
69-
7060
// OneSignal Init with app id and lauch options
7161
[OneSignal setLaunchURLsInApp:YES];
7262
[OneSignal setProvidesNotificationSettingsView:NO];
@@ -75,8 +65,7 @@ - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(
7565
[OneSignal.InAppMessages paused:true];
7666

7767
[OneSignal.Notifications addForegroundLifecycleListener:self];
78-
[OneSignal.Notifications setNotificationOpenedHandler:openNotificationHandler];
79-
68+
[OneSignal.Notifications addClickListener:self];
8069
[OneSignal.User.pushSubscription addObserver:self];
8170
NSLog(@"OneSignal Demo App push subscription observer added");
8271

@@ -115,6 +104,10 @@ - (void)onPushSubscriptionDidChangeWithState:(OSPushSubscriptionChangedState *)s
115104
mainController.subscriptionSegmentedControl.selectedSegmentIndex = (NSInteger) state.current.optedIn;
116105
}
117106

107+
- (void)onClickNotification:(OSNotificationClickEvent * _Nonnull)event {
108+
NSLog(@"Dev App onClickNotification with event %@", [event jsonRepresentation]);
109+
}
110+
118111
#pragma mark OSInAppMessageDelegate
119112

120113
- (void)onClickInAppMessage:(OSInAppMessageClickEvent * _Nonnull)event {

iOS_SDK/OneSignalDevApp/OneSignalDevAppClip/AppDelegate.m

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -54,10 +54,6 @@ - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(
5454
[OneSignal.Debug setLogLevel:ONE_S_LL_VERBOSE];
5555
[OneSignal.Debug setAlertLevel:ONE_S_LL_NONE];
5656
_notificationDelegate = [OneSignalNotificationCenterDelegate new];
57-
58-
id openNotificationHandler = ^(OSNotificationOpenedResult *result) {
59-
NSLog(@"OSNotificationOpenedResult: %@", result.action);
60-
};
6157

6258
// OneSignal Init with app id and lauch options
6359
[OneSignal setLaunchURLsInApp:YES];
@@ -74,8 +70,6 @@ - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(
7470

7571
[OneSignal.InAppMessages paused:false];
7672

77-
[OneSignal.Notifications setNotificationOpenedHandler:openNotificationHandler];
78-
7973
NSLog(@"UNUserNotificationCenter.delegate: %@", UNUserNotificationCenter.currentNotificationCenter.delegate);
8074

8175
return YES;

iOS_SDK/OneSignalSDK/OneSignalCore/Source/OSNotificationClasses.h

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -36,20 +36,17 @@ typedef NS_ENUM(NSUInteger, OSNotificationActionType) {
3636
OSNotificationActionTypeActionTaken
3737
};
3838

39-
@interface OSNotificationAction : NSObject
40-
41-
/* The type of the notification action */
42-
@property(readonly)OSNotificationActionType type;
43-
39+
@interface OSNotificationClickResult : NSObject
4440
/* The ID associated with the button tapped. NULL when the actionType is NotificationTapped */
4541
@property(readonly, nullable)NSString* actionId;
42+
@property(readonly, nullable)NSString* url;
4643

4744
@end
4845

49-
@interface OSNotificationOpenedResult : NSObject
46+
@interface OSNotificationClickEvent : NSObject
5047

5148
@property(readonly, nonnull)OSNotification* notification;
52-
@property(readonly, nonnull)OSNotificationAction *action;
49+
@property(readonly, nonnull)OSNotificationClickResult *result;
5350

5451
/* Convert object into an NSString that can be convertible into a custom Dictionary / JSON Object */
5552
- (NSString* _Nonnull)stringify;

iOS_SDK/OneSignalSDK/OneSignalCore/Source/OneSignalTrackFirebaseAnalytics.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@
3333
+(void)init;
3434
+(void)updateFromDownloadParams:(NSDictionary*)params;
3535

36-
+(void)trackOpenEvent:(OSNotificationOpenedResult*)results;
36+
+(void)trackOpenEvent:(OSNotificationClickEvent*)event;
3737
+(void)trackReceivedEvent:(OSNotification*)notification;
3838
+(void)trackInfluenceOpenEvent;
3939
@end

iOS_SDK/OneSignalSDK/OneSignalCore/Source/OneSignalTrackFirebaseAnalytics.m

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ + (NSString*)getCampaignNameFromNotification:(OSNotification *)notification {
8787
return [notification.title substringToIndex:titleLength];
8888
}
8989

90-
+ (void)trackOpenEvent:(OSNotificationOpenedResult*)results {
90+
+ (void)trackOpenEvent:(OSNotificationClickEvent*)event {
9191
if (!trackingEnabled)
9292
return;
9393

@@ -97,8 +97,8 @@ + (void)trackOpenEvent:(OSNotificationOpenedResult*)results {
9797
parameters:@{
9898
@"source": @"OneSignal",
9999
@"medium": @"notification",
100-
@"notification_id": results.notification.notificationId,
101-
@"campaign": [self getCampaignNameFromNotification:results.notification]
100+
@"notification_id": event.notification.notificationId,
101+
@"campaign": [self getCampaignNameFromNotification:event.notification]
102102
}];
103103
}
104104

iOS_SDK/OneSignalSDK/OneSignalNotifications/OSNotificationsManager.h

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,10 @@
3232
#import <UIKit/UIKit.h>
3333
#import <OneSignalNotifications/OSNotification+OneSignal.h>
3434

35-
typedef void (^OSNotificationOpenedBlock)(OSNotificationOpenedResult * _Nonnull result);
35+
@protocol OSNotificationClickListener <NSObject>
36+
- (void)onClickNotification:(OSNotificationClickEvent *_Nonnull)event
37+
NS_SWIFT_NAME(onClick(event:));
38+
@end
3639

3740
@interface OSNotificationWillDisplayEvent : NSObject
3841

@@ -54,8 +57,8 @@ typedef void (^OSNotificationOpenedBlock)(OSNotificationOpenedResult * _Nonnull
5457
+ (OSNotificationPermission)permissionNative NS_REFINED_FOR_SWIFT;
5558
+ (void)addForegroundLifecycleListener:(NSObject<OSNotificationLifecycleListener> *_Nullable)listener;
5659
+ (void)removeForegroundLifecycleListener:(NSObject<OSNotificationLifecycleListener> *_Nullable)listener;
57-
58-
+ (void)setNotificationOpenedHandler:(OSNotificationOpenedBlock _Nullable)block;
60+
+ (void)addClickListener:(NSObject<OSNotificationClickListener>*_Nonnull)listener NS_REFINED_FOR_SWIFT;
61+
+ (void)removeClickListener:(NSObject<OSNotificationClickListener>*_Nonnull)listener NS_REFINED_FOR_SWIFT;
5962
+ (void)requestPermission:(OSUserResponseBlock _Nullable )block;
6063
+ (void)requestPermission:(OSUserResponseBlock _Nullable )block fallbackToSettings:(BOOL)fallback;
6164
+ (void)registerForProvisionalAuthorization:(OSUserResponseBlock _Nullable )block NS_REFINED_FOR_SWIFT;
@@ -111,8 +114,7 @@ typedef void (^OSNotificationOpenedBlock)(OSNotificationOpenedResult * _Nonnull
111114
+ (void)setPushSubscriptionId:(NSString *_Nullable)pushSubscriptionId;
112115

113116
+ (void)handleWillShowInForegroundForNotification:(OSNotification *_Nonnull)notification completion:(OSNotificationDisplayResponse _Nonnull)completion;
114-
+ (void)handleNotificationAction:(OSNotificationActionType)actionType actionID:(NSString* _Nonnull)actionID;
115-
117+
+ (void)handleNotificationActionWithUrl:(NSString* _Nullable)url actionID:(NSString* _Nonnull)actionID;
116118
+ (BOOL)clearBadgeCount:(BOOL)fromNotifOpened;
117119

118120
+ (BOOL)receiveRemoteNotification:(UIApplication* _Nonnull)application UserInfo:(NSDictionary* _Nonnull)userInfo completionHandler:(void (^_Nonnull)(UIBackgroundFetchResult))completionHandler;

iOS_SDK/OneSignalSDK/OneSignalNotifications/OSNotificationsManager.m

Lines changed: 59 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -35,14 +35,14 @@ of this software and associated documentation files (the "Software"), to deal
3535
#import "UNUserNotificationCenter+OneSignalNotifications.h"
3636
#import "UIApplicationDelegate+OneSignalNotifications.h"
3737

38-
@implementation OSNotificationOpenedResult
39-
@synthesize notification = _notification, action = _action;
38+
@implementation OSNotificationClickEvent
39+
@synthesize notification = _notification, result = _result;
4040

41-
- (id)initWithNotification:(OSNotification*)notification action:(OSNotificationAction*)action {
41+
- (id)initWithNotification:(OSNotification*)notification result:(OSNotificationClickResult*)result {
4242
self = [super init];
4343
if(self) {
4444
_notification = notification;
45-
_action = action;
45+
_result = result;
4646
}
4747
return self;
4848
}
@@ -63,25 +63,24 @@ - (NSDictionary *_Nonnull)jsonRepresentation {
6363
error:&jsonError];
6464

6565
NSMutableDictionary* obj = [NSMutableDictionary new];
66-
NSMutableDictionary* action = [NSMutableDictionary new];
67-
[action setObject:self.action.actionId forKeyedSubscript:@"actionID"];
68-
[obj setObject:action forKeyedSubscript:@"action"];
66+
NSMutableDictionary* result = [NSMutableDictionary new];
67+
[result setObject:self.result.actionId forKeyedSubscript:@"actionID"];
68+
[result setObject:self.result.url forKeyedSubscript:@"url"];
69+
[obj setObject:result forKeyedSubscript:@"result"];
6970
[obj setObject:notifDict forKeyedSubscript:@"notification"];
70-
if(self.action.type)
71-
[obj[@"action"] setObject:@(self.action.type) forKeyedSubscript: @"type"];
72-
71+
7372
return obj;
7473
}
7574

7675
@end
7776

78-
@implementation OSNotificationAction
79-
@synthesize type = _type, actionId = _actionId;
77+
@implementation OSNotificationClickResult
78+
@synthesize url = _url, actionId = _actionId;
8079

81-
-(id)initWithActionType:(OSNotificationActionType)type :(NSString*)actionID {
80+
-(id)initWithUrl:(NSString*)url :(NSString*)actionID {
8281
self = [super init];
8382
if(self) {
84-
_type = type;
83+
_url = url;
8584
_actionId = actionID;
8685
}
8786
return self;
@@ -149,8 +148,14 @@ + (void)setLifecycleListener:(id<OSNotificationLifecycleListener>)lifecycleListe
149148

150149
static int mSubscriptionStatus = -1;
151150

152-
static NSMutableArray<OSNotificationOpenedResult*> *_unprocessedOpenedNotifis;
153-
static OSNotificationOpenedBlock _notificationOpenedHandler;
151+
static NSMutableArray<OSNotificationClickEvent*> *_unprocessedClickEvents;
152+
static NSMutableArray<NSObject<OSNotificationClickListener> *> *_clickListeners;
153+
+ (NSMutableArray<NSObject<OSNotificationClickListener> *>*)clickListeners {
154+
if (!_clickListeners)
155+
_clickListeners = [NSMutableArray new];
156+
return _clickListeners;
157+
}
158+
154159
static NSDictionary* _lastMessageReceived;
155160
static NSString *_lastMessageID = @"";
156161
static NSString *_lastMessageIdFromAction;
@@ -255,7 +260,7 @@ + (void)resetLocals {
255260
_lastMessageReceived = nil;
256261
_lastMessageIdFromAction = nil;
257262
_lastMessageID = @"";
258-
_unprocessedOpenedNotifis = nil;
263+
_unprocessedClickEvents = nil;
259264
}
260265

261266
+ (void)setLastPermissionState:(OSPermissionStateInternal *)lastPermissionState {
@@ -693,7 +698,7 @@ + (void)handleNotificationOpened:(NSDictionary*)messageDict
693698
// [[OSSessionManager sharedSessionManager] onDirectInfluenceFromNotificationOpen:_appEntryState withNotificationId:messageId];
694699
// }
695700

696-
[self handleNotificationAction:actionType actionID:actionID];
701+
[self handleNotificationActionWithUrl:notification.launchURL actionID:actionID];
697702
}
698703

699704
+ (void)submitNotificationOpened:(NSString*)messageId {
@@ -793,28 +798,35 @@ + (BOOL)handleIAMPreview:(OSNotification *)notification {
793798
return NO;
794799
}
795800

796-
+ (void)handleNotificationAction:(OSNotificationActionType)actionType actionID:(NSString*)actionID {
801+
+ (void)handleNotificationActionWithUrl:(NSString*)url actionID:(NSString*)actionID {
797802
if (![OneSignalCoreHelper isOneSignalPayload:_lastMessageReceived])
798803
return;
799804

800-
OSNotificationAction *action = [[OSNotificationAction alloc] initWithActionType:actionType :actionID];
805+
OSNotificationClickResult *result = [[OSNotificationClickResult alloc] initWithUrl:url :actionID];
801806
OSNotification *notification = [OSNotification parseWithApns:_lastMessageReceived];
802-
OSNotificationOpenedResult *result = [[OSNotificationOpenedResult alloc] initWithNotification:notification action:action];
807+
OSNotificationClickEvent *event = [[OSNotificationClickEvent alloc] initWithNotification:notification result:result];
803808

804809
// Prevent duplicate calls to same action
805810
if ([notification.notificationId isEqualToString:_lastMessageIdFromAction])
806811
return;
807812
_lastMessageIdFromAction = notification.notificationId;
808813

809-
[OneSignalTrackFirebaseAnalytics trackOpenEvent:result];
810-
811-
if (!_notificationOpenedHandler) {
812-
[self addUnprocessedOpenedNotifi:result];
814+
[OneSignalTrackFirebaseAnalytics trackOpenEvent:event];
815+
816+
if (self.clickListeners.count == 0) {
817+
[self addUnprocessedClickEvent:event];
813818
return;
814819
}
815-
_notificationOpenedHandler(result);
820+
[self fireClickListenersForEvent:event];
816821
}
817822

823+
+ (void)fireClickListenersForEvent:(OSNotificationClickEvent*)event {
824+
for (NSObject<OSNotificationClickListener> *listener in self.clickListeners) {
825+
if ([listener respondsToSelector:@selector(onClickNotification:)]) {
826+
[listener onClickNotification:event];
827+
}
828+
}
829+
}
818830

819831
+ (void)lastMessageReceived:(NSDictionary*)message {
820832
_lastMessageReceived = message;
@@ -828,11 +840,15 @@ + (BOOL)shouldSuppressURL {
828840
return shouldSuppress ?: false;
829841
}
830842

831-
+ (void)setNotificationOpenedHandler:(OSNotificationOpenedBlock)block {
832-
_notificationOpenedHandler = block;
833-
[OneSignalLog onesignalLog:ONE_S_LL_VERBOSE message:@"Notification opened handler set successfully"];
834-
[self fireNotificationOpenedHandlerForUnprocessedEvents];
835-
843+
+ (void)addClickListener:(NSObject<OSNotificationClickListener>*)listener {
844+
[self.clickListeners addObject:listener];
845+
[OneSignalLog onesignalLog:ONE_S_LL_VERBOSE message:@"Notification click listener added successfully"];
846+
[self fireClickListenersForUnprocessedEvents];
847+
}
848+
849+
+ (void)removeClickListener:(NSObject<OSNotificationClickListener>*)listener {
850+
[self.clickListeners removeObject:listener];
851+
[OneSignalLog onesignalLog:ONE_S_LL_VERBOSE message:@"Notification click listener removed successfully"];
836852
}
837853

838854
+ (void)addForegroundLifecycleListener:(NSObject<OSNotificationLifecycleListener> *_Nullable)listener {
@@ -845,24 +861,24 @@ + (void)removeForegroundLifecycleListener:(NSObject<OSNotificationLifecycleListe
845861
[OneSignalLog onesignalLog:ONE_S_LL_VERBOSE message:@"ForegroundLifecycleListener removed successfully"];
846862
}
847863

848-
+ (NSMutableArray<OSNotificationOpenedResult*>*)getUnprocessedOpenedNotifis {
849-
if (!_unprocessedOpenedNotifis)
850-
_unprocessedOpenedNotifis = [NSMutableArray new];
851-
return _unprocessedOpenedNotifis;
864+
+ (NSMutableArray<OSNotificationClickEvent*>*)getUnprocessedClickEvents {
865+
if (!_unprocessedClickEvents)
866+
_unprocessedClickEvents = [NSMutableArray new];
867+
return _unprocessedClickEvents;
852868
}
853869

854-
+ (void)addUnprocessedOpenedNotifi:(OSNotificationOpenedResult*)result {
855-
[[self getUnprocessedOpenedNotifis] addObject:result];
870+
+ (void)addUnprocessedClickEvent:(OSNotificationClickEvent*)event {
871+
[[self getUnprocessedClickEvents] addObject:event];
856872
}
857873

858-
+ (void)fireNotificationOpenedHandlerForUnprocessedEvents {
859-
if (!_notificationOpenedHandler)
874+
+ (void)fireClickListenersForUnprocessedEvents {
875+
if (self.clickListeners.count == 0) {
860876
return;
861-
862-
for (OSNotificationOpenedResult* notification in [self getUnprocessedOpenedNotifis]) {
863-
_notificationOpenedHandler(notification);
864877
}
865-
_unprocessedOpenedNotifis = [NSMutableArray new];
878+
for (OSNotificationClickEvent* event in [self getUnprocessedClickEvents]) {
879+
[self fireClickListenersForEvent:event];
880+
}
881+
_unprocessedClickEvents = [NSMutableArray new];
866882
}
867883

868884
+ (BOOL)receiveRemoteNotification:(UIApplication*)application UserInfo:(NSDictionary*)userInfo completionHandler:(void (^)(UIBackgroundFetchResult))completionHandler {

iOS_SDK/OneSignalSDK/Source/OneSignalSwiftInterface.swift

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,14 @@ public extension OSNotifications {
124124
static func removePermissionObserver(_ observer: OSNotificationPermissionObserver) {
125125
return __remove(observer)
126126
}
127+
128+
static func addClickListener(_ listener: OSNotificationClickListener) {
129+
return __add(listener)
130+
}
131+
132+
static func removeClickListener(_ listener: OSNotificationClickListener) {
133+
return __remove(listener)
134+
}
127135
}
128136

129137
public extension OSLocation {

0 commit comments

Comments
 (0)