ios - 如何使用 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 为真时,将正确发送一个通知,并显示两个操作。当两个条件都为真时,两个通知都将被发送,但是只有第二个通知会有动作。
解决方案
从有关UNUserNotificationCenter.Current.SetNotificationCategories的文档中:
在启动时调用此方法来注册您的应用的可操作通知类型。此方法一次注册所有类别,将任何先前注册的类别替换为 categories 参数中的新类别。通常,您只调用此方法一次。
您在代码中调用了此函数两次,第二次调用将替换您的第一次调用。这就是为什么只有第二个有正确行动的原因。
解决方案是只调用一次这个函数,然后注册所有的类别。
推荐阅读
- java - GraphQL 错误:需要一个名为“FileUpload”的用户定义的 GraphQL 标量类型,但没有找到
- android - 如何使 Android 通知“持久”?
- c# - Asp Net Core (2.1) WebApp - API 和 Web 界面的身份验证
- react-native - Redux 远程开发工具:Chrome 检查器选项卡而不是单独的窗口
- sql - SQL:添加外键失败
- numpy - 在TensorFlow中计算张量中所有向量之间的成对欧几里得距离的有效方法
- python - 如何从另一个方法中调用一个方法?
- python - 如何解决 'SpreadsheetNotFound:' 而无需添加 client_email?
- python - 附加到列表后删除 html 源代码中的 \n
- python - Python 将 DXF 文件转换为 PDF 或 PNG 或 JPEG