Skip to content

Commit 78d77cf

Browse files
authored
Merge pull request #104 from OneSignal/gdpr_privacy
Gdpr privacy
2 parents 1f695fd + b73df13 commit 78d77cf

File tree

9 files changed

+117
-12
lines changed

9 files changed

+117
-12
lines changed

OneSignalExample/Assets/OneSignal/Example/GameControllerExample.cs

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -36,14 +36,21 @@ public class GameControllerExample : MonoBehaviour {
3636
private static string extraMessage;
3737
public string email = "Email Address";
3838

39+
private static bool requiresUserPrivacyConsent = false;
40+
3941
void Start () {
4042
extraMessage = null;
4143

4244
// Enable line below to debug issues with setuping OneSignal. (logLevel, visualLogLevel)
4345
OneSignal.SetLogLevel(OneSignal.LOG_LEVEL.VERBOSE, OneSignal.LOG_LEVEL.NONE);
4446

47+
// If you set to true, the user will have to provide consent
48+
// using OneSignal.UserDidProvideConsent(true) before the
49+
// SDK will initialize
50+
OneSignal.SetRequiresUserPrivacyConsent(requiresUserPrivacyConsent);
51+
4552
// The only required method you need to call to setup OneSignal to receive push notifications.
46-
// Call before using any other methods on OneSignal.
53+
// Call before using any other methods on OneSignal (except setLogLevel or SetRequiredUserPrivacyConsent)
4754
// Should only be called once when your app is loaded.
4855
// OneSignal.Init(OneSignal_AppId);
4956
OneSignal.StartInit("b2f7f966-d8cc-11e4-bed1-df8f05be55ba")
@@ -58,8 +65,6 @@ void Start () {
5865
OneSignal.emailSubscriptionObserver += OneSignal_emailSubscriptionObserver;
5966

6067
var pushState = OneSignal.GetPermissionSubscriptionState();
61-
Debug.Log("pushState.subscriptionStatus.subscribed : " + pushState.subscriptionStatus.subscribed);
62-
Debug.Log("pushState.subscriptionStatus.userId : " + pushState.subscriptionStatus.userId);
6368
}
6469

6570
private void OneSignal_subscriptionObserver(OSSubscriptionStateChanges stateChanges) {
@@ -137,7 +142,7 @@ void OnGUI () {
137142
float itemWidth = Screen.width - 120.0f;
138143
float boxWidth = Screen.width - 20.0f;
139144
float boxOriginY = 120.0f;
140-
float boxHeight = 630.0f;
145+
float boxHeight = requiresUserPrivacyConsent ? 720.0f : 630.0f;
141146
float itemStartY = 200.0f;
142147
float itemHeightOffset = 90.0f;
143148
float itemHeight = 60.0f;
@@ -225,6 +230,16 @@ void OnGUI () {
225230
});
226231
}
227232

233+
if (requiresUserPrivacyConsent) {
234+
count++;
235+
236+
if (GUI.Button (new Rect (itemOriginX, itemStartY + (count * itemHeightOffset), itemWidth, itemHeight), (OneSignal.UserProvidedConsent() ? "Revoke Privacy Consent" : "Provide Privacy Consent"), customTextSize)) {
237+
extraMessage = "Providing user privacy consent";
238+
239+
OneSignal.UserDidProvideConsent(!OneSignal.UserProvidedConsent());
240+
}
241+
}
242+
228243
if (extraMessage != null) {
229244
guiBoxStyle.alignment = TextAnchor.UpperLeft;
230245
guiBoxStyle.wordWrap = true;
Binary file not shown.

OneSignalExample/Assets/OneSignal/Platforms/iOS/OneSignal.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -340,6 +340,11 @@ typedef NS_ENUM(NSUInteger, ONE_S_LOG_LEVEL) {
340340
+ (id)initWithLaunchOptions:(NSDictionary*)launchOptions appId:(NSString*)appId handleNotificationAction:(OSHandleNotificationActionBlock)actionCallback settings:(NSDictionary*)settings;
341341
+ (id)initWithLaunchOptions:(NSDictionary*)launchOptions appId:(NSString*)appId handleNotificationReceived:(OSHandleNotificationReceivedBlock)receivedCallback handleNotificationAction:(OSHandleNotificationActionBlock)actionCallback settings:(NSDictionary*)settings;
342342

343+
// - Privacy
344+
+ (void)consentGranted:(BOOL)granted;
345+
+ (BOOL)requiresUserPrivacyConsent; // tells your application if privacy consent is still needed from the current user
346+
+ (void)setRequiresUserPrivacyConsent:(BOOL)required; //used by wrapper SDK's to require user privacy consent
347+
343348
@property (class) OSNotificationDisplayType inFocusDisplayType;
344349

345350
+ (NSString*)app_id;

OneSignalExample/Assets/OneSignal/Platforms/iOS/OneSignalUnityRuntime.m

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,9 @@ void initOneSignalObject(NSDictionary* launchOptions, const char* appId, int dis
165165

166166
}
167167

168-
void _init(const char* listenerName, const char* appId, BOOL autoPrompt, BOOL inAppLaunchURL, int displayOption, int logLevel, int visualLogLevel) {
168+
void _init(const char* listenerName, const char* appId, BOOL autoPrompt, BOOL inAppLaunchURL, int displayOption, int logLevel, int visualLogLevel, bool requiresUserPrivacyConsent) {
169+
[OneSignal setRequiresUserPrivacyConsent:requiresUserPrivacyConsent];
170+
169171
[OneSignal setLogLevel:logLevel visualLevel: visualLogLevel];
170172

171173
unsigned long len = strlen(listenerName);
@@ -329,6 +331,18 @@ void _logoutEmail() {
329331
} withFailure:^(NSError *error) {
330332
UnitySendMessage(unityListener, "onLogoutEmailFailure", [[OneSignal parseNSErrorAsJsonString:error] UTF8String]);
331333
}];
334+
}
335+
336+
void _userDidProvideConsent(bool consent) {
337+
[OneSignal consentGranted:consent];
338+
}
339+
340+
bool _userProvidedConsent() {
341+
return ![OneSignal requiresUserPrivacyConsent];
342+
}
343+
344+
void _setRequiresUserPrivacyConsent(bool required) {
345+
[OneSignal setRequiresUserPrivacyConsent:required];
332346
}
333347

334348
@end
Binary file not shown.

OneSignalExample/Assets/OneSignal/src/OneSignal.cs

Lines changed: 35 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -303,6 +303,15 @@ public void EndInit() {
303303
OneSignal.Init();
304304
}
305305

306+
public UnityBuilder SetRequiresUserPrivacyConsent(bool required) {
307+
System.Diagnostics.Debug.WriteLine("Did call setRequiresUserPrivacyConsent in OneSignal.cs");
308+
#if ONESIGNAL_PLATFORM
309+
OneSignal.requiresUserConsent = true;
310+
#endif
311+
312+
return this;
313+
}
314+
306315
}
307316
internal static UnityBuilder builder = null;
308317

@@ -314,6 +323,8 @@ public void EndInit() {
314323
private static LOG_LEVEL logLevel = LOG_LEVEL.INFO, visualLogLevel = LOG_LEVEL.NONE;
315324
#endif
316325

326+
internal static bool requiresUserConsent = false;
327+
317328
internal static OnPostNotificationSuccess postNotificationSuccessDelegate = null;
318329
internal static OnPostNotificationFailure postNotificationFailureDelegate = null;
319330

@@ -374,7 +385,7 @@ private static void initOneSignalPlatform() {
374385

375386
#if ONESIGNAL_PLATFORM && UNITY_ANDROID
376387
private static void initAndroid() {
377-
oneSignalPlatform = new OneSignalAndroid(gameObjectName, builder.googleProjectNumber, builder.appID, inFocusDisplayType, logLevel, visualLogLevel);
388+
oneSignalPlatform = new OneSignalAndroid(gameObjectName, builder.googleProjectNumber, builder.appID, inFocusDisplayType, logLevel, visualLogLevel, requiresUserConsent);
378389
}
379390
#endif
380391

@@ -388,7 +399,7 @@ private static void initIOS() {
388399
if (builder.iOSSettings.ContainsKey(kOSSettingsInAppLaunchURL))
389400
inAppLaunchURL = builder.iOSSettings[kOSSettingsInAppLaunchURL];
390401
}
391-
oneSignalPlatform = new OneSignalIOS(gameObjectName, builder.appID, autoPrompt, inAppLaunchURL, inFocusDisplayType, logLevel, visualLogLevel);
402+
oneSignalPlatform = new OneSignalIOS(gameObjectName, builder.appID, autoPrompt, inAppLaunchURL, inFocusDisplayType, logLevel, visualLogLevel, requiresUserConsent);
392403
}
393404
#endif
394405

@@ -598,6 +609,28 @@ public static OSPermissionSubscriptionState GetPermissionSubscriptionState() {
598609
#endif
599610
}
600611

612+
613+
614+
public static void UserDidProvideConsent(bool consent) {
615+
#if ONESIGNAL_PLATFORM
616+
oneSignalPlatform.UserDidProvideConsent(consent);
617+
#endif
618+
}
619+
620+
public static bool UserProvidedConsent() {
621+
#if ONESIGNAL_PLATFORM
622+
return oneSignalPlatform.UserProvidedConsent();
623+
#else
624+
return true;
625+
#endif
626+
}
627+
628+
public static void SetRequiresUserPrivacyConsent(bool required) {
629+
#if ONESIGNAL_PLATFORM
630+
OneSignal.requiresUserConsent = required;
631+
#endif
632+
}
633+
601634
/*** protected and private methods ****/
602635
#if ONESIGNAL_PLATFORM
603636

OneSignalExample/Assets/OneSignal/src/OneSignalAndroid.cs

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,8 @@
3434
public class OneSignalAndroid : OneSignalPlatform {
3535
private static AndroidJavaObject mOneSignal = null;
3636

37-
public OneSignalAndroid(string gameObjectName, string googleProjectNumber, string appId, OneSignal.OSInFocusDisplayOption displayOption, OneSignal.LOG_LEVEL logLevel, OneSignal.LOG_LEVEL visualLevel) {
38-
mOneSignal = new AndroidJavaObject("com.onesignal.OneSignalUnityProxy", gameObjectName, googleProjectNumber, appId, (int)logLevel, (int)visualLevel);
37+
public OneSignalAndroid(string gameObjectName, string googleProjectNumber, string appId, OneSignal.OSInFocusDisplayOption displayOption, OneSignal.LOG_LEVEL logLevel, OneSignal.LOG_LEVEL visualLevel, bool requiresUserConsent) {
38+
mOneSignal = new AndroidJavaObject("com.onesignal.OneSignalUnityProxy", gameObjectName, googleProjectNumber, appId, (int)logLevel, (int)visualLevel, requiresUserConsent);
3939
SetInFocusDisplaying(displayOption);
4040
}
4141

@@ -127,6 +127,18 @@ public void removeEmailSubscriptionObserver() {
127127
mOneSignal.Call("removeEmailSubscriptionObserver");
128128
}
129129

130+
public void UserDidProvideConsent(bool consent) {
131+
mOneSignal.Call("provideUserConsent", consent);
132+
}
133+
134+
public bool UserProvidedConsent() {
135+
return mOneSignal.Call<bool>("userProvidedPrivacyConsent");
136+
}
137+
138+
public void SetRequiresUserPrivacyConsent(bool required) {
139+
mOneSignal.Call("setRequiresUserPrivacyConsent", required);
140+
}
141+
130142
public OSPermissionSubscriptionState getPermissionSubscriptionState() {
131143
return OneSignalPlatformHelper.parsePermissionSubscriptionState(this, mOneSignal.Call<string>("getPermissionSubscriptionState"));
132144
}

OneSignalExample/Assets/OneSignal/src/OneSignalIOS.cs

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@
3535
public class OneSignalIOS : OneSignalPlatform {
3636

3737
[System.Runtime.InteropServices.DllImport("__Internal")]
38-
extern static public void _init(string listenerName, string appId, bool autoPrompt, bool inAppLaunchURLs, int displayOption, int logLevel, int visualLogLevel);
38+
extern static public void _init(string listenerName, string appId, bool autoPrompt, bool inAppLaunchURLs, int displayOption, int logLevel, int visualLogLevel, bool requiresUserPrivacyConsent);
3939

4040
[System.Runtime.InteropServices.DllImport("__Internal")]
4141
extern static public void _registerForPushNotifications();
@@ -109,9 +109,18 @@ public class OneSignalIOS : OneSignalPlatform {
109109
[System.Runtime.InteropServices.DllImport("__Internal")]
110110
extern static public void _setOneSignalLogLevel(int logLevel, int visualLogLevel);
111111

112+
[System.Runtime.InteropServices.DllImport("__Internal")]
113+
extern static public void _userDidProvideConsent(bool consent);
114+
115+
[System.Runtime.InteropServices.DllImport("__Internal")]
116+
extern static public bool _userProvidedConsent();
117+
118+
[System.Runtime.InteropServices.DllImport("__Internal")]
119+
extern static public void _setRequiresUserPrivacyConsent(bool required);
120+
112121

113-
public OneSignalIOS(string gameObjectName, string appId, bool autoPrompt, bool inAppLaunchURLs, OneSignal.OSInFocusDisplayOption displayOption, OneSignal.LOG_LEVEL logLevel, OneSignal.LOG_LEVEL visualLevel) {
114-
_init(gameObjectName, appId, autoPrompt, inAppLaunchURLs, (int)displayOption, (int)logLevel, (int)visualLevel);
122+
public OneSignalIOS(string gameObjectName, string appId, bool autoPrompt, bool inAppLaunchURLs, OneSignal.OSInFocusDisplayOption displayOption, OneSignal.LOG_LEVEL logLevel, OneSignal.LOG_LEVEL visualLevel, bool requiresUserPrivacyConsent) {
123+
_init(gameObjectName, appId, autoPrompt, inAppLaunchURLs, (int)displayOption, (int)logLevel, (int)visualLevel, requiresUserPrivacyConsent);
115124
}
116125

117126
public void RegisterForPushNotifications() {
@@ -207,6 +216,18 @@ public void LogoutEmail() {
207216
_logoutEmail();
208217
}
209218

219+
public void UserDidProvideConsent(bool consent) {
220+
_userDidProvideConsent(consent);
221+
}
222+
223+
public bool UserProvidedConsent() {
224+
return _userProvidedConsent();
225+
}
226+
227+
public void SetRequiresUserPrivacyConsent(bool required) {
228+
_setRequiresUserPrivacyConsent(required);
229+
}
230+
210231
public OSPermissionSubscriptionState getPermissionSubscriptionState() {
211232
return OneSignalPlatformHelper.parsePermissionSubscriptionState(this, _getPermissionSubscriptionState());
212233
}

OneSignalExample/Assets/OneSignal/src/OneSignalPlatform.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,11 @@ public interface OneSignalPlatform {
4949

5050
void SetInFocusDisplaying(OneSignal.OSInFocusDisplayOption display);
5151

52+
void UserDidProvideConsent(bool consent);
53+
bool UserProvidedConsent();
54+
void SetRequiresUserPrivacyConsent(bool required);
55+
56+
5257
void addPermissionObserver();
5358
void removePermissionObserver();
5459
void addSubscriptionObserver();

0 commit comments

Comments
 (0)