예제 #1
0
        public override void DownloadMessages(OnForceContentUpdate completedHandler)
        {
            Request request = RequestBuilder.withGetInboxMessagesAction().CreateImmediate();

            request.Response += delegate(object data)
            {
                try
                {
                    var response      = 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;
                                                }

                                                if (varsDict.TryGetValue(Constants.Keys.DATA, out var advancedData))
                                                {
                                                    message.Data = advancedData as IDictionary <string, object>;
                                                }
                                            }
                                        }
                                    }
                                }

                                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);
            };

            Leanplum.RequestSender.Send(request);
        }
        private void SendRequestsHeartbeat()
        {
            Request request = RequestBuilder.withHeartbeatAction().CreateImmediate();

            Leanplum.RequestSender.Send(request);
        }