예제 #1
0
        public async void Run(IBackgroundTaskInstance taskInstance)
        {
            deferral = taskInstance.GetDeferral();

            try
            {
                var details = (SocketActivityTriggerDetails)taskInstance.TriggerDetails;
                Debug.WriteLine($"{details.Reason}");
                var internetProfile = NetworkInformation.GetInternetConnectionProfile();
                if (internetProfile == null)
                {
                    Debug.WriteLine("No internet..");
                    return;
                }
                string selectedUser = null;
                try
                {
                    var obj = ApplicationSettingsHelper.LoadSettingsValue("InstaApiSelectedUsername");
                    if (obj is string str)
                    {
                        selectedUser = str;
                    }
                }
                catch { }
                await CS.Load();

                A.InstaApiList = CS.InstaApiList;
                var api = !string.IsNullOrEmpty(selectedUser)?
                          (CS.InstaApiList.FirstOrDefault(x => x.GetLoggedUser().LoggedInUser.UserName.ToLower() == selectedUser.ToLower()) ?? CS.InstaApiList[0]) : CS.InstaApiList[0];


                //foreach (var api in CS.InstaApiList)
                {
                    try
                    {
                        var push = new PushClient(CS.InstaApiList, api)
                        {
                            IsRunningFromBackground = true
                        };
                        push.MessageReceived += A.OnMessageReceived;
                        push.OpenNow();
                        push.Start();

                        switch (details.Reason)
                        {
                        case SocketActivityTriggerReason.SocketClosed:
                        {
                            await Task.Delay(TimeSpan.FromSeconds(5));

                            await push.StartFresh();

                            break;
                        }

                        default:
                        {
                            var socket = details.SocketInformation.StreamSocket;
                            await push.StartWithExistingSocket(socket);

                            break;
                        }
                        }
                        await Task.Delay(TimeSpan.FromSeconds(5));

                        await push.TransferPushSocket();
                    }
                    catch { }
                }
            }
            catch (Exception e)
            {
                Debug.WriteLine(e);
                Debug.WriteLine($"{typeof(SocketActivityTask).FullName}: Can't finish push cycle. Abort.");
            }
            finally
            {
                deferral.Complete();
            }
        }