Display proper app name in iOS notifications

This commit is contained in:
James Babcock committed 2026-09-19 11:46:46 -07:00
1 parent 67823f66ef
commit cef879fec0
5 files changed
+223 -33

No files matched your search

+73 -23
View File
@@ -4,6 +4,7 @@ export const ANCS_FIRMWARE_VERSION = 16
export const ANCS_CONNECT_MESSAGE = 'Connect glasses to receive iPhone notifications.'
const MAX_ACTIVE = 128
const MAX_RESPONSE = 2048
const MAX_COMMAND = 80 // Firmware relay limit, including its 8-byte header.
const HEADER = 11
const read32 = (b: Uint8Array, i: number) => (b[i] | b[i+1]<<8 | b[i+2]<<16 | b[i+3]<<24) >>> 0
const le32 = (n: number) => [n & 255, n>>>8 & 255, n>>>16 & 255, n>>>24]
@@ -28,8 +29,19 @@ function utf8(bytes: Uint8Array): string {
}
return result
}
function fallbackAppName(identifier: string): string {
return identifier.split('.').filter(Boolean).pop() || 'Unknown app'
}
function notificationDate(value: string): number | undefined {
const parts = /^(\d{4})(\d{2})(\d{2})T(\d{2})(\d{2})(\d{2})$/.exec(value)
if (!parts) return undefined
const [year,month,day,hour,minute,second] = parts.slice(1).map(Number)
const date = new Date(year,month-1,day,hour,minute,second)
return date.getFullYear() === year && date.getMonth() === month-1 && date.getDate() === day &&
date.getHours() === hour && date.getMinutes() === minute && date.getSeconds() === second ? date.getTime() : undefined
}
type Source = { flags: number; category: number; revision: number; postTime: number; popup: boolean }
type Request = { uid: number; revision: number; action?: number; labels?: boolean }
type Request = { uid: number; revision: number; action?: number; metadata?: boolean; appId?: string }
export type AncsState = 'disconnected' | 'starting' | 'ready' | 'unavailable'
/** Owns one phone/lens BLE session. Nothing is persisted. Firmware enforces
@@ -46,6 +58,8 @@ export class AncsClient {
private response: number[] = []
private sources = new Map<number, Source>()
private notifications = new Map<number, AndroidNotification>()
private appNames = new Map<string, string>()
private maxWrite = 20
private queue: Request[] = []
private acknowledged = false
private responseDone = false
@@ -62,8 +76,9 @@ export class AncsClient {
this.state = state; this.statusMessage = message; this.log(message); this.changed()
}
start(token: number): void {
start(token: number, maxWrite = 20): void {
this.stop(); this.disposed = false; this.token = token >>> 0 || 1
this.maxWrite = Math.min(MAX_COMMAND, maxWrite)
this.begin()
}
private begin(): void {
@@ -84,7 +99,7 @@ export class AncsClient {
stopCommand(): Uint8Array { return new Uint8Array([65,78,1,1,...le32(this.lastStartToken)]) }
private clear(): void {
if (this.timer !== null) clearTimeout(this.timer)
this.timer = null; this.sources.clear(); this.notifications.clear(); this.queue = []
this.timer = null; this.sources.clear(); this.notifications.clear(); this.appNames.clear(); this.queue = []
this.pending = null; this.response = []; this.fragments = []; this.packetKind = -1; this.sequence = 0
}
private recover(message = 'Notification relay interrupted. Retrying…'): void {
@@ -178,9 +193,12 @@ export class AncsClient {
const request = this.queue.shift()!
if (this.sources.get(request.uid)?.revision !== request.revision) continue
this.pending = request; this.response = []; this.acknowledged = this.responseDone = false; this.deadline()
// Exactly three attributes, fits even the minimum 20-byte ATT payload.
const cp = request.action === undefined
? request.labels ? [0,...le32(request.uid),6,7] : [0,...le32(request.uid),0,1,128,0,3,0,2]
// Notification attributes use two requests, each fitting a 20-byte ATT
// payload. App IDs cannot be fragmented by this firmware's CP relay.
const cp = request.appId !== undefined
? [1,...Array.from(request.appId, c => c.charCodeAt(0)),0,0]
: request.action === undefined
? request.metadata ? [0,...le32(request.uid),2,128,0,4,5,6,7] : [0,...le32(request.uid),0,1,128,0,3,0,2]
: [2,...le32(request.uid),request.action]
const token = this.token
void this.send(this.command(2,cp)).catch(() => { if (token === this.token) this.recover() })
@@ -192,10 +210,21 @@ export class AncsClient {
if (this.response.length + data.length > MAX_RESPONSE) { this.recover(); return }
this.response.push(...data)
const b = new Uint8Array(this.response)
if (b.length < 5) return
if (b[0] !== 0 || read32(b,1) !== this.pending.uid) { this.recover(); return }
const expected = this.pending.labels ? [6,7] : [0,1,3]
let cursor = 5
if (!b.length) return
const appId = this.pending.appId
let cursor: number
if (appId !== undefined) {
if (b[0] !== 1) { this.recover(); return }
const end = b.indexOf(0,1)
if (end < 0) { if (b.length > appId.length+1) this.recover(); return }
if (utf8(b.subarray(1,end)) !== appId) { this.recover(); return }
cursor = end+1
} else {
if (b.length < 5) return
if (b[0] !== 0 || read32(b,1) !== this.pending.uid) { this.recover(); return }
cursor = 5
}
const expected = appId !== undefined ? [0] : this.pending.metadata ? [2,4,5,6,7] : [0,1,3]
const attrs = new Map<number,string>()
while (cursor < b.length) {
if (cursor+3 > b.length) return
@@ -206,30 +235,51 @@ export class AncsClient {
}
if (attrs.size !== expected.length) return
const {uid,revision} = this.pending, source = this.sources.get(uid)
if (appId !== undefined) {
const displayName = attrs.get(0)!.trim()
const name = displayName && displayName !== appId ? displayName : fallbackAppName(appId)
this.appNames.set(appId,name)
while (this.appNames.size > MAX_ACTIVE) this.appNames.delete(this.appNames.keys().next().value!)
for (const notification of this.notifications.values()) {
if (notification.packageName === appId) notification.appName = name
}
}
if (source?.revision === revision) {
const key = `ancs:${this.token}:${uid}`
if (this.pending.labels) {
if (appId !== undefined) {
this.publish(uid,source)
} else if (this.pending.metadata) {
const notification = this.notifications.get(uid)
if (notification) notification.actions = [
...(source.flags & 8 && attrs.get(6) ? [{index:0,title:attrs.get(6)!,enabled:true}] : []),
...(source.flags & 16 && attrs.get(7) ? [{index:1,title:attrs.get(7)!,enabled:true}] : []),
]
const popup = source.popup; source.popup = false
this.changed(key,popup)
if (notification) {
notification.subText = attrs.get(2)!
notification.when = notificationDate(attrs.get(5)!) ?? source.postTime
const size = attrs.get(4)!
if (/^\d+$/.test(size) && Number.isSafeInteger(Number(size))) notification.messageSize = Number(size)
notification.actions = [
...(source.flags & 8 && attrs.get(6) ? [{index:0,title:attrs.get(6)!,enabled:true}] : []),
...(source.flags & 16 && attrs.get(7) ? [{index:1,title:attrs.get(7)!,enabled:true}] : []),
]
const id = notification.packageName
// Bundle IDs are ASCII. Reject embedded NULs/invalid IDs and use the
// readable fallback if the entire command cannot fit either link.
if (!this.appNames.has(id) && /^[A-Za-z0-9.-]+$/.test(id) && id.length+11 <= this.maxWrite) {
this.queue.unshift({uid,revision,appId:id})
} else this.publish(uid,source)
}
} else {
const packageName = attrs.get(0)!, title = attrs.get(1)!, text = attrs.get(3)!
this.notifications.set(uid,{key,packageName,appName:packageName,title,text,bigText:text,
this.notifications.set(uid,{key,packageName,appName:this.appNames.get(packageName) || fallbackAppName(packageName),title,text,bigText:text,
subText:'',infoText:'',summaryText:'',category:String(source.category),lines:[],postTime:source.postTime,when:source.postTime,actions:[],dismissLabel:'Hide on glasses'})
if (source.flags & 24) {
this.queue.unshift({uid,revision,labels:true}); this.changed(key,false)
} else {
const popup = source.popup; source.popup = false; this.changed(key,popup)
}
this.queue.unshift({uid,revision,metadata:true}); this.changed(key,false)
}
}
this.responseDone = true
if (this.acknowledged) this.complete()
}
private publish(uid: number, source: Source): void {
const popup = source.popup; source.popup = false
this.changed(`ancs:${this.token}:${uid}`,popup)
}
private complete(): void {
if (this.timer !== null) clearTimeout(this.timer)
this.timer = null; this.pending = null; this.response = []; this.pump()
+2 -2
View File
@@ -145,7 +145,7 @@ export class GlassesSession {
this.displayed = null; this.retryCount = 0
this.update('connected', 'Glasses connected')
if (/^Faceclaw\/(\d+)/.test(this.state.capabilities) && Number(this.state.capabilities.split('/')[1]) >= ANCS_FIRMWARE_VERSION)
this.notifications.start((Date.now() ^ Math.floor(Math.random()*0xffffffff)) >>> 0)
this.notifications.start((Date.now() ^ Math.floor(Math.random()*0xffffffff)) >>> 0, this.limits.right)
else this.notifications.stop(`Update glasses to Faceclaw firmware ${ANCS_FIRMWARE_VERSION} or newer for iPhone notifications.`)
await this.syncCompass(); this.check(generation)
this.schedule()
@@ -327,7 +327,7 @@ export class GlassesSession {
this.notifications.stop('Enable Share System Notifications in iPhone Settings → Bluetooth → right lens.')
if (active && this.state.phase === 'connected') void this.writePackets('right', () => [command]).catch(() => {})
} else if (this.state.phase === 'connected' && /^Faceclaw\/(\d+)/.test(this.state.capabilities) && Number(this.state.capabilities.split('/')[1]) >= ANCS_FIRMWARE_VERSION) {
this.notifications.start((Date.now() ^ Math.floor(Math.random()*0xffffffff)) >>> 0)
this.notifications.start((Date.now() ^ Math.floor(Math.random()*0xffffffff)) >>> 0, this.limits.right)
}
return
}