ios - 如何使用 Xamarin iOS 发送具有不同类别标识符的多个通知?

标签 ios xamarin notifications

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

当我仅设置其中一项通知时,操作会正确显示。但是,如果我将这两个通知设置为将来的某个时间,第二个通知将执行正确的操作。

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));
}

当条件或条件2为真时,将正确发送一个通知,并显示其中两个操作。当两个条件都成立时,将发送两个通知,但只有第二个通知才会执行操作。

最佳答案

摘自关于 UNUserNotificationCenter.Current.SetNotificationCategories 的文档:

Call this method at launch time to register your app’s actionable notification types. This method registers all of your categories at once, replacing any previously registered categories with the new ones in the categories parameter. Typically, you call this method only once.

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

解决方案是仅调用此函数一次并注册当时的所有类别。

关于ios - 如何使用 Xamarin iOS 发送具有不同类别标识符的多个通知?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56894129/

相关文章:

ios - 如何在 navigationController swift 中设置后退变量?

ios - 从 appdelegate 获取当前可见的 modalViewController

ios - 如何在 sendAsynchronousRequest 的 completionHandler 中显示 UIAlertView?

ios - UISlider 未更新以绑定(bind) ViewModel 属性

android - MvxAutoCompleteTextView 停留在 'Wait starting for '

iphone - 核心数据删除最后一个条目

c# - ReactiveUI激活时不触发

android - 异步请求用户执行操作的最佳方式?

swift - 如何添加通知?

ios - 当 iOS 10 中的 UNUserNotificationCenterDelegate 收到新通知时,如何删除以前发送的通知?