-
Notifications
You must be signed in to change notification settings - Fork 291
Expand file tree
/
Copy pathPushMessageParser.ts
More file actions
56 lines (51 loc) · 1.76 KB
/
Copy pathPushMessageParser.ts
File metadata and controls
56 lines (51 loc) · 1.76 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
import type { FirebaseMessagingTypes } from '@react-native-firebase/messaging'
import { asMaybe, asObject, asOptional, asString, asValue } from 'cleaners'
import { showDevError } from '../components/services/AirshipInstance'
import type { DeepLink } from '../types/DeepLinkTypes'
import { parseDeepLink } from './DeepLinkParser'
/**
* Extracts a deep link from a push message, if present.
*/
export function parsePushMessage(
message: FirebaseMessagingTypes.RemoteMessage
): DeepLink | undefined {
const priceChange = asMaybe(asPriceChangePayloadData)(message.data)
if (priceChange != null) {
return {
type: 'price-change',
pluginId: priceChange?.pluginId,
body: asString(message.notification?.body)
}
}
const marketing = asMaybe(asMarketingPayloadData)(message.data)
if (marketing != null) {
let link: DeepLink | undefined
if (marketing.url != null) {
try {
const parsed = parseDeepLink(marketing.url)
// Only navigate for a recognized Edge deep link. `parseDeepLink` is
// lenient and returns an `other` link for anything it does not know, so
// an unrecognized or malformed URL degrades to track-only.
if (parsed.type !== 'other') link = parsed
} catch (error: unknown) {
// parseDeepLink can still throw on some malformed input; never let that
// drop the push, or we would also lose the open tracking.
showDevError(error)
}
}
return {
type: 'marketing',
campaignId: marketing.campaignId,
link
}
}
}
const asPriceChangePayloadData = asObject({
type: asValue('price-change'),
pluginId: asString
})
const asMarketingPayloadData = asObject({
type: asValue('marketing'),
campaignId: asString,
url: asOptional(asString)
})