/// <summary> /// Call this when your application pauses. /// </summary> internal static void Pause() { if (Constants.isNoop) { return; } if (!calledStart) { CompatibilityLayer.LogError("You cannot call Pause before calling start."); return; } if (!isPaused) { isPaused = true; LeanplumRequest request = LeanplumRequest.Post(Constants.Methods.PAUSE_SESSION, null); request.Response += (object obj) => { CompatibilityLayer.FlushSavedSettings(); }; request.Error += (Exception obj) => { CompatibilityLayer.FlushSavedSettings(); }; request.SendIfConnected(); } }
public override void Remove(string messageId) { if (messageId != null) { var msgs = _messages.FindAll(msg => msg.Id != messageId).ToList(); UpdateMessages(msgs); var param = new Dictionary <string, string> { [Constants.Params.INBOX_MESSAGE_ID] = messageId }; LeanplumRequest request = LeanplumRequest.Post(Constants.Methods.DELETE_INBOX_MESSAGE, param); request.SendIfConnected(); } }
public override void Read(string messageId) { if (messageId != null) { var msg = _messages.Find(message => message.Id == messageId); if (msg != null) { msg.IsRead = true; var param = new Dictionary <string, string> { [Constants.Params.INBOX_MESSAGE_ID] = messageId }; LeanplumRequest request = LeanplumRequest.Post(Constants.Methods.MARK_INBOX_MESSAGE_AS_READ, param); request.SendIfConnected(); } } }
/// <summary> /// Call this when your application starts. /// This will initiate a call to Leanplum's servers to get the values /// of the variables used in your app. /// </summary> public override void Start(string userId, IDictionary <string, object> attributes, Leanplum.StartHandler startResponseAction) { if (calledStart) { CompatibilityLayer.Log("Already called start"); return; } if (String.IsNullOrEmpty(LeanplumRequest.AppId)) { CompatibilityLayer.LogError("You cannot call Start without setting your app's " + "API keys."); return; } if (CompatibilityLayer.GetPlatformName() == "Standalone") { // TODO: Fix this. // This is a workaround for SSL issues when running on // standalone when connecting with the production server. // No issue on staging. Constants.API_SSL = false; } if (startResponseAction != null) { Started += startResponseAction; } if (Constants.isNoop) { _hasStarted = true; startSuccessful = true; OnVariablesChanged(); OnVariablesChangedAndNoDownloadsPending(); OnStarted(true); VarCache.ApplyVariableDiffs(null, null); return; } ValidateAttributes(attributes); calledStart = true; // Load the variables that were stored on the device from the last session. VarCache.IsSilent = true; VarCache.LoadDiffs(); VarCache.IsSilent = false; // Setup class members. VarCache.Update += delegate { OnVariablesChanged(); if (LeanplumRequest.PendingDownloads == 0) { OnVariablesChangedAndNoDownloadsPending(); } }; LeanplumRequest.NoPendingDownloads += delegate { OnVariablesChangedAndNoDownloadsPending(); }; string deviceId; if (customDeviceId != null) { deviceId = customDeviceId; } else { deviceId = CompatibilityLayer.GetDeviceId(); } LeanplumRequest.DeviceId = deviceId; // Don't overwrite UserID if it was set previously if Start() // was called without a new UserID. if (!String.IsNullOrEmpty(userId)) { LeanplumRequest.UserId = userId; } if (String.IsNullOrEmpty(LeanplumRequest.UserId)) { LeanplumRequest.UserId = deviceId; } // Setup parameters. var parameters = new Dictionary <string, string>(); parameters[Constants.Params.INCLUDE_DEFAULTS] = false.ToString(); parameters[Constants.Params.VERSION_NAME] = CompatibilityLayer.VersionName ?? ""; parameters[Constants.Params.DEVICE_NAME] = CompatibilityLayer.GetDeviceName(); parameters[Constants.Params.DEVICE_MODEL] = CompatibilityLayer.GetDeviceModel(); parameters[Constants.Params.DEVICE_SYSTEM_NAME] = CompatibilityLayer.GetSystemName(); parameters[Constants.Params.DEVICE_SYSTEM_VERSION] = CompatibilityLayer.GetSystemVersion(); TimeZone timezone = System.TimeZone.CurrentTimeZone; if (timezone.IsDaylightSavingTime(DateTime.UtcNow)) { parameters[Constants.Keys.TIMEZONE] = timezone.DaylightName; } else { parameters[Constants.Keys.TIMEZONE] = timezone.StandardName; } parameters[Constants.Keys.TIMEZONE_OFFSET_SECONDS] = timezone.GetUtcOffset(DateTime.UtcNow).TotalSeconds.ToString(); parameters[Constants.Keys.COUNTRY] = Constants.Values.DETECT; parameters[Constants.Keys.REGION] = Constants.Values.DETECT; parameters[Constants.Keys.CITY] = Constants.Values.DETECT; parameters[Constants.Keys.LOCATION] = Constants.Values.DETECT; if (attributes != null) { parameters[Constants.Params.USER_ATTRIBUTES] = Json.Serialize(attributes); } // Issue start API call. LeanplumRequest req = LeanplumRequest.Post(Constants.Methods.START, parameters); req.Response += delegate(object responsesObject) { IDictionary <string, object> response = Util.GetLastResponse(responsesObject) as IDictionary <string, object>; IDictionary <string, object> values = Util.GetValueOrDefault(response, Constants.Keys.VARS) as IDictionary <string, object> ?? new Dictionary <string, object>(); IDictionary <string, object> fileAttributes = Util.GetValueOrDefault(response, Constants.Keys.FILE_ATTRIBUTES) as IDictionary <string, object> ?? new Dictionary <string, object>(); List <object> variants = Util.GetValueOrDefault(response, Constants.Keys.VARIANTS) as List <object> ?? new List <object>(); bool isRegistered = (bool)Util.GetValueOrDefault(response, Constants.Keys.IS_REGISTERED, false); LeanplumRequest.Token = Util.GetValueOrDefault(response, Constants.Keys.TOKEN) as string ?? ""; // Allow bidirectional realtime variable updates. if (Constants.isDevelopmentModeEnabled) { VarCache.SetDevModeValuesFromServer( Util.GetValueOrDefault(response, Constants.Keys.VARS_FROM_CODE) as Dictionary <string, object>); if (Constants.EnableRealtimeUpdatesInDevelopmentMode && SocketUtilsFactory.Utils.AreSocketsAvailable) { leanplumSocket = new LeanplumSocket(delegate() { // Callback when we receive an updateVars command through the // development socket. // Set a flag so that the next time VarCache.CheckVarsUpdate() is // called the variables are updated. VarCache.VarsNeedUpdate = true; }); } // Register device. if (isRegistered) { // Check for updates. string latestVersion = Util.GetValueOrDefault(response, Constants.Keys.LATEST_VERSION) as string; if (latestVersion != null) { CompatibilityLayer.Log("Leanplum Unity SDK " + latestVersion + " available. Go to https://www.leanplum.com/dashboard to " + "download it."); } OnHasStartedAndRegisteredAsDeveloper(); } } VarCache.ApplyVariableDiffs(values, fileAttributes, variants); _hasStarted = true; startSuccessful = true; OnStarted(true); CompatibilityLayer.Init(); }; req.Error += delegate { VarCache.ApplyVariableDiffs(null, null); _hasStarted = true; startSuccessful = false; OnStarted(false); CompatibilityLayer.Init(); }; req.SendIfConnected(); }
public override void DownloadMessages(OnForceContentUpdate completedHandler) { LeanplumRequest request = LeanplumRequest.Post(Constants.Methods.GET_INBOX_MESSAGES, null); request.Response += delegate(object data) { try { var response = Util.GetLastResponse(data) as IDictionary <string, object>; var messages = Util.GetValueOrDefault(response, Constants.Keys.INBOX_MESSAGES) as IDictionary <string, object>; var inboxMessages = new List <Message>(); if (messages != null) { foreach (var pair in messages) { var id = pair.Key; if (pair.Value is IDictionary <string, object> value) { var message = new Message { Id = id }; if (value.TryGetValue(Constants.Keys.MESSAGE_IS_READ, out var isRead)) { if (isRead is bool b) { message.IsRead = b; } } if (value.TryGetValue(Constants.Keys.DELIVERY_TIMESTAMP, out var deliveryTimestamp)) { if (deliveryTimestamp is long l) { TimeSpan ts = TimeSpan.FromMilliseconds(l); DateTime date = new DateTime(1970, 1, 1).AddTicks(ts.Ticks); message.DeliveryTimestamp = date; } } if (value.TryGetValue(Constants.Keys.EXPIRATION_TIMESTAMP, out var expirationTimestamp)) { if (expirationTimestamp is long l) { TimeSpan ts = TimeSpan.FromMilliseconds(l); DateTime date = new DateTime(1970, 1, 1).AddTicks(ts.Ticks); message.ExpirationTimestamp = date; } } if (value.TryGetValue(Constants.Keys.MESSAGE_DATA, out var messageData)) { if (messageData is IDictionary <string, object> dict) { if (dict.TryGetValue(Constants.Keys.VARS, out var vars)) { if (vars is Dictionary <string, object> varsDict) { message.ActionContext = varsDict; if (varsDict.TryGetValue(Constants.Keys.TITLE, out var title)) { message.Title = title as string; } if (varsDict.TryGetValue(Constants.Keys.SUBTITLE, out var subtitle)) { message.Subtitle = subtitle as string; } if (varsDict.TryGetValue(Constants.Keys.IMAGE, out var image)) { message.ImageURL = image as string; } } } } } inboxMessages.Add(message); } } } UpdateMessages(inboxMessages); TriggerInboxSyncedWithStatus(true, completedHandler); } catch (Exception exception) { Debug.Log("exception getting messages: " + exception.Message); TriggerInboxSyncedWithStatus(false, completedHandler); } }; request.Error += delegate { TriggerInboxSyncedWithStatus(false, completedHandler); }; request.SendIfConnected(); }