35
35
using OneSignalSDKNew . Notifications . Models ;
36
36
using OneSignalSDKNew . InAppMessages . Models ;
37
37
using OneSignalSDKNew . User . Models ;
38
+ using System . Collections . Generic ;
38
39
39
40
// ReSharper disable InconsistentNaming
40
41
// ReSharper disable CheckNamespace
@@ -73,6 +74,11 @@ public class OneSignalExampleBehaviour : MonoBehaviour {
73
74
/// </summary>
74
75
public bool launchURLsInApp ;
75
76
77
+ /// <summary>
78
+ ///
79
+ /// </summary>
80
+ public string language ;
81
+
76
82
/// <summary>
77
83
///
78
84
/// </summary>
@@ -112,13 +118,17 @@ private void Start() {
112
118
OneSignal . Default . Debug . LogLevel = LogLevel . Info ;
113
119
OneSignal . Default . Debug . AlertLevel = LogLevel . Fatal ;
114
120
121
+ _log ( $ "Initializing with appId <b>{ appId } </b>") ;
122
+ OneSignal . Default . Initialize ( appId ) ;
123
+
115
124
// Setting RequiresPrivacyConsent to true will prevent the OneSignalSDK from operating until
116
125
// PrivacyConsent is also set to true
117
126
OneSignal . Default . RequiresPrivacyConsent = requiresUserPrivacyConsent ;
118
127
119
128
// Setup the below to listen for and respond to events from notifications
120
129
OneSignal . Default . Notifications . Clicked += _notificationOnClick ;
121
130
OneSignal . Default . Notifications . WillShow += _notificationOnDisplay ;
131
+ OneSignal . Default . Notifications . PermissionChanged += _notificationPermissionChanged ;
122
132
123
133
// Setup the below to listen for and respond to events from in-app messages
124
134
OneSignal . Default . InAppMessages . WillDisplay += _iamWillDisplay ;
@@ -128,7 +138,6 @@ private void Start() {
128
138
OneSignal . Default . InAppMessages . Clicked += _iamOnClick ;
129
139
130
140
// Setup the below to listen for and respond to state changes
131
- OneSignal . Default . Notifications . PermissionChanged += _notificationPermissionChanged ;
132
141
OneSignal . Default . User . PushSubscription . Changed += _pushSubscriptionChanged ;
133
142
}
134
143
@@ -170,7 +179,7 @@ private void _iamOnClick(InAppMessageClickedResult result) {
170
179
}
171
180
172
181
private void _notificationPermissionChanged ( bool permission ) {
173
- _log ( $ "Notification Permissions changed to: { permission } ") ;
182
+ _log ( $ "Notification Permission changed to: { permission } ") ;
174
183
}
175
184
176
185
private void _pushSubscriptionChanged ( IPushSubscription subscription ) {
@@ -180,11 +189,6 @@ private void _pushSubscriptionChanged(IPushSubscription subscription) {
180
189
/*
181
190
* SDK setup
182
191
*/
183
-
184
- public void Initialize ( ) {
185
- _log ( $ "Initializing with appId <b>{ appId } </b>") ;
186
- OneSignal . Default . Initialize ( appId ) ;
187
- }
188
192
189
193
public void ToggleRequiresPrivacyConsent ( ) {
190
194
if ( OneSignal . Default . RequiresPrivacyConsent )
@@ -224,11 +228,17 @@ public async void LoginOneSignalUser() {
224
228
_log ( $ "Logging in user (<b>{ externalId } </b>) and awaiting result...") ;
225
229
226
230
await OneSignal . Default . LoginAsync ( externalId ) ;
231
+
232
+ _log ( "User login complete" ) ;
227
233
}
228
234
229
- public void LogoutOneSignalUser ( ) {
230
- _log ( $ "Logging out user ") ;
231
- OneSignal . Default . LogoutAsync ( ) ;
235
+ public async void LogoutOneSignalUser ( ) {
236
+ _log ( $ "Logging out user and awaiting result...") ;
237
+
238
+ await OneSignal . Default . LogoutAsync ( ) ;
239
+
240
+ _log ( "User logout complete" ) ;
241
+
232
242
}
233
243
234
244
public void AddEmail ( ) {
@@ -251,6 +261,25 @@ public void RemoveSms() {
251
261
OneSignal . Default . User . RemoveSmsSubscription ( phoneNumber ) ;
252
262
}
253
263
264
+ public void GetLanguage ( ) {
265
+ _log ( $ "Language set for the user is (<b>{ OneSignal . Default . User . Language } </b>)") ;
266
+ }
267
+
268
+ public void SetLanguage ( ) {
269
+ _log ( $ "Setting language for the user to (<b>{ language } </b>)") ;
270
+ OneSignal . Default . User . Language = language ;
271
+ }
272
+
273
+ public void PushSubscriptionOptIn ( ) {
274
+ _log ( $ "Opting in push subscription") ;
275
+ OneSignal . Default . User . PushSubscription . OptIn ( ) ;
276
+ }
277
+
278
+ public void PushSubscriptionOptOut ( ) {
279
+ _log ( $ "Opting out push subscription") ;
280
+ OneSignal . Default . User . PushSubscription . OptOut ( ) ;
281
+ }
282
+
254
283
/*
255
284
* Push
256
285
*/
@@ -261,14 +290,19 @@ public async void PromptForPush() {
261
290
var result = await OneSignal . Default . Notifications . RequestPermissionAsync ( true ) ;
262
291
263
292
if ( result )
264
- _log ( "User opted in " ) ;
293
+ _log ( "Notification permission accpeted " ) ;
265
294
else
266
- _log ( "User opted out" ) ;
295
+ _log ( "Notification permission denied" ) ;
296
+
297
+ _log ( $ "Notification permission is: { OneSignal . Default . Notifications . Permission } ") ;
267
298
}
268
299
269
- public void ClearPush ( ) {
270
- _log ( "Clearing existing OneSignal push notifications..." ) ;
271
- OneSignal . Default . Notifications . ClearAllNotifications ( ) ;
300
+ public async void ClearPush ( ) {
301
+ _log ( "Clearing existing OneSignal push notifications and awaiting result ..." ) ;
302
+
303
+ await OneSignal . Default . Notifications . ClearAllNotificationsAsync ( ) ;
304
+
305
+ _log ( "Notifications cleared" ) ;
272
306
}
273
307
274
308
/*
@@ -344,7 +378,7 @@ public async void PromptLocation() {
344
378
}
345
379
346
380
public void ToggleShareLocation ( ) {
347
- _log ( $ "Toggling ShareLocation to <b>{ ! OneSignal . Default . Location . IsShared } </b>") ;
381
+ _log ( $ "Toggling Location IsShared to <b>{ ! OneSignal . Default . Location . IsShared } </b>") ;
348
382
OneSignal . Default . Location . IsShared = ! OneSignal . Default . Location . IsShared ;
349
383
}
350
384
@@ -365,12 +399,14 @@ public void ToggleLaunchURLsInApp() {
365
399
*/
366
400
367
401
public Text console ;
402
+ public Text appIdText ;
368
403
369
404
public void SetAppIdString ( string newVal ) => appId = newVal ;
370
405
371
406
public void SetExternalIdString ( string newVal ) => externalId = newVal ;
372
407
public void SetEmailString ( string newVal ) => email = newVal ;
373
408
public void SetPhoneNumberString ( string newVal ) => phoneNumber = newVal ;
409
+ public void SetLanguageString ( string newVal ) => language = newVal ;
374
410
375
411
public void SetTriggerKey ( string newVal ) => triggerKey = newVal ;
376
412
public void SetTriggerValue ( string newVal ) => triggerValue = newVal ;
@@ -385,21 +421,25 @@ private void Awake() {
385
421
SDKDebug . LogIntercept += _log ;
386
422
SDKDebug . WarnIntercept += _warn ;
387
423
SDKDebug . ErrorIntercept += _error ;
424
+ appIdText . text = appId ;
388
425
}
389
426
390
427
private void _log ( object message ) {
428
+ string green = "#3BB674" ;
391
429
UnityEngine . Debug . Log ( message ) ;
392
- console . text += $ "\n <color=green><b>I></b></color> { message } ";
430
+ console . text += $ "\n <color={ green } ><b>I></b></color> { message } ";
393
431
}
394
432
395
433
private void _warn ( object message ) {
434
+ string yellow = "#FFA940" ;
396
435
UnityEngine . Debug . LogWarning ( message ) ;
397
- console . text += $ "\n <color=orange ><b>W></b></color> { message } ";
436
+ console . text += $ "\n <color={ yellow } ><b>W></b></color> { message } ";
398
437
}
399
438
400
439
private void _error ( object message ) {
440
+ string red = "#E54B4D" ;
401
441
UnityEngine . Debug . LogError ( message ) ;
402
- console . text += $ "\n <color=red><b>E></b></color> { message } ";
442
+ console . text += $ "\n <color={ red } ><b>E></b></color> { message } ";
403
443
}
404
444
#endregion
405
445
0 commit comments