Commit 492600e6 authored by Mohamed Adel's avatar Mohamed Adel Committed by Commit Bot

Set notifications not to close by default

Set UNNotification not to close on default action, and handle it to
close through the bridge's Close method. Added unittest for it as well.

Bug: 1134175
Change-Id: Ia7cd5751fb5c245434f1f4877ae4988353080a8f
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2442970
Commit-Queue: Mohamed Adel <adelm@google.com>
Reviewed-by: default avatarPeter Beverloo <peter@chromium.org>
Reviewed-by: default avatarRichard Knoll <knollr@chromium.org>
Reviewed-by: default avatarRayan Kanso <rayankans@chromium.org>
Cr-Commit-Position: refs/heads/master@{#813186}
parent d53cfb6e
......@@ -104,7 +104,28 @@ void NotificationPlatformBridgeMacUNNotification::Display(
void NotificationPlatformBridgeMacUNNotification::Close(
Profile* profile,
const std::string& notification_id) {
NOTIMPLEMENTED();
NSString* candidateId = base::SysUTF8ToNSString(notification_id);
NSString* currentProfileId = base::SysUTF8ToNSString(GetProfileId(profile));
[notification_center_ getDeliveredNotificationsWithCompletionHandler:^(
NSArray<UNNotification*>* _Nonnull notifications) {
for (UNNotification* notification in notifications) {
NSString* toastId = [[[[notification request] content] userInfo]
objectForKey:notification_constants::kNotificationId];
NSString* persistentProfileId =
[[[[notification request] content] userInfo]
objectForKey:notification_constants::kNotificationProfileId];
if ([toastId isEqualToString:candidateId] &&
[persistentProfileId isEqualToString:currentProfileId]) {
[notification_center_
removeDeliveredNotificationsWithIdentifiers:@[ toastId ]];
break;
}
}
}];
// TODO(crbug/1134539): If the notification was not present as a banner, check
// alerts
}
void NotificationPlatformBridgeMacUNNotification::GetDisplayed(
......
......@@ -37,6 +37,15 @@
? [_notificationData
objectForKey:notification_constants::kNotificationOrigin]
: @"";
// This uses a private API to prevent notifications from dismissing on default
// action instead of clicking on a button
if ([toast respondsToSelector:@selector
(shouldPreventNotificationDismissalAfterDefaultAction)]) {
[toast setValue:@YES
forKey:@"shouldPreventNotificationDismissalAfterDefaultAction"];
}
DCHECK(
[_notificationData objectForKey:notification_constants::kNotificationId]);
NSString* notificationId =
......
......@@ -180,3 +180,16 @@ TEST(UNNotificationBuilderMacTest, TestBuildDictionary) {
isEqualToNumber:@0]);
}
}
TEST(UNNotificationBuilderMacTest,
TestNotificationDoesNotCloseOnDefaultAction) {
if (@available(macOS 10.14, *)) {
base::scoped_nsobject<UNNotificationBuilder> builder =
NewTestBuilder(NotificationHandler::Type::WEB_PERSISTENT);
UNMutableNotificationContent* content = [builder buildUserNotification];
EXPECT_TRUE([[content
valueForKey:@"shouldPreventNotificationDismissalAfterDefaultAction"]
boolValue]);
}
}
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment