Skip to content

Commit fb7b149

Browse files
committed
Added Android In-App messaging methods
* iOS has been stubbed for now
1 parent 458df46 commit fb7b149

File tree

7 files changed

+181
-13
lines changed

7 files changed

+181
-13
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
1-
<dependencies>
1+
<dependencies>
22
<androidPackages>
33
<androidPackage spec="com.google.android.gms:play-services-base:[10.2.1, 12.1.0[" />
44
<androidPackage spec="com.google.firebase:firebase-messaging:[10.2.1, 12.1.0["/>
55
<androidPackage spec="com.google.android.gms:play-services-location:[10.2.1, 12.1.0[" />
66
<androidPackage spec="com.android.support:support-v4:[26.0.0, 27.2.0[" />
7+
<androidPackage spec="com.android.support:cardview-v7:[26.0.0, 27.2.0[" />
78
<androidPackage spec="com.android.support:customtabs:[26.0.0, 27.2.0[" />
89
</androidPackages>
910
</dependencies>

OneSignalExample/Assets/OneSignal/Example/GameControllerExample.cs

100755100644
+32-2
Original file line numberDiff line numberDiff line change
@@ -54,10 +54,10 @@ void Start () {
5454
// Call before using any other methods on OneSignal (except setLogLevel or SetRequiredUserPrivacyConsent)
5555
// Should only be called once when your app is loaded.
5656
// OneSignal.Init(OneSignal_AppId);
57-
OneSignal.StartInit("78e8aff3-7ce2-401f-9da0-2d41f287ebaf")
57+
OneSignal.StartInit("99015f5e-87b1-462e-a75b-f99bf7c2822e")
5858
.HandleNotificationReceived(HandleNotificationReceived)
5959
.HandleNotificationOpened(HandleNotificationOpened)
60-
//.InFocusDisplaying(OneSignal.OSInFocusDisplayOption.Notification)
60+
.HandlerInAppMessageClicked(HandlerInAppMessageClicked)
6161
.EndInit();
6262

6363
OneSignal.inFocusDisplayType = OneSignal.OSInFocusDisplayOption.Notification;
@@ -66,6 +66,30 @@ void Start () {
6666
OneSignal.emailSubscriptionObserver += OneSignal_emailSubscriptionObserver;
6767

6868
var pushState = OneSignal.GetPermissionSubscriptionState();
69+
70+
OneSignalInAppMessageTriggerExamples();
71+
}
72+
73+
// Examples of using OneSignal In-App Message triggers
74+
private void OneSignalInAppMessageTriggerExamples() {
75+
// Add a single trigger
76+
OneSignal.AddTrigger("key", "value");
77+
78+
// Get the current value to a trigger by key
79+
var triggerValue = OneSignal.GetTriggerValueForKey("key");
80+
81+
// Add multiple triggers
82+
OneSignal.AddTriggers(new Dictionary<string, object>() { { "key1", "value1" }, { "key2", 2 } });
83+
84+
// Delete a trigger
85+
OneSignal.RemoveTriggerForKey("key");
86+
87+
// Delete a list of triggers
88+
OneSignal.RemoveTriggersForKeys(new List<string>() { "key1", "key2" });
89+
90+
// Temporarily puase In-App messages; If true is passed in.
91+
// Great to ensure you never interrupt your user while they are in the middle of a match in your game.
92+
OneSignal.PauseInAppMessages(false);
6993
}
7094

7195
private void OneSignal_subscriptionObserver(OSSubscriptionStateChanges stateChanges) {
@@ -126,6 +150,12 @@ public static void HandleNotificationOpened(OSNotificationOpenedResult result) {
126150
}
127151
}
128152

153+
public static void HandlerInAppMessageClicked(OSInAppMessageAction action) {
154+
String logInAppClickEvent = "In-App Message opened with action.clickName " + action.clickName;
155+
print(logInAppClickEvent);
156+
extraMessage = logInAppClickEvent;
157+
}
158+
129159
// Test Menu
130160
// Includes SendTag/SendTags, getting the userID and pushToken, and scheduling an example notification
131161
void OnGUI () {
Binary file not shown.

OneSignalExample/Assets/OneSignal/src/OneSignal.cs

100755100644
+80-6
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,13 @@ public class OSNotificationOpenedResult {
102102
public OSNotification notification;
103103
}
104104

105+
public class OSInAppMessageAction {
106+
public string clickName;
107+
public string clickUrl;
108+
public bool firstClick;
109+
public bool closesMessage;
110+
}
111+
105112
public enum OSNotificationPermission {
106113
NotDetermined,
107114
Denied,
@@ -161,6 +168,8 @@ public class OneSignal : MonoBehaviour {
161168
// result = The Notification open result describing : 1. The notification opened 2. The action taken by the user
162169
public delegate void NotificationOpened(OSNotificationOpenedResult result);
163170

171+
public delegate void InAppMessageClicked(OSInAppMessageAction action);
172+
164173
public delegate void IdsAvailableCallback(string playerID, string pushToken);
165174
public delegate void TagsReceived(Dictionary<string, object> tags);
166175
public delegate void PromptForPushNotificationsUserResponse(bool accepted);
@@ -264,11 +273,12 @@ public enum OSInFocusDisplayOption {
264273
}
265274

266275
public class UnityBuilder {
267-
public string appID = null;
268-
public string googleProjectNumber = null;
269-
public Dictionary<string, bool> iOSSettings = null;
270-
public NotificationReceived notificationReceivedDelegate = null;
271-
public NotificationOpened notificationOpenedDelegate = null;
276+
public string appID;
277+
public string googleProjectNumber;
278+
public Dictionary<string, bool> iOSSettings;
279+
public NotificationReceived notificationReceivedDelegate;
280+
public NotificationOpened notificationOpenedDelegate;
281+
public InAppMessageClicked inAppMessageClickHandlerDelegate;
272282

273283
// inNotificationReceivedDelegate = Calls this delegate when a notification is received.
274284
public UnityBuilder HandleNotificationReceived(NotificationReceived inNotificationReceivedDelegate) {
@@ -278,7 +288,13 @@ public UnityBuilder HandleNotificationReceived(NotificationReceived inNotificati
278288

279289
// inNotificationOpenedDelegate = Calls this delegate when a push notification is opened.
280290
public UnityBuilder HandleNotificationOpened(NotificationOpened inNotificationOpenedDelegate) {
281-
notificationOpenedDelegate = inNotificationOpenedDelegate; ;
291+
notificationOpenedDelegate = inNotificationOpenedDelegate;
292+
return this;
293+
}
294+
295+
// inInAppMessageClickHandlerDelegate = Calls this delegate when an In-App Message is opened.
296+
public UnityBuilder HandlerInAppMessageClicked(InAppMessageClicked inInAppMessageClickedDelegate) {
297+
inAppMessageClickHandlerDelegate = inInAppMessageClickedDelegate;
282298
return this;
283299
}
284300

@@ -654,6 +670,44 @@ public static void RemoveExternalUserId()
654670
#endif
655671
}
656672

673+
public static void AddTrigger(string key, object value) {
674+
#if ONESIGNAL_PLATFORM
675+
oneSignalPlatform.AddTrigger(key, value);
676+
#endif
677+
}
678+
679+
public static void AddTriggers(Dictionary<string, object> triggers) {
680+
#if ONESIGNAL_PLATFORM
681+
oneSignalPlatform.AddTriggers(triggers);
682+
#endif
683+
}
684+
685+
public static void RemoveTriggerForKey(string key) {
686+
#if ONESIGNAL_PLATFORM
687+
oneSignalPlatform.RemoveTriggerForKey(key);
688+
#endif
689+
}
690+
691+
public static void RemoveTriggersForKeys(IList<string> keys) {
692+
#if ONESIGNAL_PLATFORM
693+
oneSignalPlatform.RemoveTriggersForKeys(keys);
694+
#endif
695+
}
696+
697+
public static object GetTriggerValueForKey(string key) {
698+
#if ONESIGNAL_PLATFORM
699+
return oneSignalPlatform.GetTriggerValueForKey(key);
700+
#else
701+
return null;
702+
#endif
703+
}
704+
705+
public static void PauseInAppMessages(bool pause) {
706+
#if ONESIGNAL_PLATFORM
707+
oneSignalPlatform.PauseInAppMessages(pause);
708+
#endif
709+
}
710+
657711
/*** protected and private methods ****/
658712
#if ONESIGNAL_PLATFORM
659713

@@ -831,5 +885,25 @@ private void onPromptForPushNotificationsWithUserResponse(string accepted) {
831885
notificationUserResponseDelegate(Convert.ToBoolean(accepted));
832886
}
833887

888+
// Called from native SDK
889+
private void onInAppMessageClicked(string jsonString) {
890+
if (builder.inAppMessageClickHandlerDelegate == null)
891+
return;
892+
893+
var jsonObject = Json.Deserialize(jsonString) as Dictionary<string, object>;
894+
895+
var action = new OSInAppMessageAction();
896+
if (jsonObject.ContainsKey("click_name"))
897+
action.clickName = jsonObject["click_name"] as String;
898+
if (jsonObject.ContainsKey("click_url"))
899+
action.clickUrl = jsonObject["click_url"] as String;
900+
if (jsonObject.ContainsKey("closes_message"))
901+
action.closesMessage = (bool)jsonObject["closes_message"];
902+
if (jsonObject.ContainsKey("first_click"))
903+
action.firstClick = (bool)jsonObject["first_click"];
904+
905+
builder.inAppMessageClickHandlerDelegate(action);
906+
}
907+
834908
#endif
835909
}

OneSignalExample/Assets/OneSignal/src/OneSignalAndroid.cs

100755100644
+31-1
Original file line numberDiff line numberDiff line change
@@ -211,6 +211,36 @@ public void SetEmail(string email) {
211211

212212
public void LogoutEmail() {
213213
mOneSignal.Call("logoutEmail");
214-
}
214+
}
215+
216+
public void AddTrigger(string key, object value) {
217+
mOneSignal.Call("addTrigger", key, value.ToString());
218+
}
219+
220+
public void AddTriggers(IDictionary<string, object> triggers) {
221+
mOneSignal.Call("addTriggers", Json.Serialize(triggers));
222+
}
223+
224+
public void RemoveTriggerForKey(string key) {
225+
mOneSignal.Call("removeTriggerForKey", key);
226+
}
227+
228+
public void RemoveTriggersForKeys(IList<string> keys) {
229+
mOneSignal.Call("removeTriggersForKeys", Json.Serialize(keys));
230+
}
231+
232+
public object GetTriggerValueForKey(string key) {
233+
var valueJsonStr = mOneSignal.Call<string>("getTriggerValueForKey", key);
234+
if (valueJsonStr == null)
235+
return null;
236+
var valueDict = Json.Deserialize(valueJsonStr) as Dictionary<string, object>;
237+
if (valueDict.ContainsKey("value"))
238+
return valueDict["value"];
239+
return null;
240+
}
241+
242+
public void PauseInAppMessages(bool pause) {
243+
mOneSignal.Call("pauseInAppMessages", pause);
244+
}
215245
}
216246
#endif

OneSignalExample/Assets/OneSignal/src/OneSignalIOS.cs

100755100644
+26-1
Original file line numberDiff line numberDiff line change
@@ -311,6 +311,31 @@ public OSEmailSubscriptionState parseOSEmailSubscriptionState(object stateDict)
311311
state.subscribed = stateDictCasted.ContainsKey("emailUserId") && stateDictCasted["emailUserId"] != null;
312312

313313
return state;
314-
}
314+
}
315+
316+
public void AddTrigger(string key, object value) {
317+
// Do nothing, In-App not available for iOS yet.
318+
}
319+
320+
public void AddTriggers(IDictionary<string, object> triggers) {
321+
// Do nothing, In-App not available for iOS yet.
322+
}
323+
324+
public void RemoveTriggerForKey(string key) {
325+
// Do nothing, In-App not available for iOS yet.
326+
}
327+
328+
public void RemoveTriggersForKeys(IList<string> keys) {
329+
// Do nothing, In-App not available for iOS yet.
330+
}
331+
332+
public object GetTriggerValueForKey(string key) {
333+
// Do nothing, In-App not available for iOS yet.
334+
return null;
335+
}
336+
337+
public void PauseInAppMessages(bool pause) {
338+
// Do nothing, In-App not available for iOS yet.
339+
}
315340
}
316341
#endif

OneSignalExample/Assets/OneSignal/src/OneSignalPlatform.cs

100755100644
+10-2
Original file line numberDiff line numberDiff line change
@@ -64,8 +64,16 @@ public interface OneSignalPlatform {
6464
void addEmailSubscriptionObserver();
6565
void removeEmailSubscriptionObserver();
6666

67-
OSPermissionSubscriptionState getPermissionSubscriptionState();
68-
67+
OSPermissionSubscriptionState getPermissionSubscriptionState();
68+
69+
// In-App Messaging
70+
void AddTrigger(string key, object value);
71+
void AddTriggers(IDictionary<string, object> triggers);
72+
void RemoveTriggerForKey(string key);
73+
void RemoveTriggersForKeys(IList<string> keys);
74+
object GetTriggerValueForKey(string key);
75+
void PauseInAppMessages(bool pause);
76+
6977
OSPermissionState parseOSPermissionState(object stateDict);
7078
OSSubscriptionState parseOSSubscriptionState(object stateDict);
7179
OSEmailSubscriptionState parseOSEmailSubscriptionState (object stateDict);

0 commit comments

Comments
 (0)