Ejemplo n.º 1
0
        public void TestValidateThrowsExceptionWhenPushTokenIsNull()
        {
            var mockDevice = new Mock <Device>();

            PushInstallationLog log = new PushInstallationLog(mockDevice.Object, null, Timestamp, null, null);

            Assert.ThrowsException <ValidationException>(() => log.Validate());
        }
Ejemplo n.º 2
0
        /// <summary>
        /// If enabled, register push channel and send URI to backend.
        /// Also start intercepting pushes.
        /// If disabled and previously enabled, stop listening for pushes (they will still be received though).
        /// </summary>
        private void ApplyEnabledState(bool enabled)
        {
            if (enabled)
            {
                // We expect caller of this method to lock on _mutex, we can't do it here as that lock is not recursive
                AppCenterLog.Debug(LogTag, "Getting push token...");
                var state = _mutex.State;
                CreatePushNotificationChannel(channel =>
                {
                    try
                    {
                        using (_mutex.GetLock(state))
                        {
                            LatestPushToken = channel?.Uri;
                            if (!string.IsNullOrEmpty(LatestPushToken))
                            {
                                // Save channel member
                                _channel = channel;

                                // Subscribe to UserId Change.
                                UserIdContext.UserIdUpdated -= OnUserIdUpdated;
                                UserIdContext.UserIdUpdated += OnUserIdUpdated;

                                // Subscribe to push.
                                channel.PushNotificationReceived += OnPushNotificationReceivedHandler;

                                // Send channel URI to backend
                                AppCenterLog.Debug(LogTag, $"Push token '{LatestPushToken}'");
                                var pushInstallationLog = new PushInstallationLog(null, LatestPushToken, null, Guid.NewGuid(), UserIdContext.Instance.UserId);

                                // Do not await the call to EnqueueAsync or the UI thread can be blocked!
#pragma warning disable CS4014
                                Channel.EnqueueAsync(pushInstallationLog);
#pragma warning restore
                            }
                            else
                            {
                                AppCenterLog.Error(LogTag, "Push service registering with App Center backend has failed.");
                            }
                        }
                    }
                    catch (StatefulMutexException)
                    {
                        AppCenterLog.Warn(LogTag, "Push Enabled state changed after creating channel.");
                    }
                });
            }
            else if (_channel != null)
            {
                LatestPushToken                    = null;
                UserIdContext.UserIdUpdated       -= OnUserIdUpdated;
                _channel.PushNotificationReceived -= OnPushNotificationReceivedHandler;
            }
        }
Ejemplo n.º 3
0
        public void OnUserIdUpdated(object sender, UserIdUpdatedEventArgs e)
        {
            using (_mutex.GetLock(_mutex.State))
            {
                if (!string.IsNullOrEmpty(LatestPushToken))
                {
                    var pushInstallationLog = new PushInstallationLog(null, LatestPushToken, null, Guid.NewGuid(), e.UserId);
#pragma warning disable CS4014
                    Channel.EnqueueAsync(pushInstallationLog);
#pragma warning restore
                }
            }
        }
Ejemplo n.º 4
0
        /// <summary>
        /// If enabled, register push channel and send URI to backend.
        /// Also start intercepting pushes.
        /// If disabled and previously enabled, stop listening for pushes (they will still be received though).
        /// </summary>
        private void ApplyEnabledState(bool enabled)
        {
            if (enabled)
            {
                // We expect caller of this method to lock on _mutex, we can't do it here as that lock is not recursive
                MobileCenterLog.Debug(LogTag, "Getting push token...");
                var state = _mutex.State;
                Task.Run(async() =>
                {
                    var channel = await new WindowsPushNotificationChannelManager().CreatePushNotificationChannelForApplicationAsync()
                                  .AsTask().ConfigureAwait(false);
                    try
                    {
                        using (await _mutex.GetLockAsync(state).ConfigureAwait(false))
                        {
                            var pushToken = channel.Uri;
                            if (!string.IsNullOrEmpty(pushToken))
                            {
                                // Save channel member
                                _channel = channel;

                                // Subscribe to push
                                channel.PushNotificationReceived += OnPushNotificationReceivedHandler;

                                // Send channel URI to backend
                                MobileCenterLog.Debug(LogTag, $"Push token '{pushToken}'");

                                var pushInstallationLog = new PushInstallationLog(null, null, pushToken, Guid.NewGuid());

                                // Do not await the call to EnqueueAsync or the UI thread can be blocked!
#pragma warning disable CS4014
                                Channel.EnqueueAsync(pushInstallationLog);
#pragma warning restore
                            }
                            else
                            {
                                MobileCenterLog.Error(LogTag, "Push service registering with Mobile Center backend has failed.");
                            }
                        }
                    }
                    catch (StatefulMutexException)
                    {
                        MobileCenterLog.Warn(LogTag, "Push Enabled state changed after creating channel.");
                    }
                });
            }
            else if (_channel != null)
            {
                _channel.PushNotificationReceived -= OnPushNotificationReceivedHandler;
            }
        }
Ejemplo n.º 5
0
        public void TestConstructor()
        {
            var mockDevice = new Mock <Device>();

            PushInstallationLog log = new PushInstallationLog(Timestamp, mockDevice.Object, "token1");

            Assert.IsNotNull(log);
            Assert.AreEqual(default(System.Guid?), log.Sid);
            Assert.AreEqual("token1", log.PushToken);

            PushInstallationLog log2 = new PushInstallationLog(Timestamp, mockDevice.Object, "token2", System.Guid.NewGuid());

            Assert.IsNotNull(log2);
            Assert.IsNotNull(log2.Sid);
            Assert.AreEqual("token2", log2.PushToken);
        }
Ejemplo n.º 6
0
        /// <summary>
        /// If enabled, register push channel and send URI to backend.
        /// Also start intercepting pushes.
        /// If disabled and previously enabled, stop listening for pushes (they will still be received though).
        /// </summary>
        private void ApplyEnabledState(bool enabled)
        {
            if (enabled)
            {
                // We expect caller of this method to lock on _mutex, we can't do it here as that lock is not recursive
                var stateSnapshot = _stateKeeper.GetStateSnapshot();
                Task.Run(async() =>
                {
                    var channel = await new WindowsPushNotificationChannelManager().CreatePushNotificationChannelForApplicationAsync()
                                  .AsTask().ConfigureAwait(false);
                    try
                    {
                        _mutex.Lock(stateSnapshot);
                        var pushToken = channel.Uri;
                        if (!string.IsNullOrEmpty(pushToken))
                        {
                            // Save channel member
                            _channel = channel;

                            // Subscribe to push
                            channel.PushNotificationReceived += OnPushNotificationReceivedHandler;

                            // Send channel URI to backend
                            MobileCenterLog.Debug(LogTag, $"Push token '{pushToken}'");
                            var pushInstallationLog = new PushInstallationLog(0, null, pushToken, Guid.NewGuid());
                            await Channel.Enqueue(pushInstallationLog).ConfigureAwait(false);
                        }
                        else
                        {
                            MobileCenterLog.Error(LogTag, "Push service registering with Mobile Center backend has failed.");
                        }
                    }
                    catch (StatefulMutexException)
                    {
                        MobileCenterLog.Warn(LogTag, "Push Enabled state changed after creating channel.");
                    }
                    finally
                    {
                        _mutex.Unlock();
                    }
                });
            }
            else if (_channel != null)
            {
                _channel.PushNotificationReceived -= OnPushNotificationReceivedHandler;
            }
        }