Ejemplo n.º 1
0
        static void RegisterUserNotificationCategories(NotificationUserCategory[] userCategories)
        {
            if (userCategories != null && userCategories.Length > 0)
            {
                UsernNotificationCategories.Clear();
                IList <UNNotificationCategory> categories = new List <UNNotificationCategory>();
                foreach (var userCat in userCategories)
                {
                    IList <UNNotificationAction> actions = new List <UNNotificationAction>();

                    foreach (var action in userCat.Actions)
                    {
                        // Create action
                        switch (action.Type)
                        {
                        case NotificationActionType.Default:
                            actions.Add(UNNotificationAction.FromIdentifier(action.Id, action.Title, UNNotificationActionOptions.None));
                            break;

                        case NotificationActionType.AuthenticationRequired:
                            actions.Add(UNNotificationAction.FromIdentifier(action.Id, action.Title, UNNotificationActionOptions.AuthenticationRequired));
                            break;

                        case NotificationActionType.Destructive:
                            actions.Add(UNNotificationAction.FromIdentifier(action.Id, action.Title, UNNotificationActionOptions.Destructive));
                            break;

                        case NotificationActionType.Foreground:
                            actions.Add(UNNotificationAction.FromIdentifier(action.Id, action.Title, UNNotificationActionOptions.Foreground));
                            break;

                        case NotificationActionType.Reply:
                            actions.Add(UNTextInputNotificationAction.FromIdentifier(action.Id, action.Title, UNNotificationActionOptions.None, action.Title, string.Empty));
                            break;
                        }
                    }

                    // Create category
                    var categoryID          = userCat.Category;
                    var notificationActions = actions.ToArray() ?? new UNNotificationAction[] { };
                    var intentIDs           = new string[] { };
                    var categoryOptions     = new UNNotificationCategoryOptions[] { };

                    var category = UNNotificationCategory.FromIdentifier(categoryID, notificationActions, intentIDs, userCat.Type == NotificationCategoryType.Dismiss ? UNNotificationCategoryOptions.CustomDismissAction : UNNotificationCategoryOptions.None);
                    categories.Add(category);

                    UsernNotificationCategories.Add(userCat);
                }

                // Register categories
                UNUserNotificationCenter.Current.SetNotificationCategories(new NSSet <UNNotificationCategory>(categories.ToArray()));
            }
        }