首页 > 解决方案 > 如何使用 Xamarin iOS 发送具有不同类别标识符的多个通知?

问题描述

我必须发送两个不同的通知,每个通知都有不同的类别标识符。

当我只设置其中一个通知时,操作会正确显示。但是,如果我将两个通知都设置为将来的某个时间,则第二个通知将有适当的操作。

if(condition){
        var content = new UNMutableNotificationContent();
        content.Title = "Notification1";
        content.Body = "blah blah balh";
        content.Badge = 1;
        content.CategoryIdentifier = "cat1";

        var requestID = pos1.ToString();
        var date = new NSDateComponents();
        date.Hour = this.time.Hour;
        date.Minute = this.time.Minute;
        date.Weekday = i + 1;
        var trigger = UNCalendarNotificationTrigger.CreateTrigger(date, true);

        var request = UNNotificationRequest.FromIdentifier(requestID, content, trigger);
        UNUserNotificationCenter.Current.AddNotificationRequest(request, (error) => {
            if (error != null) {
                Console.WriteLine($"Error: {error.LocalizedDescription ?? ""}");
            }
            else {
                Console.WriteLine("Scheduled alarm for " + date);
            }
        });
        // Create actions
        var action1 = UNNotificationAction.FromIdentifier("action1", "Action1", UNNotificationActionOptions.Foreground);
        var cancelID = "cancel";
        var cancel_title = "Cancel";
        var cancel_action = UNNotificationAction.FromIdentifier(cancelID, cancel_title, UNNotificationActionOptions.Destructive);

        // Create Category
        var actions = new UNNotificationAction[] { action1, cancel_action };

        var intentIDs = new string[] { };
        var categoryOptions = new UNNotificationCategoryOptions[] { };
        var category = UNNotificationCategory.FromIdentifier("cat1", actions, intentIDs, UNNotificationCategoryOptions.None);

        // Register Category
        var categories = new UNNotificationCategory[] { category };
        UNUserNotificationCenter.Current.SetNotificationCategories(new NSSet<UNNotificationCategory>(categories));
    }
if(condition2){
        var content = new UNMutableNotificationContent();
        content.Title = "Notification2";
        content.Body = "blah";
        content.Badge = 1;
        content.CategoryIdentifier = "Cat2";
        var requestID = pos2.ToString();
        var date = new NSDateComponents();
        date.Hour = this.time.Hour;
        date.Minute = this.time.Minute;
        date.Weekday = i + 1;
        var trigger = UNCalendarNotificationTrigger.CreateTrigger(date, true);

        var request = UNNotificationRequest.FromIdentifier(requestID, content, trigger);
        UNUserNotificationCenter.Current.AddNotificationRequest(request, (error) => {
            if (error != null) {
                Console.WriteLine($"Error: {error.LocalizedDescription ?? ""}");
            }
            else {
                Console.WriteLine("Scheduled alarm for " + date);
            }
        });
        var action2 = UNNotificationAction.FromIdentifier("action2","Action2", UNNotificationActionOptions.Foreground);
        var cancelID = "cancel";
        var cancel_title = "Cancel";
        var cancel_action = UNNotificationAction.FromIdentifier(cancelID, cancel_title, UNNotificationActionOptions.Destructive);

        // Create Category
        var actions = new UNNotificationAction[] { action2, cancel_action };
        Console.WriteLine(this.time + actions[0].ToString());
        var intentIDs = new string[] { };
        var categoryOptions = new UNNotificationCategoryOptions[] { };
        var category = UNNotificationCategory.FromIdentifier("Cat2", actions, intentIDs, UNNotificationCategoryOptions.None);

        // Register Category
        var categories = new UNNotificationCategory[] { category };
        UNUserNotificationCenter.Current.SetNotificationCategories(new NSSet<UNNotificationCategory>(categories));
}

当仅 condition 或 condition2 为真时,将正确发送一个通知,并显示两个操作。当两个条件都为真时,两个通知都将被发送,但是只有第二个通知会有动作。

标签: iosxamarinnotifications

解决方案


从有关UNUserNotificationCenter.Current.SetNotificationCategories的文档中:

在启动时调用此方法来注册您的应用的可操作通知类型。此方法一次注册所有类别,将任何先前注册的类别替换为 categories 参数中的新类别。通常,您只调用此方法一次。

您在代码中调用了此函数两次,第二次调用将替换您的第一次调用。这就是为什么只有第二个有正确行动的原因。

解决方案是只调用一次这个函数,然后注册所有的类别。


推荐阅读