我必须发送两个带有不同类别标识符的不同通知。
当我仅设置这些通知之一时,操作将正确显示。但是,如果我将这两个通知都设置为将来的某个时间,则第二个通知将采取适当的措施。
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));
}
当just condition或condition2为true时,将正确发送一个通知,并显示两个操作。当两个条件都成立时,将发送两个通知,但是只有第二个通知才有操作。
答案 0 :(得分:0)
摘自有关UNUserNotificationCenter.Current.SetNotificationCategories的文档:
在启动时调用此方法以注册您的应用可操作 通知类型。此方法将您的所有类别注册在 一次,将所有以前注册的类别替换为新的 在类别参数中。 通常,您仅调用此方法 一次。
您在代码中调用了此功能两次,第二次调用将替换。这就是为什么只有第二个采取适当措施的原因。
解决方案仅调用一次此功能,然后注册所有类别。