Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Fix] iOS notifications clicked event to fire on cold start #641

Merged
merged 6 commits into from
Sep 26, 2023
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 to fire when app is opened from clicking on a notification
shepherd-l marked this conversation as resolved.
Show resolved Hide resolved

## [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
35 changes: 17 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,28 @@ 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;

private bool _clickDelegate;
shepherd-l marked this conversation as resolved.
Show resolved Hide resolved
private EventHandler<NotificationClickEventArgs> _clicked;
public event EventHandler<NotificationClickEventArgs> Clicked {
add {
// only add one click listener
if (!_clickDelegate) {
_clicked += value;
shepherd-l marked this conversation as resolved.
Show resolved Hide resolved
_clickDelegate = true;
_notificationsSetClickCallback(_onClicked);
}
}
remove { _clicked -= value; }
}

private static iOSNotificationsManager _instance;

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

[AOT.MonoPInvokeCallback(typeof(PermissionListenerDelegate))]
Expand Down Expand Up @@ -133,23 +145,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;
jkasten2 marked this conversation as resolved.
Show resolved Hide resolved
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
Loading