34
34
public class GameControllerExample : MonoBehaviour {
35
35
36
36
private static string extraMessage ;
37
+ public string email = "Email Address" ;
37
38
38
39
void Start ( ) {
39
40
extraMessage = null ;
@@ -54,23 +55,29 @@ void Start () {
54
55
OneSignal . inFocusDisplayType = OneSignal . OSInFocusDisplayOption . Notification ;
55
56
OneSignal . permissionObserver += OneSignal_permissionObserver ;
56
57
OneSignal . subscriptionObserver += OneSignal_subscriptionObserver ;
58
+ OneSignal . emailSubscriptionObserver += OneSignal_emailSubscriptionObserver ;
57
59
58
60
var pushState = OneSignal . GetPermissionSubscriptionState ( ) ;
59
61
Debug . Log ( "pushState.subscriptionStatus.subscribed : " + pushState . subscriptionStatus . subscribed ) ;
60
62
Debug . Log ( "pushState.subscriptionStatus.userId : " + pushState . subscriptionStatus . userId ) ;
61
63
}
62
64
63
65
private void OneSignal_subscriptionObserver ( OSSubscriptionStateChanges stateChanges ) {
64
- Debug . Log ( "stateChanges: " + stateChanges ) ;
65
- Debug . Log ( "stateChanges.to.userId: " + stateChanges . to . userId ) ;
66
- Debug . Log ( "stateChanges.to.subscribed: " + stateChanges . to . subscribed ) ;
66
+ Debug . Log ( "SUBSCRIPTION stateChanges: " + stateChanges ) ;
67
+ Debug . Log ( "SUBSCRIPTION stateChanges.to.userId: " + stateChanges . to . userId ) ;
68
+ Debug . Log ( "SUBSCRIPTION stateChanges.to.subscribed: " + stateChanges . to . subscribed ) ;
67
69
}
68
70
69
- private void OneSignal_permissionObserver ( OSPermissionStateChanges stateChanges ) {
70
- Debug . Log ( "stateChanges.from.status: " + stateChanges . from . status ) ;
71
- Debug . Log ( "stateChanges.to.status: " + stateChanges . to . status ) ;
71
+ private void OneSignal_permissionObserver ( OSPermissionStateChanges stateChanges ) {
72
+ Debug . Log ( "PERMISSION stateChanges.from.status: " + stateChanges . from . status ) ;
73
+ Debug . Log ( "PERMISSION stateChanges.to.status: " + stateChanges . to . status ) ;
72
74
}
73
75
76
+ private void OneSignal_emailSubscriptionObserver ( OSEmailSubscriptionStateChanges stateChanges ) {
77
+ Debug . Log ( "EMAIL stateChanges.from.status: " + stateChanges . from . emailUserId + ", " + stateChanges . from . emailAddress ) ;
78
+ Debug . Log ( "EMAIL stateChanges.to.status: " + stateChanges . to . emailUserId + ", " + stateChanges . to . emailAddress ) ;
79
+ }
80
+
74
81
// Called when your app is in focus and a notificaiton is recieved.
75
82
// The name of the method can be anything as long as the signature matches.
76
83
// Method must be static or this object should be marked as DontDestroyOnLoad
@@ -107,11 +114,11 @@ public static void HandleNotificationOpened(OSNotificationOpenedResult result) {
107
114
Debug . Log ( "[HandleNotificationOpened] message " + message + ", additionalData: " + Json . Serialize ( additionalData ) as string ) ;
108
115
109
116
if ( actionID != null ) {
110
- // actionSelected equals the id on the button the user pressed.
111
- // actionSelected will equal "__DEFAULT__" when the notification itself was tapped when buttons were present.
112
- extraMessage = "Pressed ButtonId: " + actionID ;
113
- }
114
- }
117
+ // actionSelected equals the id on the button the user pressed.
118
+ // actionSelected will equal "__DEFAULT__" when the notification itself was tapped when buttons were present.
119
+ extraMessage = "Pressed ButtonId: " + actionID ;
120
+ }
121
+ }
115
122
116
123
// Test Menu
117
124
// Includes SendTag/SendTags, getting the userID and pushToken, and scheduling an example notification
@@ -122,9 +129,24 @@ void OnGUI () {
122
129
GUIStyle guiBoxStyle = new GUIStyle ( "box" ) ;
123
130
guiBoxStyle . fontSize = 30 ;
124
131
125
- GUI . Box ( new Rect ( 10 , 10 , 390 , 340 ) , "Test Menu" , guiBoxStyle ) ;
132
+ GUIStyle textFieldStyle = new GUIStyle ( "textField" ) ;
133
+ textFieldStyle . fontSize = 30 ;
134
+
135
+
136
+ float itemOriginX = 50.0f ;
137
+ float itemWidth = Screen . width - 120.0f ;
138
+ float boxWidth = Screen . width - 20.0f ;
139
+ float boxOriginY = 120.0f ;
140
+ float boxHeight = 630.0f ;
141
+ float itemStartY = 200.0f ;
142
+ float itemHeightOffset = 90.0f ;
143
+ float itemHeight = 60.0f ;
144
+
145
+ GUI . Box ( new Rect ( 10 , boxOriginY , boxWidth , boxHeight ) , "Test Menu" , guiBoxStyle ) ;
146
+
147
+ float count = 0.0f ;
126
148
127
- if ( GUI . Button ( new Rect ( 60 , 80 , 300 , 60 ) , "SendTags" , customTextSize ) ) {
149
+ if ( GUI . Button ( new Rect ( itemOriginX , itemStartY + ( count * itemHeightOffset ) , itemWidth , itemHeight ) , "SendTags" , customTextSize ) ) {
128
150
// You can tags users with key value pairs like this:
129
151
OneSignal . SendTag ( "UnityTestKey" , "TestValue" ) ;
130
152
// Or use an IDictionary if you need to set more than one tag.
@@ -136,13 +158,18 @@ void OnGUI () {
136
158
// OneSignal.DeleteTags(new List<string>() {"UnityTestKey2", "UnityTestKey3" });
137
159
}
138
160
139
- if ( GUI . Button ( new Rect ( 60 , 170 , 300 , 60 ) , "GetIds" , customTextSize ) ) {
140
- OneSignal . IdsAvailable ( ( userId , pushToken ) => {
161
+ count ++ ;
162
+
163
+ if ( GUI . Button ( new Rect ( itemOriginX , itemStartY + ( count * itemHeightOffset ) , itemWidth , itemHeight ) , "GetIds" , customTextSize ) ) {
164
+ OneSignal . IdsAvailable ( ( userId , pushToken ) => {
141
165
extraMessage = "UserID:\n " + userId + "\n \n PushToken:\n " + pushToken ;
142
166
} ) ;
143
167
}
144
168
145
- if ( GUI . Button ( new Rect ( 60 , 260 , 300 , 60 ) , "TestNotification" , customTextSize ) ) {
169
+
170
+ count ++ ;
171
+
172
+ if ( GUI . Button ( new Rect ( itemOriginX , itemStartY + ( count * itemHeightOffset ) , itemWidth , itemHeight ) , "TestNotification" , customTextSize ) ) {
146
173
extraMessage = "Waiting to get a OneSignal userId. Uncomment OneSignal.SetLogLevel in the Start method if it hangs here to debug the issue." ;
147
174
OneSignal . IdsAvailable ( ( userId , pushToken ) => {
148
175
if ( pushToken != null ) {
@@ -158,21 +185,50 @@ void OnGUI () {
158
185
notification [ "send_after" ] = System . DateTime . Now . ToUniversalTime ( ) . AddSeconds ( 30 ) . ToString ( "U" ) ;
159
186
160
187
extraMessage = "Posting test notification now." ;
188
+
161
189
OneSignal . PostNotification ( notification , ( responseSuccess ) => {
162
190
extraMessage = "Notification posted successful! Delayed by about 30 secounds to give you time to press the home button to see a notification vs an in-app alert.\n " + Json . Serialize ( responseSuccess ) ;
163
191
} , ( responseFailure ) => {
164
192
extraMessage = "Notification failed to post:\n " + Json . Serialize ( responseFailure ) ;
165
193
} ) ;
166
- }
167
- else
194
+ } else {
168
195
extraMessage = "ERROR: Device is not registered." ;
196
+ }
197
+ } ) ;
198
+ }
199
+
200
+ count ++ ;
201
+
202
+ email = GUI . TextField ( new Rect ( itemOriginX , itemStartY + ( count * itemHeightOffset ) , itemWidth , itemHeight ) , email , customTextSize ) ;
203
+
204
+ count ++ ;
205
+
206
+ if ( GUI . Button ( new Rect ( itemOriginX , itemStartY + ( count * itemHeightOffset ) , itemWidth , itemHeight ) , "SetEmail" , customTextSize ) ) {
207
+ extraMessage = "Setting email to " + email ;
208
+
209
+ OneSignal . SetEmail ( email , ( ) => {
210
+ Debug . Log ( "Successfully set email" ) ;
211
+ } , ( error ) => {
212
+ Debug . Log ( "Encountered error setting email: " + Json . Serialize ( error ) ) ;
213
+ } ) ;
214
+ }
215
+
216
+ count ++ ;
217
+
218
+ if ( GUI . Button ( new Rect ( itemOriginX , itemStartY + ( count * itemHeightOffset ) , itemWidth , itemHeight ) , "LogoutEmail" , customTextSize ) ) {
219
+ extraMessage = "Logging Out of [email protected] " ;
220
+
221
+ OneSignal . LogoutEmail ( ( ) => {
222
+ Debug . Log ( "Successfully logged out of email" ) ;
223
+ } , ( error ) => {
224
+ Debug . Log ( "Encountered error logging out of email: " + Json . Serialize ( error ) ) ;
169
225
} ) ;
170
226
}
171
227
172
228
if ( extraMessage != null ) {
173
229
guiBoxStyle . alignment = TextAnchor . UpperLeft ;
174
230
guiBoxStyle . wordWrap = true ;
175
- GUI . Box ( new Rect ( 10 , 390 , Screen . width - 20 , Screen . height - 400 ) , extraMessage , guiBoxStyle ) ;
231
+ GUI . Box ( new Rect ( 10 , boxOriginY + boxHeight + 20 , Screen . width - 20 , Screen . height - ( boxOriginY + boxHeight + 40 ) ) , extraMessage , guiBoxStyle ) ;
176
232
}
177
233
}
178
234
}
0 commit comments