( device: DeviceInfo, packageName: string, payload: AndroidBroadcastPayload, )
| 9 | }; |
| 10 | |
| 11 | export async function pushAndroidNotification( |
| 12 | device: DeviceInfo, |
| 13 | packageName: string, |
| 14 | payload: AndroidBroadcastPayload, |
| 15 | ): Promise<{ action: string; extrasCount: number }> { |
| 16 | const action = |
| 17 | typeof payload.action === 'string' && payload.action.trim() |
| 18 | ? payload.action.trim() |
| 19 | : `${packageName}.TEST_PUSH`; |
| 20 | const args = ['shell', 'am', 'broadcast', '-a', action, '-p', packageName]; |
| 21 | const receiver = typeof payload.receiver === 'string' ? payload.receiver.trim() : ''; |
| 22 | if (receiver) { |
| 23 | args.push('-n', receiver); |
| 24 | } |
| 25 | const rawExtras = payload.extras; |
| 26 | if ( |
| 27 | rawExtras !== undefined && |
| 28 | (typeof rawExtras !== 'object' || rawExtras === null || Array.isArray(rawExtras)) |
| 29 | ) { |
| 30 | throw new AppError('INVALID_ARGS', 'Android push payload extras must be an object'); |
| 31 | } |
| 32 | const extras = rawExtras ?? {}; |
| 33 | let extrasCount = 0; |
| 34 | for (const [key, rawValue] of Object.entries(extras)) { |
| 35 | if (!key) continue; |
| 36 | appendBroadcastExtra(args, key, rawValue); |
| 37 | extrasCount += 1; |
| 38 | } |
| 39 | await runAndroidAdb(device, args); |
| 40 | return { action, extrasCount }; |
| 41 | } |
| 42 | |
| 43 | function appendBroadcastExtra(args: string[], key: string, value: unknown): void { |
| 44 | if (typeof value === 'string') { |
no test coverage detected