也许有人可以帮助我。在我的应用程序中,我使用推送通知来通知用户一条新消息已写入数据库。一位用户可以接受通知并处理内容或将其关闭。如果用户接受它,则会向所有其他较早收到通知的设备发送静默推送。这是我处理此静默通知的代码:
public override void ReceivedRemoteNotification(UIApplication application, NSDictionary remoteNotification)
{
try
{
if (remoteNotification != null)
{
var alert = remoteNotification[FromObject("aps")];
if (alert != null)
{
string id = ((NSDictionary)alert)[FromObject("deleteId")].Description;
if (!String.IsNullOrEmpty(id))
{
List<string> idents = new List<string>();
UNUserNotificationCenter.Current.GetDeliveredNotifications(completionHandler: (UNNotification[] t) =>
{
foreach (UNNotification item in t)
{
UNNotificationRequest curRequest = item.Request;
var notificationId = ((NSDictionary)curRequest.Content.UserInfo[FromObject("aps")])[FromObject("notificationId")].Description;
if (id == notificationId)
{
idents.Add(curRequest.Identifier);
}
}
UNUserNotificationCenter.Current.RemoveDeliveredNotifications(idents.ToArray());
});
}
}
}
}
catch (Exception ex)
{
Debug.WriteLine(ex);
}
}
问题是通知在通知中心仍然可见,直到应用程序被带到前台。但随后它被删除。
有没有办法强制该方法立即删除通知,而不仅仅是在(重新)打开应用程序时?