From d6180e445760d2ef1521e53c18176cd8f35e53f1 Mon Sep 17 00:00:00 2001 From: James Babcock Date: Sat, 10 Oct 2026 17:03:45 -0700 Subject: [PATCH] Use ANCS notification categories for icons --- CHANGELOG | 1 + app/graphics/icons.ts | 19 ++++++++++ app/native/notification-icons.ios.ts | 26 ++++++++++---- tests/ios-notification-icons.test.cjs | 51 +++++++++++++++++++++++++++ 4 files changed, 90 insertions(+), 7 deletions(-) create mode 100644 tests/ios-notification-icons.test.cjs diff --git a/CHANGELOG b/CHANGELOG index 1c45f736..77c8e6b3 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -5,6 +5,7 @@ * Fix several issues that would cause disconnection and disconnect-looping * Add a client for T3 Code * Weather app shows an icon for current conditions, and can add the weather to the status bar and the Glanceboard system card (Weather menu > Settings) +* iOS Notifications have an icon based on ANCS category ## 0.8.2 - 2026-10-03 diff --git a/app/graphics/icons.ts b/app/graphics/icons.ts index c18b69ae..65862693 100644 --- a/app/graphics/icons.ts +++ b/app/graphics/icons.ts @@ -70,6 +70,25 @@ export const ICON_SVGS = { '', calendar: '', + // iOS notification categories (with bell, message-circle and calendar). + "phone-incoming": + '', + "phone-missed": + '', + voicemail: + '', + mail: + '', + newspaper: + '', + "heart-pulse": + '', + briefcase: + '', + "map-pin": + '', + tv: + '', "cloud-sun": '', "flask-conical": diff --git a/app/native/notification-icons.ios.ts b/app/native/notification-icons.ios.ts index 95a387ed..4b327118 100644 --- a/app/native/notification-icons.ios.ts +++ b/app/native/notification-icons.ios.ts @@ -1,6 +1,8 @@ import { GrayImage } from '../graphics/image' +import { renderIcon, type IconName } from '../graphics/icons' import { AncsClient, ANCS_CONNECT_MESSAGE, type AncsState } from '../g2/ancs-client' import { rememberNotificationSources } from './notification-sources' +import type { AndroidNotification } from './notification-types' export type { AndroidNotification, AndroidNotificationAction } from './notification-types' export type { NotificationIconsResult, NotificationIconResult } from './notification-icons' @@ -26,8 +28,17 @@ export function dismissNotification(key: string): boolean { return active?.dismi export function onAndroidNotificationPosted(listener: (key: string) => void): () => void { listeners.add(listener); return () => { listeners.delete(listener) } } -// ANCS does not provide app icons. Use a local bell; never fetch notification -// sources or content from a third-party icon service. +// ANCS does not provide app icons, so show one for the notification's ANCS +// category; never fetch notification sources or content from a third-party +// icon service. Indexed by CategoryID: Other, IncomingCall, MissedCall, +// Voicemail, Social, Schedule, Email, News, HealthAndFitness, +// BusinessAndFinance, Location, Entertainment. +const CATEGORY_ICONS: readonly IconName[] = ['bell','phone-incoming','phone-missed','voicemail','message-circle', + 'calendar','mail','newspaper','heart-pulse','briefcase','map-pin','tv'] +function categoryIcon(category: string): GrayImage { + return renderIcon(CATEGORY_ICONS[Number(category)] ?? 'bell',24)?.clone() ?? bell() +} +// Drawn fallback for when the SVG rasterizer fails. function bell(): GrayImage { const image = new GrayImage(24,24,0) image.fillRoundedRect(6,5,12,13,220,5) @@ -37,11 +48,12 @@ function bell(): GrayImage { } export function readActiveNotificationIcons(maxIcons: number, _allowStale: boolean) { // One icon per source, standing for its first notification. - const sources = new Map() - for (const n of readActiveNotifications(ALL_NOTIFICATIONS)) if (!sources.has(n.packageName)) sources.set(n.packageName, n.key) - const keys = Array.from(sources.values()).slice(0,Math.max(0,maxIcons)) - return {icons: keys.map(() => bell()),keys,stale:false} + const sources = new Map() + for (const n of readActiveNotifications(ALL_NOTIFICATIONS)) if (!sources.has(n.packageName)) sources.set(n.packageName, n) + const shown = Array.from(sources.values()).slice(0,Math.max(0,maxIcons)) + return {icons: shown.map(n => categoryIcon(n.category)),keys: shown.map(n => n.key),stale:false} } export function readNotificationIconByKey(key: string, _allowStale: boolean) { - return {icon: readActiveNotifications(ALL_NOTIFICATIONS).some(n => n.key === key) ? bell() : null,stale:false} + const notification = readActiveNotifications(ALL_NOTIFICATIONS).find(n => n.key === key) + return {icon: notification ? categoryIcon(notification.category) : null,stale:false} } diff --git a/tests/ios-notification-icons.test.cjs b/tests/ios-notification-icons.test.cjs new file mode 100644 index 00000000..e47e8146 --- /dev/null +++ b/tests/ios-notification-icons.test.cjs @@ -0,0 +1,51 @@ +const test = require('node:test'); +const assert = require('node:assert/strict'); +const { loader } = require('./helpers/load-typescript.cjs'); + +class FakeImage { + constructor(name) { this.name = name; } + clone() { return new FakeImage(this.name); } + fillRoundedRect() {} + fillRect() {} +} + +function setup(notifications, render = name => new FakeImage(name)) { + const icons = loader({}, { + '../graphics/image': { GrayImage: class extends FakeImage { constructor() { super('drawn-bell'); } } }, + '../graphics/icons': { renderIcon: (name, size) => { assert.equal(size, 24); return render(name); } }, + '../g2/ancs-client': { AncsClient: class {}, ANCS_CONNECT_MESSAGE: '' }, + './notification-sources': { rememberNotificationSources() {} }, + })('app/native/notification-icons.ios.ts'); + icons.bindIosNotifications({ read: () => notifications }); + return icons; +} + +const notification = (key, packageName, category) => ({ key, packageName, category }); + +test('tray icons follow each source\'s first notification category', () => { + const icons = setup([ + notification('a', 'com.apple.mobilephone', '2'), + notification('b', 'com.apple.mobilephone', '1'), + notification('c', 'com.apple.mobilemail', '6'), + notification('d', 'com.example.tv', '11'), + ]); + const result = icons.readActiveNotificationIcons(10, false); + assert.deepEqual([...result.keys], ['a', 'c', 'd']); + assert.deepEqual([...result.icons].map(icon => icon.name), ['phone-missed', 'mail', 'tv']); + assert.equal(icons.readActiveNotificationIcons(1, false).icons.length, 1); +}); + +test('unknown categories use the bell; a failed rasterization uses the drawn bell', () => { + const icons = setup([notification('a', 'x', '0'), notification('b', 'y', '12'), notification('c', 'z', '')]); + assert.deepEqual([...icons.readActiveNotificationIcons(10, false).icons].map(icon => icon.name), ['bell', 'bell', 'bell']); + assert.equal(icons.readNotificationIconByKey('missing', false).icon, null); + + const failing = setup([notification('a', 'x', '4')], () => null); + assert.equal(failing.readNotificationIconByKey('a', false).icon.name, 'drawn-bell'); +}); + +test('icons are copies, so callers can draw on them', () => { + const shared = new FakeImage('message-circle'); + const icons = setup([notification('a', 'x', '4')], () => shared); + assert.notEqual(icons.readNotificationIconByKey('a', false).icon, shared); +});