Skip to content

Commit

Permalink
Merge pull request #1258 from OneSignal/5.0.0/notification_foreground…
Browse files Browse the repository at this point in the history
…_listener_api

[5.0.0] Notification Foreground Listener - API update
  • Loading branch information
emawby authored and nan-li committed Oct 30, 2023
2 parents 7ac7ae2 + 976f0cb commit 969b324
Show file tree
Hide file tree
Showing 38 changed files with 528 additions and 344 deletions.
2 changes: 1 addition & 1 deletion iOS_SDK/OneSignalDevApp/OneSignalDevApp/AppDelegate.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
#import <UIKit/UIKit.h>
#import <OneSignalFramework/OneSignalFramework.h>

@interface AppDelegate : UIResponder <UIApplicationDelegate, OSNotificationPermissionObserver, OSInAppMessageLifecycleListener, OSPushSubscriptionObserver>
@interface AppDelegate : UIResponder <UIApplicationDelegate, OSNotificationPermissionObserver, OSInAppMessageLifecycleListener, OSPushSubscriptionObserver, OSNotificationLifecycleListener, OSInAppMessageClickListener, OSNotificationClickListener>

@property (strong, nonatomic) UIWindow *window;

Expand Down
62 changes: 25 additions & 37 deletions iOS_SDK/OneSignalDevApp/OneSignalDevApp/AppDelegate.m
Original file line number Diff line number Diff line change
Expand Up @@ -57,44 +57,21 @@ - (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
};
id notificationReceiverBlock = ^(OSNotification *notif, OSNotificationDisplayResponse completion) {
NSLog(@"Will Receive Notification - %@", notif.notificationId);
completion(notif);
};

// Example block for IAM action click handler
id inAppMessagingActionClickBlock = ^(OSInAppMessageAction *action) {
NSString *message = [NSString stringWithFormat:@"Click Action Occurred: %@", [action jsonRepresentation]];
[OneSignalLog onesignalLog:ONE_S_LL_DEBUG message:message];
};

// Example setter for IAM action click handler using OneSignal public method
[OneSignal.InAppMessages setClickHandler:inAppMessagingActionClickBlock];

// OneSignal Init with app id and lauch options
[OneSignal setLaunchURLsInApp:YES];
[OneSignal setProvidesNotificationSettingsView:NO];

[OneSignal.InAppMessages addLifecycleListener:self];
[OneSignal.InAppMessages paused:true];

[OneSignal.Notifications setNotificationWillShowInForegroundHandler:notificationReceiverBlock];
[OneSignal.Notifications setNotificationOpenedHandler:openNotificationHandler];

[OneSignal.Notifications addForegroundLifecycleListener:self];
[OneSignal.Notifications addClickListener:self];
[OneSignal.User.pushSubscription addObserver:self];
NSLog(@"OneSignal Demo App push subscription observer added");

[OneSignal.Notifications addPermissionObserver:self];

[OneSignal.InAppMessages addClickListener:self];

NSLog(@"UNUserNotificationCenter.delegate: %@", UNUserNotificationCenter.currentNotificationCenter.delegate);

return YES;
Expand All @@ -121,36 +98,47 @@ - (void)onNotificationPermissionDidChange:(BOOL)permission {
NSLog(@"Dev App onNotificationPermissionDidChange: %d", permission);
}

- (void)onOSPushSubscriptionChangedWithStateChanges:(OSPushSubscriptionStateChanges *)stateChanges {
NSLog(@"Dev App onOSPushSubscriptionChangedWithStateChanges: %@", stateChanges);
- (void)onPushSubscriptionDidChangeWithState:(OSPushSubscriptionChangedState *)state {
NSLog(@"Dev App onPushSubscriptionDidChange: %@", state);
ViewController* mainController = (ViewController*) self.window.rootViewController;
mainController.subscriptionSegmentedControl.selectedSegmentIndex = (NSInteger) stateChanges.to.optedIn;
mainController.subscriptionSegmentedControl.selectedSegmentIndex = (NSInteger) state.current.optedIn;
}

- (void)onClickNotification:(OSNotificationClickEvent * _Nonnull)event {
NSLog(@"Dev App onClickNotification with event %@", [event jsonRepresentation]);
}

#pragma mark OSInAppMessageDelegate

- (void)handleMessageAction:(OSInAppMessageAction *)action {
NSLog(@"OSInAppMessageDelegate: handling message action: %@",action);
return;
- (void)onClickInAppMessage:(OSInAppMessageClickEvent * _Nonnull)event {
NSLog(@"Dev App onClickInAppMessage event: %@", [event jsonRepresentation]);
}

- (void)onWillDisplayNotification:(OSNotificationWillDisplayEvent *)event {
NSLog(@"Dev App OSNotificationWillDisplayEvent with event: %@",event);
[event preventDefault];
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, 5 * NSEC_PER_SEC), dispatch_get_main_queue(), ^{
[event.notification display];
});
}

- (void)onWillDisplayInAppMessage:(OSInAppMessageWillDisplayEvent *)event {
NSLog(@"OSInAppMessageDelegate: onWillDisplay Message: %@",event.message);
NSLog(@"Dev App OSInAppMessageLifecycleListener: onWillDisplay Message: %@",event.message);
return;
}

- (void)onDidDisplayInAppMessage:(OSInAppMessageDidDisplayEvent *)event {
NSLog(@"OSInAppMessageDelegate: onDidDisplay Message: %@",event.message);
NSLog(@"Dev App OSInAppMessageLifecycleListener: onDidDisplay Message: %@",event.message);
return;
}

- (void)onWillDismissInAppMessage:(OSInAppMessageWillDismissEvent *)event {
NSLog(@"OSInAppMessageDelegate: onWillDismiss Message: %@",event.message);
NSLog(@"Dev App OSInAppMessageLifecycleListener: onWillDismiss Message: %@",event.message);
return;
}

- (void)onDidDismissInAppMessage:(OSInAppMessageDidDismissEvent *)event {
NSLog(@"OSInAppMessageDelegate: onDidDismiss Message: %@",event.message);
NSLog(@"Dev App OSInAppMessageLifecycleListener: onDidDismiss Message: %@",event.message);
return;
}

Expand Down
2 changes: 1 addition & 1 deletion iOS_SDK/OneSignalDevApp/OneSignalDevApp/ViewController.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
#import <UIKit/UIKit.h>
#import <OneSignalFramework/OneSignalFramework.h>

@interface ViewController : UIViewController <OSInAppMessageDelegate>
@interface ViewController : UIViewController

@property (weak, nonatomic) IBOutlet UIActivityIndicatorView *activityIndicatorView;
@property (weak, nonatomic) IBOutlet UISegmentedControl *consentSegmentedControl;
Expand Down
4 changes: 0 additions & 4 deletions iOS_SDK/OneSignalDevApp/OneSignalDevApp/ViewController.m
Original file line number Diff line number Diff line change
Expand Up @@ -190,10 +190,6 @@ - (IBAction)inAppMessagingSegmentedControlValueChanged:(UISegmentedControl *)sen
[OneSignal.InAppMessages paused:(bool) !sender.selectedSegmentIndex];
}

- (void)handleMessageAction:(NSString *)actionId {
NSLog(@"View controller did get action: %@", actionId);
}

- (IBAction)loginExternalUserId:(UIButton *)sender {
NSString* externalUserId = self.externalUserIdTextField.text;
NSLog(@"Dev App: Logging in to external user ID %@", externalUserId);
Expand Down
22 changes: 1 addition & 21 deletions iOS_SDK/OneSignalDevApp/OneSignalDevAppClip/AppDelegate.m
Original file line number Diff line number Diff line change
Expand Up @@ -54,24 +54,7 @@ - (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);
};
id notificationReceiverBlock = ^(OSNotification *notif, OSNotificationDisplayResponse completion) {
NSLog(@"Will Receive Notification - %@", notif.notificationId);
completion(notif);
};

// Example block for IAM action click handler
id inAppMessagingActionClickBlock = ^(OSInAppMessageAction *action) {
NSString *message = [NSString stringWithFormat:@"Click Action Occurred: %@", [action jsonRepresentation]];
[OneSignalLog onesignalLog:ONE_S_LL_DEBUG message:message];
};

// Example setter for IAM action click handler using OneSignal public method
[OneSignal.InAppMessages setClickHandler:inAppMessagingActionClickBlock];


// OneSignal Init with app id and lauch options
[OneSignal setLaunchURLsInApp:YES];
[OneSignal setProvidesNotificationSettingsView:NO];
Expand All @@ -87,9 +70,6 @@ - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(

[OneSignal.InAppMessages paused:false];

[OneSignal.Notifications setNotificationWillShowInForegroundHandler:notificationReceiverBlock];
[OneSignal.Notifications setNotificationOpenedHandler:openNotificationHandler];

NSLog(@"UNUserNotificationCenter.delegate: %@", UNUserNotificationCenter.currentNotificationCenter.delegate);

return YES;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
#import <UIKit/UIKit.h>
#import <OneSignalFramework/OneSignalFramework.h>

@interface ViewController : UIViewController <OSInAppMessageDelegate>
@interface ViewController : UIViewController

@property (weak, nonatomic) IBOutlet UIActivityIndicatorView *activityIndicatorView;
@property (weak, nonatomic) IBOutlet UISegmentedControl *consentSegmentedControl;
Expand Down
4 changes: 0 additions & 4 deletions iOS_SDK/OneSignalDevApp/OneSignalDevAppClip/ViewController.m
Original file line number Diff line number Diff line change
Expand Up @@ -144,10 +144,6 @@ - (IBAction)inAppMessagingSegmentedControlValueChanged:(UISegmentedControl *)sen
[OneSignal.InAppMessages paused:(bool) !sender.selectedSegmentIndex];
}

- (void)handleMessageAction:(NSString *)actionId {
NSLog(@"View controller did get action: %@", actionId);
}

- (IBAction)loginExternalUserId:(UIButton *)sender {
NSLog(@"setExternalUserId is no longer supported. Please use login or addAlias.");
// TODO: Update
Expand Down
Loading

0 comments on commit 969b324

Please sign in to comment.