Skip to content

Commit

Permalink
Merge pull request #641 from OneSignal/fix/iOSNotificationClicked
Browse files Browse the repository at this point in the history
[Fix] iOS notifications clicked event on app open
  • Loading branch information
shepherd-l authored and jinliu9508 committed Mar 1, 2024
2 parents 42cc729 + d97b69d commit 14eeb5e
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 21 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<dependencies>
<iosPods>
<iosPod name="OneSignalXCFramework" version="5.0.1" addToAllTargets="true" />
<iosPod name="OneSignalXCFramework" version="5.0.2" addToAllTargets="true" />
</iosPods>
</dependencies>
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,6 @@ - (instancetype) init {
if (self = [super init]) {
[OneSignal.Notifications addPermissionObserver:self];
[OneSignal.Notifications addForegroundLifecycleListener:self];
[OneSignal.Notifications addClickListener:self];

_willDisplayEvents = [NSMutableDictionary new];
}
Expand Down Expand Up @@ -161,5 +160,7 @@ void _notificationsDisplay(const char* notifcationId) {

void _notificationsSetClickCallback(ClickListenerDelegate callback) {
[[OneSignalNotificationsObserver sharedNotificationsObserver] setClickDelegate:callback];

[OneSignal.Notifications addClickListener:[OneSignalNotificationsObserver sharedNotificationsObserver]];
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ - (void)setOneSignalUnityDelegate:(id <UIApplicationDelegate>)delegate {
- (BOOL)oneSignalApplication:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
[OneSignalWrapper setSdkType:@"unity"];
[OneSignalWrapper setSdkVersion:@"050002"];
[OneSignal setLaunchOptions:launchOptions];
[OneSignal initialize:nil withLaunchOptions:launchOptions];

if ([self respondsToSelector:@selector(oneSignalApplication:didFinishLaunchingWithOptions:)])
return [self oneSignalApplication:application didFinishLaunchingWithOptions:launchOptions];
Expand Down
37 changes: 19 additions & 18 deletions com.onesignal.unity.ios/Runtime/iOSNotificationsManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -48,15 +48,30 @@ internal sealed class iOSNotificationsManager : INotificationsManager {
[DllImport("__Internal")] private static extern void _notificationsWillDisplayEventPreventDefault(string notificationId);
[DllImport("__Internal")] private static extern void _notificationsSetClickCallback(ClickListenerDelegate callback);

public delegate void PermissionListenerDelegate(bool permission);
private delegate void PermissionListenerDelegate(bool permission);
private delegate void WillDisplayListenerDelegate(string notification);
private delegate void ClickListenerDelegate(string notification, string resultActionId, string resultUrl);
private delegate void BooleanResponseDelegate(int hashCode, bool response);

public event EventHandler<NotificationWillDisplayEventArgs> ForegroundWillDisplay;
public event EventHandler<NotificationClickEventArgs> Clicked;
public event EventHandler<NotificationPermissionChangedEventArgs> PermissionChanged;

// Only set the native listner once
private bool _clickNativeListenerSet;

private EventHandler<NotificationClickEventArgs> _clicked;
public event EventHandler<NotificationClickEventArgs> Clicked {
add {
_clicked += value;

if (!_clickNativeListenerSet) {
_clickNativeListenerSet = true;
_notificationsSetClickCallback(_onClicked);
}
}
remove { _clicked -= value; }
}

private static iOSNotificationsManager _instance;

public iOSNotificationsManager() {
Expand Down Expand Up @@ -88,7 +103,6 @@ public void ClearAllNotifications() {
public void Initialize() {
_notificationsAddPermissionObserver(_onPermissionStateChanged);
_notificationsSetForegroundWillDisplayCallback(_onForegroundWillDisplay);
_notificationsSetClickCallback(_onClicked);
}

[AOT.MonoPInvokeCallback(typeof(PermissionListenerDelegate))]
Expand Down Expand Up @@ -133,23 +147,10 @@ private static void _onClicked(string notification, string resultActionId, strin
_fillNotifFromObj(ref notif, Json.Deserialize(notification));

var result = new NotificationClickResult(resultActionId, resultUrl);

NotificationClickEventArgs args = new NotificationClickEventArgs(notif, result);

EventHandler<NotificationClickEventArgs> handler = _instance.Clicked;
if (handler != null)
{
if (OneSignalPlatform.DidInitialize)
UnityMainThreadDispatch.Post(state => handler(_instance, args));
else {
void invokeOpened(string appId) {
OneSignalPlatform.OnInitialize -= invokeOpened;
UnityMainThreadDispatch.Post(state => handler(_instance, args));
}

OneSignalPlatform.OnInitialize += invokeOpened;
}
}
EventHandler<NotificationClickEventArgs> handler = _instance._clicked;
UnityMainThreadDispatch.Post(state => handler(_instance, args));
}

private static void _fillNotifFromObj(ref iOSDisplayableNotification notif, object notifObj) {
Expand Down

0 comments on commit 14eeb5e

Please sign in to comment.