예제 #1
0
        public async Task <PushNotificationsUnregisterResult> UnregisterFromPushNotifications(PushNotificationsUnregisterOptions options)
        {
            // Unregister in system if needed
            if (options.ShouldUnregisterInSystem() && PushTokenStorageService.IsTokenRegisteredInSystem)
            {
                var tokenRemovedFromSystem = await UnregisterFromPushTokenInSystem();

                if (!tokenRemovedFromSystem)
                {
                    return(PushNotificationsUnregisterResult.Failed);
                }

                PushTokenStorageService.IsTokenRegisteredInSystem = false;
            }

            var token = PushTokenStorageService.PushToken;

            if (string.IsNullOrEmpty(token))
            {
                return(PushNotificationsUnregisterResult.Success);
            }

            // Unregister on server if needed
            if (options.ShouldUnregisterOnServer() && PushTokenStorageService.IsTokenSavedOnServer)
            {
                var tokenRemovedFromServer = await RemotePushNotificationsService
                                             .RemovePushNotificationsToken(token).ConfigureAwait(false);

                if (!tokenRemovedFromServer)
                {
                    return(PushNotificationsUnregisterResult.ServerFailed);
                }

                PushTokenStorageService.IsTokenSavedOnServer = false;
            }

            // Clear token if it is unregistered both in the system (optional) and on server
            PushTokenStorageService.PushToken = string.Empty;

            return(PushNotificationsUnregisterResult.Success);
        }
 public static bool ShouldUnregisterOnServer(this PushNotificationsUnregisterOptions options) =>
 options == PushNotificationsUnregisterOptions.OnServerOnly ||
 options == PushNotificationsUnregisterOptions.InSystemAndOnServer;