Commit 85546199 authored by Mohamed Adel's avatar Mohamed Adel Committed by Commit Bot

Add support for simple categories

Added categories that show close and settings buttons on banners.
Settings button is determined by whether the notification wants one or
not.

Bug: 1129398, 1136040
Change-Id: I1d3d9b2c440780293aa23bd842a4b8c587bbbb58
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2456106Reviewed-by: default avatarRichard Knoll <knollr@chromium.org>
Reviewed-by: default avatarPeter Beverloo <peter@chromium.org>
Reviewed-by: default avatarRayan Kanso <rayankans@chromium.org>
Auto-Submit: Mohamed Adel <adelm@google.com>
Commit-Queue: Peter Beverloo <peter@chromium.org>
Cr-Commit-Position: refs/heads/master@{#815575}
parent ba2a8f08
...@@ -50,6 +50,8 @@ class API_AVAILABLE(macosx(10.14)) NotificationPlatformBridgeMacUNNotification ...@@ -50,6 +50,8 @@ class API_AVAILABLE(macosx(10.14)) NotificationPlatformBridgeMacUNNotification
// Request permission to send notifications // Request permission to send notifications
void RequestPermission(); void RequestPermission();
// Create default categories for banners and alerts
void CreateDefaultCategories();
private: private:
// Cocoa class that receives callbacks from the UNUserNotificationCenter. // Cocoa class that receives callbacks from the UNUserNotificationCenter.
......
...@@ -16,12 +16,21 @@ ...@@ -16,12 +16,21 @@
#include "chrome/browser/profiles/profile.h" #include "chrome/browser/profiles/profile.h"
#include "chrome/browser/ui/cocoa/notifications/unnotification_builder_mac.h" #include "chrome/browser/ui/cocoa/notifications/unnotification_builder_mac.h"
#import "chrome/browser/ui/cocoa/notifications/unnotification_response_builder_mac.h" #import "chrome/browser/ui/cocoa/notifications/unnotification_response_builder_mac.h"
#include "chrome/grit/generated_resources.h"
#include "content/public/browser/browser_task_traits.h" #include "content/public/browser/browser_task_traits.h"
#include "ui/base/l10n/l10n_util_mac.h"
#include "ui/message_center/public/cpp/notification.h" #include "ui/message_center/public/cpp/notification.h"
@class UNMutableNotificationContent; @class UNMutableNotificationContent;
@class UNUserNotificationCenter; @class UNUserNotificationCenter;
namespace {
NSString* const kCloseAndSettingsCategory = @"CLOSE_AND_SETTINGS";
NSString* const kCloseCategory = @"CLOSE";
} // namespace
// A Cocoa class that represents the delegate of UNUserNotificationCenter and // A Cocoa class that represents the delegate of UNUserNotificationCenter and
// can forward commands to C++. // can forward commands to C++.
API_AVAILABLE(macosx(10.14)) API_AVAILABLE(macosx(10.14))
...@@ -44,6 +53,7 @@ NotificationPlatformBridgeMacUNNotification:: ...@@ -44,6 +53,7 @@ NotificationPlatformBridgeMacUNNotification::
// TODO(crbug/1129366): Determine when to request permission // TODO(crbug/1129366): Determine when to request permission
NotificationPlatformBridgeMacUNNotification::RequestPermission(); NotificationPlatformBridgeMacUNNotification::RequestPermission();
NotificationPlatformBridgeMacUNNotification::CreateDefaultCategories();
} }
NotificationPlatformBridgeMacUNNotification:: NotificationPlatformBridgeMacUNNotification::
...@@ -90,7 +100,13 @@ void NotificationPlatformBridgeMacUNNotification::Display( ...@@ -90,7 +100,13 @@ void NotificationPlatformBridgeMacUNNotification::Display(
UNMutableNotificationContent* content = [builder buildUserNotification]; UNMutableNotificationContent* content = [builder buildUserNotification];
// TODO(crbug/1129398): Add close button to banners // TODO(crbug/1136061): Add support for complex categories and move setting
// the categories to the place that will be building the complex categories
if (notification.should_show_settings_button())
[content setCategoryIdentifier:kCloseAndSettingsCategory];
else
[content setCategoryIdentifier:kCloseCategory];
UNNotificationRequest* request = [UNNotificationRequest UNNotificationRequest* request = [UNNotificationRequest
requestWithIdentifier:base::SysUTF8ToNSString(notification.id()) requestWithIdentifier:base::SysUTF8ToNSString(notification.id())
content:content content:content
...@@ -191,6 +207,38 @@ void NotificationPlatformBridgeMacUNNotification::RequestPermission() { ...@@ -191,6 +207,38 @@ void NotificationPlatformBridgeMacUNNotification::RequestPermission() {
}]; }];
} }
void NotificationPlatformBridgeMacUNNotification::CreateDefaultCategories() {
UNNotificationAction* closeButton = [UNNotificationAction
actionWithIdentifier:notification_constants::kNotificationCloseButtonTag
title:l10n_util::GetNSString(IDS_NOTIFICATION_BUTTON_CLOSE)
options:UNNotificationActionOptionNone];
UNNotificationAction* settingsButton = [UNNotificationAction
actionWithIdentifier:notification_constants::
kNotificationSettingsButtonTag
title:l10n_util::GetNSString(
IDS_NOTIFICATION_BUTTON_SETTINGS)
options:UNNotificationActionOptionForeground];
// The actions in categories are ordered by LIFO. So having closeButton at the
// end ensures that it is always the button on top.
UNNotificationCategory* closeAndSettingsCategory = [UNNotificationCategory
categoryWithIdentifier:kCloseAndSettingsCategory
actions:@[ settingsButton, closeButton ]
intentIdentifiers:@[]
options:UNNotificationCategoryOptionCustomDismissAction];
UNNotificationCategory* closeCategory = [UNNotificationCategory
categoryWithIdentifier:kCloseCategory
actions:@[ closeButton ]
intentIdentifiers:@[]
options:UNNotificationCategoryOptionCustomDismissAction];
[notification_center_
setNotificationCategories:[NSSet setWithObjects:closeAndSettingsCategory,
closeCategory, nil]];
}
// ///////////////////////////////////////////////////////////////////////////// // /////////////////////////////////////////////////////////////////////////////
@implementation UNNotificationCenterDelegate @implementation UNNotificationCenterDelegate
......
...@@ -7,6 +7,7 @@ ...@@ -7,6 +7,7 @@
#import <UserNotifications/UserNotifications.h> #import <UserNotifications/UserNotifications.h>
#include "base/check.h" #include "base/check.h"
#include "base/notreached.h"
#include "chrome/browser/ui/cocoa/notifications/notification_constants_mac.h" #include "chrome/browser/ui/cocoa/notifications/notification_constants_mac.h"
#include "chrome/browser/ui/cocoa/notifications/notification_operation.h" #include "chrome/browser/ui/cocoa/notifications/notification_operation.h"
...@@ -39,14 +40,28 @@ ...@@ -39,14 +40,28 @@
NSNumber* notificationType = NSNumber* notificationType =
[userInfo objectForKey:notification_constants::kNotificationType]; [userInfo objectForKey:notification_constants::kNotificationType];
NotificationOperation operation =
[[response actionIdentifier]
isEqual:UNNotificationDismissActionIdentifier]
? NotificationOperation::NOTIFICATION_CLOSE
: NotificationOperation::NOTIFICATION_CLICK;
int buttonIndex = notification_constants::kNotificationInvalidButtonIndex; int buttonIndex = notification_constants::kNotificationInvalidButtonIndex;
NotificationOperation operation = NotificationOperation::NOTIFICATION_CLICK;
if ([[response actionIdentifier]
isEqual:UNNotificationDismissActionIdentifier]) {
operation = NotificationOperation::NOTIFICATION_CLOSE;
} else if ([[response actionIdentifier]
isEqual:UNNotificationDefaultActionIdentifier]) {
operation = NotificationOperation::NOTIFICATION_CLICK;
} else if ([[response actionIdentifier]
isEqualToString:notification_constants::
kNotificationCloseButtonTag]) {
operation = NotificationOperation::NOTIFICATION_CLOSE;
} else if ([[response actionIdentifier]
isEqualToString:notification_constants::
kNotificationSettingsButtonTag]) {
operation = NotificationOperation::NOTIFICATION_SETTINGS;
} else {
NOTREACHED();
}
return @{ return @{
notification_constants::kNotificationOrigin : origin, notification_constants::kNotificationOrigin : origin,
notification_constants::kNotificationId : notificationId, notification_constants::kNotificationId : notificationId,
......
...@@ -156,3 +156,57 @@ TEST(UNNotificationResponseBuilderMacTest, TestNotificationClose) { ...@@ -156,3 +156,57 @@ TEST(UNNotificationResponseBuilderMacTest, TestNotificationClose) {
buttonIndex.intValue); buttonIndex.intValue);
} }
} }
TEST(UNNotificationResponseBuilderMacTest, TestNotificationCloseButton) {
if (@available(macOS 10.14, *)) {
base::scoped_nsobject<UNNotificationBuilder> builder =
NewTestBuilder(NotificationHandler::Type::WEB_PERSISTENT);
UNMutableNotificationContent* content = [builder buildUserNotification];
base::scoped_nsobject<NSMutableDictionary> userInfo(
[[content userInfo] mutableCopy]);
FakeUNNotificationResponse* fakeResponse = CreateFakeResponse(userInfo);
fakeResponse.actionIdentifier =
notification_constants::kNotificationCloseButtonTag;
NSDictionary* response = [UNNotificationResponseBuilder
buildDictionary:static_cast<UNNotificationResponse*>(fakeResponse)];
NSNumber* operation =
[response objectForKey:notification_constants::kNotificationOperation];
NSNumber* buttonIndex = [response
objectForKey:notification_constants::kNotificationButtonIndex];
EXPECT_EQ(static_cast<int>(NotificationOperation::NOTIFICATION_CLOSE),
operation.intValue);
EXPECT_EQ(notification_constants::kNotificationInvalidButtonIndex,
buttonIndex.intValue);
}
}
TEST(UNNotificationResponseBuilderMacTest, TestNotificationSettingsButton) {
if (@available(macOS 10.14, *)) {
base::scoped_nsobject<UNNotificationBuilder> builder =
NewTestBuilder(NotificationHandler::Type::WEB_PERSISTENT);
UNMutableNotificationContent* content = [builder buildUserNotification];
base::scoped_nsobject<NSMutableDictionary> userInfo(
[[content userInfo] mutableCopy]);
FakeUNNotificationResponse* fakeResponse = CreateFakeResponse(userInfo);
fakeResponse.actionIdentifier =
notification_constants::kNotificationSettingsButtonTag;
NSDictionary* response = [UNNotificationResponseBuilder
buildDictionary:static_cast<UNNotificationResponse*>(fakeResponse)];
NSNumber* operation =
[response objectForKey:notification_constants::kNotificationOperation];
NSNumber* buttonIndex = [response
objectForKey:notification_constants::kNotificationButtonIndex];
EXPECT_EQ(static_cast<int>(NotificationOperation::NOTIFICATION_SETTINGS),
operation.intValue);
EXPECT_EQ(notification_constants::kNotificationInvalidButtonIndex,
buttonIndex.intValue);
}
}
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