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 Feb 27, 2024
2 parents 2748008 + 9b0ac69 commit 16ef5bf
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 21 deletions.
2 changes: 2 additions & 0 deletions OneSignalExample/Assets/OneSignal/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
### Changed
- `InstallEdm4uStep` now imports version [1.2.177](https://github.com/googlesamples/unity-jar-resolver/releases/tag/v1.2.177) of [EDM4U](https://github.com/googlesamples/unity-jar-resolver)
- Updated included Android SDK to [5.0.1](https://github.com/OneSignal/OneSignal-Android-SDK/releases/tag/5.0.1)
- Updated included iOS SDK to [5.0.2](https://github.com/OneSignal/OneSignal-iOS-SDK/releases/tag/5.0.2)
### Fixed
- Sending VSAttribution data from the editor
- iOS notifications clicked event firing if the app was cold started from clicking a notification

## [5.0.2]
### Fixed
Expand Down
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 16ef5bf

Please sign in to comment.