Skip to content

Commit b5b9a2e

Browse files
Merge pull request #448 from OneSignal/feature/explicitBindings
[FIX] il2cpp exception on NotificationPermissionChanged
2 parents b7a0b8a + 6892c69 commit b5b9a2e

File tree

6 files changed

+109
-75
lines changed

6 files changed

+109
-75
lines changed

OneSignalExample/Assets/OneSignal/CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,12 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
77
## [Unreleased]
88
### Fixed
99
- iOS build post processor will determine extension's imported OneSignalXCFramework from the package's dependencies xml
10+
- iOS callbacks for the `NotificationPermissionChanged` event will no longer cause an il2cpp exception
1011
### Changed
1112
- Added AndroidManifest with location permissions to the example app to display `PromptLocation`
1213
- `InstallEdm4uStep` now imports version [1.2.169](https://github.com/googlesamples/unity-jar-resolver/releases/tag/v1.2.169) of [EDM4U](https://github.com/googlesamples/unity-jar-resolver)
1314
- Log an error in the example app when `RequiresPrivacyConsent` is attempted to be set to false from true
15+
- Internal state mappings on iOS now rely on class defined objects over dynamic Dictionary types
1416

1517
## [3.0.0-beta.5]
1618
### Changed

com.onesignal.unity.ios/Runtime/OneSignalIOS.Callbacks.cs

Lines changed: 15 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -48,20 +48,24 @@ private static (Later<TResult> proxy, int hashCode) _setupProxy<TResult>() {
4848
WaitingProxies[hashCode] = proxy;
4949
return (proxy, hashCode);
5050
}
51-
52-
[AOT.MonoPInvokeCallback(typeof(BooleanResponseDelegate))]
53-
private static void BooleanCallbackProxy(int hashCode, bool response) {
54-
if (WaitingProxies[hashCode] is Later<bool> later)
51+
52+
private static void ResolveCallbackProxy<TResponse>(int hashCode, TResponse response) {
53+
if (!WaitingProxies.ContainsKey(hashCode))
54+
return;
55+
56+
if (WaitingProxies[hashCode] is Later<TResponse> later)
5557
later.Complete(response);
58+
5659
WaitingProxies.Remove(hashCode);
5760
}
5861

62+
[AOT.MonoPInvokeCallback(typeof(BooleanResponseDelegate))]
63+
private static void BooleanCallbackProxy(int hashCode, bool response)
64+
=> ResolveCallbackProxy(hashCode, response);
65+
5966
[AOT.MonoPInvokeCallback(typeof(StringResponseDelegate))]
60-
private static void StringCallbackProxy(int hashCode, string response) {
61-
if (WaitingProxies[hashCode] is Later<string> later)
62-
later.Complete(response);
63-
WaitingProxies.Remove(hashCode);
64-
}
67+
private static void StringCallbackProxy(int hashCode, string response)
68+
=> ResolveCallbackProxy(hashCode, response);
6569

6670
/*
6771
* Global Callbacks
@@ -134,19 +138,8 @@ private static void _onInAppMessageClicked(string response)
134138

135139
[AOT.MonoPInvokeCallback(typeof(StateListenerDelegate))]
136140
private static void _onPermissionStateChanged(string current, string previous) {
137-
if (!(Json.Deserialize(current) is Dictionary<string, object> currState)) {
138-
SDKDebug.Error("Could not deserialize current permission state");
139-
return;
140-
}
141-
142-
if (!(Json.Deserialize(previous) is Dictionary<string, object> prevState)) {
143-
SDKDebug.Error("Could not deserialize previous permission state");
144-
return;
145-
}
146-
147-
var curr = (NotificationPermission)currState["status"];
148-
var prev = (NotificationPermission)prevState["status"];
149-
141+
var curr = JsonUtility.FromJson<NotificationPermissionState>(current);
142+
var prev = JsonUtility.FromJson<NotificationPermissionState>(previous);
150143
_instance.NotificationPermissionChanged?.Invoke(curr, prev);
151144
}
152145

com.onesignal.unity.ios/Runtime/OneSignalIOS.Interface.cs

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,6 @@
2828
using System.Runtime.InteropServices;
2929

3030
namespace OneSignalSDK {
31-
/// <summary>
32-
///
33-
/// </summary>
3431
public sealed partial class OneSignalIOS : OneSignal {
3532
/*
3633
* Global callbacks
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
/*
2+
* Modified MIT License
3+
*
4+
* Copyright 2022 OneSignal
5+
*
6+
* Permission is hereby granted, free of charge, to any person obtaining a copy
7+
* of this software and associated documentation files (the "Software"), to deal
8+
* in the Software without restriction, including without limitation the rights
9+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10+
* copies of the Software, and to permit persons to whom the Software is
11+
* furnished to do so, subject to the following conditions:
12+
*
13+
* 1. The above copyright notice and this permission notice shall be included in
14+
* all copies or substantial portions of the Software.
15+
*
16+
* 2. All copies of substantial portions of the Software may only be used in connection
17+
* with services provided by OneSignal.
18+
*
19+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
22+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
24+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
25+
* THE SOFTWARE.
26+
*/
27+
28+
using System;
29+
30+
namespace OneSignalSDK {
31+
public sealed partial class OneSignalIOS : OneSignal {
32+
[Serializable] private sealed class DeviceState {
33+
public long notificationPermissionStatus;
34+
35+
public string userId;
36+
public string pushToken;
37+
public bool isSubscribed;
38+
public bool isPushDisabled;
39+
40+
public string emailUserId;
41+
public string emailAddress;
42+
public bool isEmailSubscribed;
43+
44+
public string smsUserId;
45+
public string smsNumber;
46+
public bool isSMSSubscribed;
47+
48+
public static implicit operator PushSubscriptionState(DeviceState source)
49+
=> new PushSubscriptionState {
50+
userId = source.userId,
51+
pushToken = source.pushToken,
52+
isSubscribed = source.isSubscribed,
53+
isPushDisabled = source.isPushDisabled,
54+
};
55+
56+
public static implicit operator EmailSubscriptionState(DeviceState source)
57+
=> new EmailSubscriptionState {
58+
emailUserId = source.emailUserId,
59+
emailAddress = source.emailAddress,
60+
isSubscribed = source.isEmailSubscribed,
61+
};
62+
63+
public static implicit operator SMSSubscriptionState(DeviceState source)
64+
=> new SMSSubscriptionState {
65+
smsUserId = source.smsUserId,
66+
smsNumber = source.smsNumber,
67+
isSubscribed = source.isSMSSubscribed,
68+
};
69+
}
70+
71+
[Serializable] private sealed class NotificationPermissionState {
72+
public long status;
73+
public bool provisional;
74+
public bool hasPrompted;
75+
76+
public static implicit operator NotificationPermission(NotificationPermissionState source)
77+
=> (NotificationPermission)source.status;
78+
}
79+
}
80+
}

com.onesignal.unity.ios/Runtime/OneSignalIOS.Mappings.cs.meta

Lines changed: 3 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

com.onesignal.unity.ios/Runtime/OneSignalIOS.cs

Lines changed: 9 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
using System.Collections.Generic;
2929
using System.Linq;
3030
using System.Threading.Tasks;
31+
using UnityEngine;
3132

3233
namespace OneSignalSDK {
3334
public sealed partial class OneSignalIOS : OneSignal {
@@ -45,61 +46,19 @@ public sealed partial class OneSignalIOS : OneSignal {
4546

4647
public override NotificationPermission NotificationPermission {
4748
get {
48-
if (Json.Deserialize(_getDeviceState()) is Dictionary<string, object> deviceState) {
49-
if (deviceState["notificationPermissionStatus"] is long status)
50-
return (NotificationPermission) status;
51-
}
52-
53-
SDKDebug.Error("Could not deserialize device state for permissions");
54-
return NotificationPermission.NotDetermined;
49+
var deviceState = JsonUtility.FromJson<DeviceState>(_getDeviceState());
50+
return (NotificationPermission)deviceState.notificationPermissionStatus;
5551
}
5652
}
5753

58-
public override PushSubscriptionState PushSubscriptionState {
59-
get {
60-
if (Json.Deserialize(_getDeviceState()) is Dictionary<string, object> deviceState) {
61-
return new PushSubscriptionState {
62-
userId = deviceState["userId"] as string,
63-
pushToken = deviceState["pushToken"] as string,
64-
isSubscribed = (bool) deviceState["isSubscribed"],
65-
isPushDisabled = (bool) deviceState["isPushDisabled"],
66-
};
67-
}
68-
69-
SDKDebug.Error("Could not deserialize device state for push");
70-
return null;
71-
}
72-
}
54+
public override PushSubscriptionState PushSubscriptionState
55+
=> JsonUtility.FromJson<DeviceState>(_getDeviceState());
7356

74-
public override EmailSubscriptionState EmailSubscriptionState {
75-
get {
76-
if (Json.Deserialize(_getDeviceState()) is Dictionary<string, object> deviceState) {
77-
return new EmailSubscriptionState {
78-
emailUserId = deviceState["emailUserId"] as string,
79-
emailAddress = deviceState["emailAddress"] as string,
80-
isSubscribed = (bool) deviceState["isEmailSubscribed"],
81-
};
82-
}
83-
84-
SDKDebug.Error("Could not deserialize device state for email");
85-
return null;
86-
}
87-
}
57+
public override EmailSubscriptionState EmailSubscriptionState
58+
=> JsonUtility.FromJson<DeviceState>(_getDeviceState());
8859

89-
public override SMSSubscriptionState SMSSubscriptionState {
90-
get {
91-
if (Json.Deserialize(_getDeviceState()) is Dictionary<string, object> deviceState) {
92-
return new SMSSubscriptionState {
93-
smsUserId = deviceState["smsUserId"] as string,
94-
smsNumber = deviceState["smsNumber"] as string,
95-
isSubscribed = (bool) deviceState["isSMSSubscribed"],
96-
};
97-
}
98-
99-
SDKDebug.Error("Could not deserialize device state for sms");
100-
return null;
101-
}
102-
}
60+
public override SMSSubscriptionState SMSSubscriptionState
61+
=> JsonUtility.FromJson<DeviceState>(_getDeviceState());
10362

10463
public override LogLevel LogLevel {
10564
get => _logLevel;

0 commit comments

Comments
 (0)